|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.domain.Vote;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.dto.OptionCount;
|
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.dto.VoteResult;
|
|
5
|
6
|
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
6
|
7
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -8,6 +9,8 @@ import org.springframework.http.ResponseEntity;
|
|
8
|
9
|
import org.springframework.web.bind.annotation.*;
|
|
9
|
10
|
|
|
10
|
11
|
import javax.inject.Inject;
|
|
|
12
|
+import java.util.HashMap;
|
|
|
13
|
+import java.util.Map;
|
|
11
|
14
|
|
|
12
|
15
|
@RestController
|
|
13
|
16
|
public class ComputeResultController {
|
|
|
@@ -20,7 +23,22 @@ public class ComputeResultController {
|
|
20
|
23
|
VoteResult voteResult = new VoteResult();
|
|
21
|
24
|
Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
|
|
22
|
25
|
|
|
23
|
|
- return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
|
|
|
26
|
+ int totalVotes = 0;
|
|
|
27
|
+ Map<Long, OptionCount> tempMap = new HashMap<>();
|
|
|
28
|
+ for(Vote v : allVotes) {
|
|
|
29
|
+ totalVotes++;
|
|
|
30
|
+ OptionCount optionCount = tempMap.get(v.getOption().getId());
|
|
|
31
|
+ if(optionCount == null) {
|
|
|
32
|
+ optionCount = new OptionCount();
|
|
|
33
|
+ optionCount.setOptionId(v.getOption().getId());
|
|
|
34
|
+ tempMap.put(v.getOption().getId(), optionCount);
|
|
|
35
|
+ }
|
|
|
36
|
+ optionCount.setCount(optionCount.getCount() + 1);
|
|
|
37
|
+ }
|
|
|
38
|
+ voteResult.setTotalVotes(totalVotes);
|
|
|
39
|
+ voteResult.setResults(tempMap.values());
|
|
|
40
|
+
|
|
|
41
|
+ return new ResponseEntity<>(voteResult, HttpStatus.OK);
|
|
24
|
42
|
}
|
|
25
|
43
|
|
|
26
|
44
|
}
|