forked from vmware-tanzu-learning/pal-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeEntryController.java
More file actions
74 lines (62 loc) · 2.49 KB
/
TimeEntryController.java
File metadata and controls
74 lines (62 loc) · 2.49 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package io.pivotal.pal.tracker;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
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 final CounterService counter;
private final GaugeService gauge;
private TimeEntryRepository timeEntriesRepo;
public TimeEntryController(
TimeEntryRepository timeEntriesRepo,
CounterService counter,
GaugeService gauge
) {
this.timeEntriesRepo = timeEntriesRepo;
this.counter = counter;
this.gauge = gauge;
}
@PostMapping
public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntry) {
TimeEntry createdTimeEntry = timeEntriesRepo.create(timeEntry);
counter.increment("TimeEntry.created");
gauge.submit("timeEntries.count", timeEntriesRepo.list().size());
return new ResponseEntity<>(createdTimeEntry, HttpStatus.CREATED);
}
@GetMapping("{id}")
public ResponseEntity<TimeEntry> read(@PathVariable Long id) {
TimeEntry timeEntry = timeEntriesRepo.find(id);
if (timeEntry != null) {
counter.increment("TimeEntry.read");
return new ResponseEntity<>(timeEntry, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping
public ResponseEntity<List<TimeEntry>> list() {
counter.increment("TimeEntry.listed");
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) {
counter.increment("TimeEntry.updated");
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);
counter.increment("TimeEntry.deleted");
gauge.submit("timeEntries.count", timeEntriesRepo.list().size());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}