Carolynn Vansant 8 лет назад
Родитель
Сommit
4032df83dc

+ 6
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

@@ -4,7 +4,7 @@ import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
4 4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
5 5
 
6 6
 
7
-
7
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
8 8
 import org.springframework.beans.factory.annotation.Autowired;
9 9
 import org.springframework.http.HttpHeaders;
10 10
 import org.springframework.http.HttpStatus;
@@ -18,9 +18,13 @@ import java.net.URI;
18 18
 @RestController
19 19
 public class PollController {
20 20
 
21
-    @Autowired
22 21
     private PollRepository pollRepository;
23 22
 
23
+    @Autowired
24
+    public PollController(PollRepository pollRepository) {
25
+        this.pollRepository = pollRepository;
26
+    }
27
+
24 28
     @RequestMapping(value="/polls", method= RequestMethod.GET)
25 29
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
26 30
         Iterable<Poll> allPolls = pollRepository.findAll();

+ 18
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Просмотреть файл

@@ -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
 }

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Просмотреть файл

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