|
|
@@ -2,6 +2,7 @@ package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.domain.Vote;
|
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
|
5
|
+
|
|
5
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
6
|
7
|
import org.springframework.http.HttpHeaders;
|
|
7
|
8
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -13,17 +14,32 @@ import javax.inject.Inject;
|
|
13
|
14
|
|
|
14
|
15
|
@RestController
|
|
15
|
16
|
public class VoteController {
|
|
16
|
|
- @Autowired
|
|
|
17
|
+
|
|
17
|
18
|
private VoteRepository voteRepository;
|
|
18
|
19
|
|
|
|
20
|
+ @Autowired
|
|
|
21
|
+ public VoteController(VoteRepository voteRepository) {
|
|
|
22
|
+ this.voteRepository = voteRepository;
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
19
|
25
|
@RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
|
|
20
|
26
|
public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
|
|
21
|
27
|
vote) {
|
|
22
|
28
|
vote = voteRepository.save(vote);
|
|
23
|
|
- // Set the headers for the newly created resource
|
|
24
|
29
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
25
|
30
|
responseHeaders.setLocation(ServletUriComponentsBuilder.
|
|
26
|
31
|
fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
|
|
27
|
32
|
return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
|
|
28
|
33
|
}
|
|
|
34
|
+
|
|
|
35
|
+ @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
|
|
|
36
|
+ public Iterable<Vote> getAllVotes() {
|
|
|
37
|
+ return voteRepository.findAll();
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
|
|
|
41
|
+ public Iterable<Vote> getVote(@PathVariable Long pollId) {
|
|
|
42
|
+ return voteRepository.findByPoll(pollId);
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
29
|
45
|
}
|