Patrick Glavin 8 лет назад
Родитель
Сommit
58ebc2e570

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

@@ -33,7 +33,7 @@ public class PollController {
33 33
         HttpHeaders responseHeaders = new HttpHeaders();
34 34
         responseHeaders.setLocation(newPollUri);
35 35
         poll = pollRepository.save(poll);
36
-        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
36
+        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
37 37
     }
38 38
 
39 39
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)

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

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

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

@@ -1,7 +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
+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> findById(Long pollId);
7 13
 }