|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.controllers;
|
|
|
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.VoteResult;
|
|
|
6
|
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
|
7
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
8
|
+import org.springframework.http.HttpStatus;
|
|
|
9
|
+import org.springframework.http.ResponseEntity;
|
|
|
10
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
11
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
12
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
13
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
14
|
+
|
|
|
15
|
+import java.util.Iterator;
|
|
|
16
|
+
|
|
|
17
|
+/**
|
|
|
18
|
+ * project: spring-demo
|
|
|
19
|
+ * package: io.zipcoder.tc_spring_poll_application.controllers
|
|
|
20
|
+ * author: https://github.com/vvmk
|
|
|
21
|
+ * date: 4/6/18
|
|
|
22
|
+ */
|
|
|
23
|
+@RestController
|
|
|
24
|
+public class ComputeResultController {
|
|
|
25
|
+
|
|
|
26
|
+ private VoteRepository voteRepository;
|
|
|
27
|
+
|
|
|
28
|
+ @Autowired
|
|
|
29
|
+ public ComputeResultController(VoteRepository voteRepository) {
|
|
|
30
|
+ this.voteRepository = voteRepository;
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
|
|
|
34
|
+ public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
|
|
|
35
|
+ Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
|
|
|
36
|
+ VoteResult voteResult = computeResults(allVotes);
|
|
|
37
|
+
|
|
|
38
|
+ return new ResponseEntity<>(voteResult, HttpStatus.OK);
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ protected VoteResult computeResults(Iterable<Vote> votes) {
|
|
|
42
|
+ Iterator<Vote> voterator = votes.iterator();
|
|
|
43
|
+ VoteResult result = new VoteResult();
|
|
|
44
|
+ while(voterator.hasNext()) {
|
|
|
45
|
+ Vote v = voterator.next();
|
|
|
46
|
+ }
|
|
|
47
|
+ return result;
|
|
|
48
|
+ }
|
|
|
49
|
+}
|