|
|
@@ -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;
|
|
|
@@ -9,6 +10,7 @@ import org.springframework.http.ResponseEntity;
|
|
9
|
10
|
import org.springframework.web.bind.annotation.*;
|
|
10
|
11
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
11
|
12
|
|
|
|
13
|
+import javax.validation.Valid;
|
|
12
|
14
|
import java.net.URI;
|
|
13
|
15
|
|
|
14
|
16
|
@RestController
|
|
|
@@ -28,7 +30,7 @@ public class PollController {
|
|
28
|
30
|
}
|
|
29
|
31
|
|
|
30
|
32
|
@RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
31
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
|
33
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
|
|
32
|
34
|
poll = pollRepository.save(poll);
|
|
33
|
35
|
URI newPollUri = ServletUriComponentsBuilder
|
|
34
|
36
|
.fromCurrentRequest()
|
|
|
@@ -42,21 +44,34 @@ public class PollController {
|
|
42
|
44
|
|
|
43
|
45
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
44
|
46
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
47
|
+ verifyPoll(pollId);
|
|
45
|
48
|
Poll p = pollRepository.findOne(pollId);
|
|
46
|
49
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
47
|
50
|
}
|
|
48
|
51
|
|
|
49
|
52
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
50
|
|
- public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
53
|
+ public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
54
|
+ verifyPoll(pollId);
|
|
51
|
55
|
Poll p = pollRepository.save(poll);
|
|
52
|
56
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
53
|
57
|
}
|
|
54
|
58
|
|
|
55
|
59
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
56
|
60
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
61
|
+ verifyPoll(pollId);
|
|
57
|
62
|
pollRepository.delete(pollId);
|
|
58
|
63
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
59
|
64
|
}
|
|
60
|
65
|
|
|
61
|
|
-
|
|
|
66
|
+ public void verifyPoll(Long id) {
|
|
|
67
|
+ Iterable<Poll> allPolls = pollRepository.findAll();
|
|
|
68
|
+ boolean verified = false;
|
|
|
69
|
+ for(Poll poll : allPolls){
|
|
|
70
|
+ if(poll.getId() == id) {
|
|
|
71
|
+ verified = true;
|
|
|
72
|
+ }
|
|
|
73
|
+ }
|
|
|
74
|
+ if(!verified)
|
|
|
75
|
+ throw new ResourceNotFoundException();
|
|
|
76
|
+ }
|
|
62
|
77
|
}
|