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