forked from msrraju13/pal-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeEntryController.java
More file actions
57 lines (45 loc) · 1.78 KB
/
Copy pathTimeEntryController.java
File metadata and controls
57 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package io.pivotal.pal.tracker;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/time-entries")
public class TimeEntryController {
private TimeEntryRepository timeEntriesRepo;
public TimeEntryController(TimeEntryRepository timeEntriesRepo) {
this.timeEntriesRepo = timeEntriesRepo;
}
@PostMapping
public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntry) {
TimeEntry createdTimeEntry = timeEntriesRepo.create(timeEntry);
return new ResponseEntity<>(createdTimeEntry, HttpStatus.CREATED);
}
@GetMapping("{id}")
public ResponseEntity<TimeEntry> read(@PathVariable Long id) {
TimeEntry timeEntry = timeEntriesRepo.find(id);
if (timeEntry != null) {
return new ResponseEntity<>(timeEntry, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping
public ResponseEntity<List<TimeEntry>> list() {
return new ResponseEntity<>(timeEntriesRepo.list(), HttpStatus.OK);
}
@PutMapping("{id}")
public ResponseEntity<TimeEntry> update(@PathVariable Long id, @RequestBody TimeEntry timeEntry) {
TimeEntry updatedTimeEntry = timeEntriesRepo.update(id, timeEntry);
if (updatedTimeEntry != null) {
return new ResponseEntity<>(updatedTimeEntry, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("{id}")
public ResponseEntity<TimeEntry> delete(@PathVariable Long id) {
timeEntriesRepo.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}