Преглед изворни кода

Vote controller methods added

Mitch Taylor пре 8 година
родитељ
комит
11103afeaa

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Прегледај датотеку

@@ -27,4 +27,14 @@ public class VoteController {
27 27
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
28 28
     }
29 29
 
30
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
31
+    public Iterable<Vote> getAllVotes() {
32
+        return voteRepository.findAll();
33
+    }
34
+
35
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
36
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
37
+        return voteRepository.findById(pollId);
38
+    }
39
+
30 40
 }

+ 7
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Прегледај датотеку

@@ -1,10 +1,13 @@
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
-
8
-
9
-
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);
10 13
 }