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
72 lines (61 loc) · 2.65 KB
/
Copy pathTimeEntryController.java
File metadata and controls
72 lines (61 loc) · 2.65 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
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;
import java.util.concurrent.CountDownLatch;
@RestController
@RequestMapping("/time-entries")
public class TimeEntryController {
private TimeEntryRepository timeEntryRepository;
private CounterService counterService;
private GaugeService gaugeService;
public TimeEntryController(
TimeEntryRepository timeEntryRepository,
CounterService counterService,
GaugeService gaugeService) {
this.timeEntryRepository = timeEntryRepository;
this.counterService = counterService;
this.gaugeService = gaugeService;
}
@GetMapping
public ResponseEntity<List<TimeEntry>> list() {
counterService.increment("Time.Listed");
return new ResponseEntity<>(timeEntryRepository.list(),HttpStatus.OK);
}
@PostMapping
public ResponseEntity<TimeEntry> create(@RequestBody TimeEntry timeEntryToCreate) {
counterService.increment("Time.Created");
gaugeService.submit("Time.NumberOfEntries", timeEntryRepository.list().size());
return new ResponseEntity<TimeEntry>(timeEntryRepository.create(timeEntryToCreate),HttpStatus.CREATED);
}
@GetMapping("{id}")
public ResponseEntity<TimeEntry> read (@PathVariable long id) {
TimeEntry timeEntry = timeEntryRepository.find(id);
if (timeEntry == null) {
return new ResponseEntity<TimeEntry>(HttpStatus.NOT_FOUND);
} else {
counterService.increment("Time.Read");
return new ResponseEntity<TimeEntry>(timeEntry, HttpStatus.OK);
}
}
@PutMapping("{id}")
public ResponseEntity<TimeEntry> update(@PathVariable Long id, @RequestBody TimeEntry timeEntry) {
TimeEntry updatedTimeEntry = timeEntryRepository.update(id, timeEntry);
if (updatedTimeEntry != null) {
counterService.increment("Time.Updated");
return new ResponseEntity<>(updatedTimeEntry, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("{id}")
public ResponseEntity<TimeEntry> delete (@PathVariable long id) {
timeEntryRepository.delete(id);
counterService.increment("Time.Deleted");
gaugeService.submit("Time.NumberOfEntries", timeEntryRepository.list().size());
return new ResponseEntity<TimeEntry>(HttpStatus.NO_CONTENT);
}
}