Trinh Tong 7 anos atrás
pai
commit
4190d682d2

+ 1
- 1
README.md Ver arquivo

358
 
358
 
359
 * The final piece remaining for us is the implementation of the ComputeResult resource.
359
 * The final piece remaining for us is the implementation of the ComputeResult resource.
360
 * Because we don’t have any domain objects that can directly help generate this resource representation, we implement two Data Transfer Objects or DTOs—OptionCount and VoteResult
360
 * Because we don’t have any domain objects that can directly help generate this resource representation, we implement two Data Transfer Objects or DTOs—OptionCount and VoteResult
361
-* Create a sub package of `java` named `dtos`
361
+* Create a sub package of `java` named `io.zipcoder.tc_spring_poll_application.dtos`
362
 
362
 
363
 
363
 
364
 ## Part 4.1 - Create class `OptionCount`
364
 ## Part 4.1 - Create class `OptionCount`

+ 31
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Ver arquivo

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.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RequestParam;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+@RestController
14
+public class ComputeResultController {
15
+
16
+    private VoteRepository voteRepository;
17
+
18
+    @Autowired
19
+    public ComputeResultController(VoteRepository voteRepository) {
20
+        this.voteRepository = voteRepository;
21
+    }
22
+
23
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
24
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
25
+        VoteResult voteResult = new VoteResult();
26
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
27
+
28
+        //TODO: Implement algorithm to count votes
29
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
30
+
31
+}

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Ver arquivo

29
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
29
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
30
     }
30
     }
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> getVote(@PathVariable Long pollId) {
39
+        return voteRepository.findVotesByPoll(pollId);
40
+    }
32
 }
41
 }

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Ver arquivo

1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+    private Long optionId;
5
+    private int count;
6
+
7
+    public Long getOptionId() {
8
+        return optionId;
9
+    }
10
+
11
+    public void setOptionId(Long optionId) {
12
+        this.optionId = optionId;
13
+    }
14
+
15
+    public int getCount() {
16
+        return count;
17
+    }
18
+
19
+    public void setCount(int count) {
20
+        this.count = count;
21
+    }
22
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Ver arquivo

1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+
7
+    private int totalVotes;
8
+    private Collection<OptionCount> results;
9
+
10
+    public int getTotalVotes() {
11
+        return totalVotes;
12
+    }
13
+
14
+    public void setTotalVotes(int totalVotes) {
15
+        this.totalVotes = totalVotes;
16
+    }
17
+
18
+    public Collection<OptionCount> getResults() {
19
+        return results;
20
+    }
21
+
22
+    public void setResults(Collection<OptionCount> results) {
23
+        this.results = results;
24
+    }
25
+}

+ 6
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Ver arquivo

1
 package io.zipcoder.tc_spring_poll_application.repositories;
1
 package io.zipcoder.tc_spring_poll_application.repositories;
2
 
2
 
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
4
 import org.springframework.data.repository.CrudRepository;
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)
7
 
12
 
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
8
 
14
 
9
 }
15
 }