|
|
@@ -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.beans.factory.annotation.Autowired;
|
|
6
|
7
|
import org.springframework.http.HttpHeaders;
|
|
|
@@ -43,6 +44,7 @@ public class PollController {
|
|
43
|
44
|
|
|
44
|
45
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
45
|
46
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
47
|
+ verifyPoll(pollId);
|
|
46
|
48
|
Poll p = pollRepository.findOne(pollId);
|
|
47
|
49
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
48
|
50
|
}
|
|
|
@@ -50,14 +52,23 @@ public class PollController {
|
|
50
|
52
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
51
|
53
|
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
52
|
54
|
// Save the entity
|
|
|
55
|
+ verifyPoll(pollId);
|
|
53
|
56
|
Poll p = pollRepository.save(poll);
|
|
54
|
57
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
55
|
58
|
}
|
|
56
|
59
|
|
|
57
|
60
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
58
|
61
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
62
|
+ verifyPoll(pollId);
|
|
59
|
63
|
pollRepository.delete(pollId);
|
|
60
|
64
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
61
|
65
|
}
|
|
62
|
66
|
|
|
|
67
|
+ public void verifyPoll(Long pollId){
|
|
|
68
|
+ Poll poll = pollRepository.findOne(pollId);
|
|
|
69
|
+ if(pollId == null){
|
|
|
70
|
+ throw new ResourceNotFoundException("The given poll id" + pollId + "does not exist!");
|
|
|
71
|
+ }
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
63
|
74
|
}
|