|
|
@@ -1,10 +1,11 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
3
|
4
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
|
6
|
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
5
|
7
|
|
|
6
|
8
|
|
|
7
|
|
-import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
8
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
9
|
10
|
import org.springframework.http.HttpHeaders;
|
|
10
|
11
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.*;
|
|
13
|
14
|
|
|
14
|
15
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
15
|
16
|
|
|
|
17
|
+import javax.validation.Valid;
|
|
16
|
18
|
import java.net.URI;
|
|
17
|
19
|
|
|
18
|
20
|
@RestController
|
|
|
@@ -25,17 +27,9 @@ public class PollController {
|
|
25
|
27
|
this.pollRepository = pollRepository;
|
|
26
|
28
|
}
|
|
27
|
29
|
|
|
28
|
|
- @RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
29
|
|
- public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
30
|
|
- Iterable<Poll> allPolls = pollRepository.findAll();
|
|
31
|
|
- return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
32
|
|
- }
|
|
33
|
|
-
|
|
34
|
30
|
@RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
35
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll){
|
|
36
|
|
-
|
|
|
31
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll){
|
|
37
|
32
|
poll = pollRepository.save(poll);
|
|
38
|
|
-
|
|
39
|
33
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
40
|
34
|
URI newPollUri = ServletUriComponentsBuilder
|
|
41
|
35
|
.fromCurrentRequest()
|
|
|
@@ -43,29 +37,41 @@ public class PollController {
|
|
43
|
37
|
.buildAndExpand(poll.getId())
|
|
44
|
38
|
.toUri();
|
|
45
|
39
|
responseHeaders.setLocation(newPollUri);
|
|
46
|
|
-
|
|
47
|
40
|
return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
|
|
48
|
41
|
}
|
|
49
|
42
|
|
|
|
43
|
+ @RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
|
44
|
+ public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
|
45
|
+ Iterable<Poll> allPolls = pollRepository.findAll();
|
|
|
46
|
+ return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
50
|
49
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
51
|
50
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
52
|
51
|
Poll p = pollRepository.findOne(pollId);
|
|
|
52
|
+ verifyPoll(pollId);
|
|
53
|
53
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
54
|
54
|
}
|
|
55
|
55
|
|
|
56
|
56
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
57
|
|
- public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
57
|
+ public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
|
|
58
|
58
|
// Save the entity
|
|
59
|
59
|
Poll p = pollRepository.save(poll);
|
|
|
60
|
+ verifyPoll(pollId);
|
|
60
|
61
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
61
|
62
|
}
|
|
62
|
63
|
|
|
63
|
64
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
64
|
65
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
65
|
66
|
pollRepository.delete(pollId);
|
|
|
67
|
+ verifyPoll(pollId);
|
|
66
|
68
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
67
|
69
|
}
|
|
68
|
70
|
|
|
69
|
|
-
|
|
|
71
|
+ protected void verifyPoll(Long pollId) throws ResourceNotFoundException {
|
|
|
72
|
+ Poll poll = pollRepository.findOne(pollId);
|
|
|
73
|
+ if(poll == null) {
|
|
|
74
|
+ throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
|
|
|
75
|
+ } }
|
|
70
|
76
|
|
|
71
|
77
|
}
|