|
|
@@ -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,6 +26,7 @@ public class PollController {
|
|
25
|
26
|
|
|
26
|
27
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
27
|
28
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
29
|
+ verifyPoll(pollId);
|
|
28
|
30
|
Poll p = pollRepository.findOne(pollId);
|
|
29
|
31
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
30
|
32
|
}
|
|
|
@@ -44,15 +46,23 @@ public class PollController {
|
|
44
|
46
|
|
|
45
|
47
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
46
|
48
|
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
47
|
|
- // Save the entity
|
|
|
49
|
+ verifyPoll(pollId);
|
|
48
|
50
|
Poll p = pollRepository.save(poll);
|
|
49
|
51
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
50
|
52
|
}
|
|
51
|
53
|
|
|
52
|
54
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
53
|
55
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
56
|
+ verifyPoll(pollId);
|
|
54
|
57
|
pollRepository.delete(pollId);
|
|
55
|
58
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
56
|
59
|
}
|
|
57
|
60
|
|
|
|
61
|
+ private void verifyPoll(long pollId) throws ResourceNotFoundException{
|
|
|
62
|
+ Poll poll = pollRepository.findOne(pollId);
|
|
|
63
|
+ if (poll == null) {
|
|
|
64
|
+ throw new ResourceNotFoundException("Poll with this id not found: " + pollId);
|
|
|
65
|
+ }
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
58
|
68
|
}
|