Kibret Tecle 8 年之前
父節點
當前提交
32ff83e2a7

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

@@ -32,6 +32,8 @@ public class PollController {
32 32
     @RequestMapping(value = "/polls", method = RequestMethod.POST)
33 33
     public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
34 34
         poll = pollRepository.save(poll);
35
+
36
+        // Set the location header for the newly created resource
35 37
         HttpHeaders responseHeaders = new HttpHeaders();
36 38
         URI newPollUri = ServletUriComponentsBuilder
37 39
                 .fromCurrentRequest()

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

@@ -29,5 +29,15 @@ public class VoteController {
29 29
                 fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
30 30
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
31 31
     }
32
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
33
+    public Iterable<Vote> getAllVotes() {
34
+        return voteRepository.findAll();
35
+    }
36
+
37
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
38
+    public Iterable<Vote> getAllVotes(@PathVariable Long pollId) {
39
+        return voteRepository. findVotesByPoll(pollId);
40
+    }
41
+
32 42
 
33 43
 }

+ 10
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java 查看文件

@@ -1,7 +1,16 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
4 5
 import org.springframework.data.repository.CrudRepository;
5 6
 
6
-public interface VoteRepository extends CrudRepository<Vote,Long>{
7
+public interface VoteRepository extends CrudRepository<Vote, Long> {
8
+    @Query(value = "SELECT v.* " +
9
+            "FROM Option o, Vote v " +
10
+            "WHERE o.POLL_ID = ?1 " +
11
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
+    public Iterable<Vote> findVotesByPoll(Long pollId);
13
+
14
+
7 15
 }
16
+