|
@@ -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.excepection.ResourceNotFoundException;
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.repository.PollRepository;
|
5
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
7
|
import org.springframework.http.HttpStatus;
|
|
@@ -41,22 +42,28 @@ public class PollController {
|
41
|
42
|
|
42
|
43
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
43
|
44
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
45
|
+ verifyPoll(pollId);
|
44
|
46
|
Poll p = pollRepository.findOne(pollId);
|
45
|
47
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
46
|
48
|
}
|
47
|
49
|
|
48
|
50
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
49
|
51
|
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
50
|
|
- // Save the entity
|
|
52
|
+ verifyPoll(pollId);
|
51
|
53
|
Poll p = pollRepository.save(poll);
|
52
|
54
|
return new ResponseEntity<>(HttpStatus.OK);
|
53
|
55
|
}
|
54
|
56
|
|
55
|
57
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
56
|
58
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
59
|
+ verifyPoll(pollId);
|
57
|
60
|
pollRepository.delete(pollId);
|
58
|
61
|
return new ResponseEntity<>(HttpStatus.OK);
|
59
|
62
|
}
|
60
|
63
|
|
|
64
|
+ private void verifyPoll(Long pollId) {
|
|
65
|
+ Poll poll = pollRepository.findOne(pollId);
|
|
66
|
+ if(poll == null) throw new ResourceNotFoundException();
|
|
67
|
+ }
|
61
|
68
|
|
62
|
69
|
}
|