瀏覽代碼

Finished up Step 4

PeterMcCormick 8 年之前
父節點
當前提交
c140e4c8ad

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

2
 
2
 
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
4
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.http.HttpHeaders;
6
 import org.springframework.http.HttpHeaders;
6
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
12
 
13
 
13
 @RestController
14
 @RestController
14
 public class VoteController {
15
 public class VoteController {
15
-    @Inject
16
+    @Autowired
16
     private VoteRepository voteRepository;
17
     private VoteRepository voteRepository;
17
 
18
 
18
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
19
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)

+ 24
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ComputeResultController.java 查看文件

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