|
|
@@ -3,6 +3,7 @@ package io.zipcoder.tc_spring_poll_application.Controller;
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.Domain.Poll;
|
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.Exception.ResourceNotFoundException;
|
|
5
|
5
|
import io.zipcoder.tc_spring_poll_application.Repositories.PollRepository;
|
|
|
6
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
6
|
7
|
import org.springframework.http.HttpHeaders;
|
|
7
|
8
|
import org.springframework.http.HttpStatus;
|
|
8
|
9
|
import org.springframework.http.ResponseEntity;
|
|
|
@@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
|
|
10
|
11
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
11
|
12
|
|
|
12
|
13
|
import javax.inject.Inject;
|
|
|
14
|
+import javax.validation.Valid;
|
|
13
|
15
|
import java.net.URI;
|
|
14
|
16
|
|
|
15
|
17
|
@RestController
|
|
|
@@ -17,17 +19,19 @@ public class PollController {
|
|
17
|
19
|
@Inject
|
|
18
|
20
|
PollRepository pollRepository;
|
|
19
|
21
|
|
|
20
|
|
- public PollController(){
|
|
|
22
|
+ @Autowired
|
|
|
23
|
+ public PollController() {
|
|
21
|
24
|
this.pollRepository = pollRepository;
|
|
22
|
25
|
}
|
|
23
|
26
|
|
|
24
|
|
- @RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
|
27
|
+ @RequestMapping(value = "/polls", method = RequestMethod.GET)
|
|
25
|
28
|
public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
26
|
29
|
Iterable<Poll> allPolls = pollRepository.findAll(); // reads all the polls using the poll repository
|
|
27
|
30
|
return new ResponseEntity<>(allPolls, HttpStatus.OK); // poll data becomes part of the response body & responds as OK (200)
|
|
28
|
31
|
}
|
|
29
|
|
- @RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
30
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll) { // ensures client has some way of knowing the URI of the newly created poll
|
|
|
32
|
+
|
|
|
33
|
+ @RequestMapping(value = "/polls", method = RequestMethod.POST)
|
|
|
34
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) { // ensures client has some way of knowing the URI of the newly created poll
|
|
31
|
35
|
poll = (Poll) pollRepository.save(poll);
|
|
32
|
36
|
URI newPollUri = ServletUriComponentsBuilder
|
|
33
|
37
|
.fromCurrentRequest()
|
|
|
@@ -38,26 +42,30 @@ public class PollController {
|
|
38
|
42
|
httpHeaders.setLocation(newPollUri);
|
|
39
|
43
|
return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
|
|
40
|
44
|
}
|
|
41
|
|
- @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET) // enables us to access an individual poll
|
|
|
45
|
+
|
|
|
46
|
+ @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET) // enables us to access an individual poll
|
|
42
|
47
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
43
|
48
|
Poll p = pollRepository.findOne(pollId);
|
|
44
|
|
- return new ResponseEntity<> (p, HttpStatus.OK);
|
|
|
49
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
|
45
|
50
|
}
|
|
46
|
|
- @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
47
|
|
- public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) { // enables us to update a poll
|
|
|
51
|
+
|
|
|
52
|
+ @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
|
|
|
53
|
+ public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) { // enables us to update a poll
|
|
48
|
54
|
// Save the entity
|
|
49
|
55
|
Poll p = pollRepository.save(poll);
|
|
50
|
56
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
51
|
57
|
}
|
|
52
|
|
- @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE) // enables us to delete a poll
|
|
|
58
|
+
|
|
|
59
|
+ @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE) // enables us to delete a poll
|
|
53
|
60
|
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
54
|
61
|
pollRepository.delete(pollId);
|
|
55
|
62
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
56
|
63
|
}
|
|
57
|
64
|
|
|
58
|
|
- public void verifyPoll() throws ResourceNotFoundException{
|
|
59
|
|
- Poll poll = pollRepository.findOne(pollId);
|
|
60
|
|
- if(poll == null){
|
|
|
65
|
+ public void verifyPoll(Long pollId) throws ResourceNotFoundException {
|
|
|
66
|
+ Poll p = pollRepository.findOne(pollId);
|
|
|
67
|
+ if (p == null) {
|
|
61
|
68
|
throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
|
|
|
69
|
+ }
|
|
62
|
70
|
}
|
|
63
|
|
-}}
|
|
|
71
|
+}
|