|
| 1 | +package io.pivotal.pal.tracker; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.springframework.http.HttpStatus; |
| 6 | +import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.util.StringUtils; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.PostMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestBody; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 14 | +import org.springframework.web.bind.annotation.RestController; |
| 15 | + |
| 16 | +@RestController |
| 17 | +public class TimeEntryController { |
| 18 | + |
| 19 | + public TimeEntryController(TimeEntryRepository timeEntryRepository) { |
| 20 | + this.timeEntryRepository = timeEntryRepository; |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + private TimeEntryRepository timeEntryRepository; |
| 25 | + |
| 26 | + |
| 27 | + @RequestMapping(value="/time-entries", method = RequestMethod.POST) |
| 28 | + public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntryToCreate) { |
| 29 | + TimeEntry createdTimeEntry = timeEntryRepository.create(timeEntryToCreate); |
| 30 | + return new ResponseEntity<TimeEntry>(createdTimeEntry, HttpStatus.CREATED); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + @RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.GET) |
| 35 | + public ResponseEntity<TimeEntry> read(@PathVariable("itemid") long l) { |
| 36 | + TimeEntry readTimeEntry = timeEntryRepository.find(l); |
| 37 | + if(!StringUtils.isEmpty(readTimeEntry)) |
| 38 | + return new ResponseEntity<TimeEntry>(readTimeEntry, HttpStatus.OK); |
| 39 | + else |
| 40 | + return new ResponseEntity<TimeEntry>(readTimeEntry, HttpStatus.NOT_FOUND); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @RequestMapping(value="/time-entries", method = RequestMethod.GET) |
| 45 | + public ResponseEntity<List<TimeEntry>> list() { |
| 46 | + List<TimeEntry> listTimeEntry = timeEntryRepository.list(); |
| 47 | + if(!StringUtils.isEmpty(listTimeEntry)) |
| 48 | + return new ResponseEntity<List<TimeEntry>>(listTimeEntry, HttpStatus.OK); |
| 49 | + else |
| 50 | + return new ResponseEntity<List<TimeEntry>>(listTimeEntry, HttpStatus.NOT_FOUND); |
| 51 | + } |
| 52 | + |
| 53 | + @RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.PUT) |
| 54 | + public ResponseEntity update(@PathVariable("itemid") long l, @RequestBody TimeEntry expected) { |
| 55 | + TimeEntry updatedTimeEntry = timeEntryRepository.update(l, expected); |
| 56 | + if(!StringUtils.isEmpty(updatedTimeEntry)) |
| 57 | + return new ResponseEntity<TimeEntry>(updatedTimeEntry, HttpStatus.OK); |
| 58 | + else |
| 59 | + return new ResponseEntity<TimeEntry>(updatedTimeEntry, HttpStatus.NOT_FOUND); |
| 60 | + } |
| 61 | + |
| 62 | + @RequestMapping(value="/time-entries/{itemid}", method = RequestMethod.DELETE) |
| 63 | + public ResponseEntity<TimeEntry> delete(@PathVariable("itemid") long l) { |
| 64 | + timeEntryRepository.delete(l); |
| 65 | + return new ResponseEntity<TimeEntry>(HttpStatus.NO_CONTENT); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments