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