|
@@ -0,0 +1,44 @@
|
|
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.beans.factory.annotation.Autowired;
|
|
6
|
+import org.springframework.http.HttpHeaders;
|
|
7
|
+import org.springframework.http.HttpStatus;
|
|
8
|
+import org.springframework.http.ResponseEntity;
|
|
9
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
10
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
11
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
12
|
+import org.springframework.web.bind.annotation.RestController;
|
|
13
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
14
|
+
|
|
15
|
+import java.net.URI;
|
|
16
|
+import java.util.Spliterator;
|
|
17
|
+
|
|
18
|
+@RestController
|
|
19
|
+public class ComputeResultController {
|
|
20
|
+
|
|
21
|
+ private VoteRepository voteRepository;
|
|
22
|
+
|
|
23
|
+ @Autowired
|
|
24
|
+ public ComputeResultController(VoteRepository voteRepository) {
|
|
25
|
+ this.voteRepository = voteRepository;
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
|
|
29
|
+ public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
|
|
30
|
+ VoteResult voteResult = new VoteResult();
|
|
31
|
+ Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
|
|
32
|
+ Spliterator split = allVotes.spliterator();
|
|
33
|
+ voteResult.setTotalVotes((int)split.getExactSizeIfKnown());
|
|
34
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
35
|
+ .fromCurrentRequest()
|
|
36
|
+ .path("/{computeresult}")
|
|
37
|
+ .buildAndExpand(voteResult)
|
|
38
|
+ .toUri();
|
|
39
|
+ HttpHeaders header = new HttpHeaders();
|
|
40
|
+ header.setLocation(newPollUri);
|
|
41
|
+ //TODO: Implement algorithm to count votes
|
|
42
|
+ return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
|
|
43
|
+ }
|
|
44
|
+}
|