|
@@ -0,0 +1,51 @@
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
+
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.domain.Option;
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
|
|
5
|
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
|
|
6
|
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
|
|
7
|
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
8
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
9
|
+import org.springframework.http.HttpStatus;
|
|
10
|
+import org.springframework.http.ResponseEntity;
|
|
11
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
12
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
13
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
14
|
+import org.springframework.web.bind.annotation.RestController;
|
|
15
|
+
|
|
16
|
+import java.util.ArrayList;
|
|
17
|
+import java.util.Collection;
|
|
18
|
+import java.util.Iterator;
|
|
19
|
+
|
|
20
|
+@RestController
|
|
21
|
+public class ComputeResultController {
|
|
22
|
+
|
|
23
|
+ private VoteRepository voteRepository;
|
|
24
|
+
|
|
25
|
+ @Autowired
|
|
26
|
+ public ComputeResultController(VoteRepository voteRepository) {
|
|
27
|
+ this.voteRepository = voteRepository;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
|
|
31
|
+ public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
|
|
32
|
+ VoteResult voteResult = new VoteResult();
|
|
33
|
+ Iterable<Vote> allVotes = voteRepository.findById(pollId);
|
|
34
|
+ Iterator it = allVotes.iterator();
|
|
35
|
+ Collection<OptionCount> optionCounts = new ArrayList<>();
|
|
36
|
+
|
|
37
|
+// while(it.hasNext()) {
|
|
38
|
+// Vote vote = (Vote) it.next();
|
|
39
|
+// Option option = vote.getOption();
|
|
40
|
+// OptionCount optionCount = new OptionCount();
|
|
41
|
+// try {
|
|
42
|
+// ((ArrayList<OptionCount>) optionCounts).get(optionCount)
|
|
43
|
+// optionCount.setOptionId(option.getId());
|
|
44
|
+// optionCount.setCount(1);
|
|
45
|
+// ((ArrayList<OptionCount>) optionCounts).add(optionCount);
|
|
46
|
+// }
|
|
47
|
+// voteResult.setResults(optionCounts);
|
|
48
|
+// }
|
|
49
|
+ return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
|
|
50
|
+ }
|
|
51
|
+}
|