|
|
@@ -1,6 +1,8 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.domain.Option;
|
|
3
|
4
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
|
5
|
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
4
|
6
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
5
|
7
|
import org.springframework.http.HttpHeaders;
|
|
6
|
8
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -32,27 +34,40 @@ public class PollController {
|
|
32
|
34
|
.toUri();
|
|
33
|
35
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
34
|
36
|
responseHeaders.setLocation(newPollUri);
|
|
|
37
|
+ for (Option option:poll.getOptions()) {
|
|
|
38
|
+ System.out.println(option.toString());
|
|
|
39
|
+ }
|
|
35
|
40
|
poll = pollRepository.save(poll);
|
|
36
|
|
- return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
|
|
|
41
|
+ return new ResponseEntity<>(poll, responseHeaders, HttpStatus.CREATED);
|
|
37
|
42
|
}
|
|
38
|
43
|
|
|
39
|
44
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
40
|
45
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
46
|
+ verifyPoll(pollId);
|
|
41
|
47
|
Poll p = pollRepository.findOne(pollId);
|
|
42
|
48
|
return new ResponseEntity<> (p, HttpStatus.OK);
|
|
43
|
49
|
}
|
|
44
|
50
|
|
|
45
|
51
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
46
|
52
|
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
53
|
+ verifyPoll(pollId);
|
|
47
|
54
|
// Save the entity
|
|
48
|
55
|
Poll p = pollRepository.save(poll);
|
|
49
|
|
- return new ResponseEntity<>(HttpStatus.OK);
|
|
|
56
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
|
50
|
57
|
}
|
|
51
|
58
|
|
|
52
|
59
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
53
|
60
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
61
|
+ verifyPoll(pollId);
|
|
54
|
62
|
pollRepository.delete(pollId);
|
|
55
|
63
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
56
|
64
|
}
|
|
57
|
65
|
|
|
|
66
|
+ public void verifyPoll(Long pollid){
|
|
|
67
|
+ Poll poll = pollRepository.findOne(pollid);
|
|
|
68
|
+ if (poll == null){
|
|
|
69
|
+ throw new ResourceNotFoundException("Unable to verify poll");
|
|
|
70
|
+ }
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
58
|
73
|
}
|