|
|
@@ -1,6 +1,8 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.domain.Option;
|
|
3
|
4
|
import io.zipcoder.tc_spring_poll_application.domain.Vote;
|
|
|
5
|
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
|
|
4
|
6
|
import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
|
|
5
|
7
|
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
6
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -11,6 +13,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|
11
|
13
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
12
|
14
|
import org.springframework.web.bind.annotation.RestController;
|
|
13
|
15
|
|
|
|
16
|
+import java.util.Collection;
|
|
|
17
|
+import java.util.HashMap;
|
|
|
18
|
+import java.util.HashSet;
|
|
|
19
|
+
|
|
14
|
20
|
@RestController
|
|
15
|
21
|
public class ComputeResultController {
|
|
16
|
22
|
|
|
|
@@ -26,8 +32,32 @@ public class ComputeResultController {
|
|
26
|
32
|
VoteResult voteResult = new VoteResult();
|
|
27
|
33
|
Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
|
|
28
|
34
|
|
|
|
35
|
+ // for each vote in all votes, get the option ID and set to Option ID of OptionCount
|
|
|
36
|
+ // increase the optioncount count field accordingly for each vote
|
|
|
37
|
+
|
|
|
38
|
+ // create Collection Option Counts
|
|
|
39
|
+ int voteCount = 0;
|
|
|
40
|
+ HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
|
|
|
41
|
+
|
|
|
42
|
+ for (Vote v: allVotes
|
|
|
43
|
+ ) {
|
|
|
44
|
+ voteCount++;
|
|
|
45
|
+ Long name = (v.getOption().getId());
|
|
|
46
|
+ if (!optionCountsMap.keySet().contains(name)) {
|
|
|
47
|
+ optionCountsMap.put(name, new OptionCount());
|
|
|
48
|
+ } else {
|
|
|
49
|
+ OptionCount temp = optionCountsMap.get(name);
|
|
|
50
|
+ temp.setCount(temp.getCount()+1);
|
|
|
51
|
+ optionCountsMap.replace(name, temp);
|
|
|
52
|
+ }
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ voteResult.setTotalVotes(voteCount);
|
|
|
56
|
+
|
|
|
57
|
+ voteResult.setResults(optionCountsMap.values());
|
|
|
58
|
+
|
|
29
|
59
|
//TODO: Implement algorithm to count votes
|
|
30
|
|
- return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
|
|
|
60
|
+ return new ResponseEntity<>(voteResult, HttpStatus.OK);
|
|
31
|
61
|
|
|
32
|
62
|
}
|
|
33
|
63
|
}
|