|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
5
|
6
|
import org.springframework.http.HttpHeaders;
|
|
6
|
7
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -25,7 +26,7 @@ public class PollController {
|
|
25
|
26
|
}
|
|
26
|
27
|
|
|
27
|
28
|
@RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
28
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
|
29
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
|
|
29
|
30
|
poll = pollRepository.save(poll);
|
|
30
|
31
|
HttpHeaders httpHeaders = new HttpHeaders();
|
|
31
|
32
|
URI newPollUri = ServletUriComponentsBuilder
|
|
|
@@ -39,12 +40,14 @@ public class PollController {
|
|
39
|
40
|
|
|
40
|
41
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
41
|
42
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
43
|
+ verifyPoll(pollId);
|
|
42
|
44
|
Poll p = pollRepository.findOne(pollId);
|
|
43
|
45
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
44
|
46
|
}
|
|
45
|
47
|
|
|
46
|
48
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
47
|
49
|
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
50
|
+ verifyPoll(pollId);
|
|
48
|
51
|
// Save the entity
|
|
49
|
52
|
Poll p = pollRepository.save(poll);
|
|
50
|
53
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
|
@@ -52,11 +55,21 @@ public class PollController {
|
|
52
|
55
|
|
|
53
|
56
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
54
|
57
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
58
|
+ verifyPoll(pollId);
|
|
55
|
59
|
pollRepository.delete(pollId);
|
|
56
|
60
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
57
|
61
|
}
|
|
58
|
|
-}
|
|
59
|
62
|
|
|
|
63
|
+ public void verifyPoll(@PathVariable Long pollId){
|
|
|
64
|
+ if(!pollRepository.exists(pollId)){
|
|
|
65
|
+ throw new ResourceNotFoundException("Poll with id does not exist");
|
|
|
66
|
+ }
|
|
|
67
|
+ }
|
|
|
68
|
+}
|
|
60
|
69
|
|
|
|
70
|
+//checks if a specific poll id exists and throws a
|
|
|
71
|
+// ResourceNotFoundException if not. Use this in any
|
|
|
72
|
+// method that searches for or updates an existing poll
|
|
|
73
|
+// (eg: Get, Put, and Delete methods).
|
|
61
|
74
|
|
|
62
|
75
|
|