PeterMcCormick преди 8 години
родител
ревизия
c140e4c8ad

+ 2
- 1
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
+import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
7 8
 import org.springframework.http.ResponseEntity;
@@ -12,7 +13,7 @@ import javax.inject.Inject;
12 13
 
13 14
 @RestController
14 15
 public class VoteController {
15
-    @Inject
16
+    @Autowired
16 17
     private VoteRepository voteRepository;
17 18
 
18 19
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)

+ 24
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ComputeResultController.java Целия файл

@@ -2,6 +2,7 @@ package io.zipcoder.tc_spring_poll_application.dtos;
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
+import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.http.HttpStatus;
6 7
 import org.springframework.http.ResponseEntity;
7 8
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -9,19 +10,40 @@ import org.springframework.web.bind.annotation.RequestMethod;
9 10
 import org.springframework.web.bind.annotation.RequestParam;
10 11
 import org.springframework.web.bind.annotation.RestController;
11 12
 
12
-import javax.inject.Inject;
13
+import java.util.HashMap;
14
+import java.util.Map;
15
+
13 16
 
14 17
 @RestController
15 18
 public class ComputeResultController {
16 19
 
17
-    @Inject
18 20
     private VoteRepository voteRepository;
19 21
 
22
+    @Autowired
23
+    public ComputeResultController(VoteRepository voteRepository) {
24
+        this.voteRepository = voteRepository;
25
+    }
26
+
20 27
     @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
21 28
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
22 29
         VoteResult voteResult = new VoteResult();
23 30
         Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
24 31
 
32
+        int totalVotes = 0;
33
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
34
+        for(Vote v : allVotes) {
35
+            totalVotes ++;
36
+            // Get the OptionCount corresponding to this Option
37
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
38
+            if(optionCount == null) {
39
+                optionCount = new OptionCount();
40
+                optionCount.setOptionId(v.getOption().getId());
41
+                tempMap.put(v.getOption().getId(), optionCount);
42
+            }
43
+            optionCount.setCount(optionCount.getCount()+1);
44
+        }
45
+        voteResult.setTotalVotes(totalVotes);
46
+        voteResult.setResults(tempMap.values());
25 47
         //TODO: Implement algorithm to count votes
26 48
         return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
27 49
     }