Lawrence Wu 8 лет назад
Родитель
Сommit
b5168042de

+ 8
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Просмотреть файл

@@ -25,6 +25,12 @@ public class VoteController {
25 25
                 fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
26 26
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
27 27
     }
28
-
29
-
28
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
29
+    public Iterable<Vote> getAllVotes() {
30
+        return voteRepository.findAll();
31
+    }
32
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
33
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
34
+        return voteRepository.findVotesByPoll(pollId);
35
+    }
30 36
 }

+ 45
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ComputeResultController.java Просмотреть файл

@@ -0,0 +1,45 @@
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.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RequestMethod;
9
+import org.springframework.web.bind.annotation.RequestParam;
10
+import org.springframework.web.bind.annotation.RestController;
11
+
12
+import javax.inject.Inject;
13
+import java.util.HashMap;
14
+import java.util.Map;
15
+
16
+@RestController
17
+public class ComputeResultController {
18
+    @Inject
19
+    private VoteRepository voteRepository;
20
+
21
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
22
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
23
+        VoteResult voteResult = new VoteResult();
24
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
25
+
26
+        int totalVotes = 0;
27
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
28
+        for(Vote v : allVotes) {
29
+            totalVotes ++;
30
+            // Get the OptionCount corresponding to this Option
31
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
32
+            if(optionCount == null) {
33
+                optionCount = new OptionCount();
34
+                optionCount.setOptionId(v.getOption().getId());
35
+                tempMap.put(v.getOption().getId(), optionCount);
36
+            }
37
+            optionCount.setCount(optionCount.getCount()+1);
38
+        }
39
+        voteResult.setTotalVotes(totalVotes);
40
+        voteResult.setResults(tempMap.values());
41
+        //TODO: Implement algorithm to count votes
42
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
43
+    }
44
+
45
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Просмотреть файл

@@ -0,0 +1,22 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+    private Long optionId;
5
+    private int count;
6
+
7
+    public Long getOptionId() {
8
+        return optionId;
9
+    }
10
+
11
+    public void setOptionId(Long optionId) {
12
+        this.optionId = optionId;
13
+    }
14
+
15
+    public int getCount() {
16
+        return count;
17
+    }
18
+
19
+    public void setCount(int count) {
20
+        this.count = count;
21
+    }
22
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Просмотреть файл

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+import java.util.Collection;
3
+
4
+public class VoteResult {
5
+    private int totalVotes;
6
+    private Collection<OptionCount> results;
7
+
8
+    public int getTotalVotes() {
9
+        return totalVotes;
10
+    }
11
+
12
+    public void setTotalVotes(int totalVotes) {
13
+        this.totalVotes = totalVotes;
14
+    }
15
+
16
+    public Collection<OptionCount> getResults() {
17
+        return results;
18
+    }
19
+
20
+    public void setResults(Collection<OptionCount> results) {
21
+        this.results = results;
22
+    }
23
+}