Lauren Green пре 5 година
родитељ
комит
ebf49c91af

+ 25
- 17
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Прегледај датотеку

@@ -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.Option;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 5
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5 6
 import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
6 7
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
@@ -13,9 +14,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
13 14
 import org.springframework.web.bind.annotation.RequestParam;
14 15
 import org.springframework.web.bind.annotation.RestController;
15 16
 
16
-import java.util.ArrayList;
17
-import java.util.Collection;
18
-import java.util.Iterator;
17
+import java.util.*;
19 18
 
20 19
 @RestController
21 20
 public class ComputeResultController {
@@ -31,21 +30,30 @@ public class ComputeResultController {
31 30
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
32 31
         VoteResult voteResult = new VoteResult();
33 32
         Iterable<Vote> allVotes = voteRepository.findById(pollId);
34
-        Iterator it = allVotes.iterator();
33
+        Map<Long, Integer> mapOptions = new HashMap<>();
35 34
         Collection<OptionCount> optionCounts = new ArrayList<>();
35
+        int totalVotes = 0;
36 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);
37
+        for(Vote vote: allVotes) {
38
+            Long id = vote.getOption().getId();
39
+            totalVotes++;
40
+            if(!mapOptions.containsKey(id)) {
41
+                mapOptions.put(id, 1);
42
+            } else {
43
+                int count = mapOptions.get(id);
44
+                count = count + 1;
45
+                mapOptions.put(id, count);
46
+            }
47
+        }
48
+
49
+        for(Long i: mapOptions.keySet()) {
50
+            OptionCount optionCount = new OptionCount(i, mapOptions.get(i));
51
+            optionCounts.add(optionCount);
52
+        }
53
+
54
+        voteResult.setResults(optionCounts);
55
+        voteResult.setTotalVotes(totalVotes);
56
+
57
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
50 58
     }
51 59
 }

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Прегледај датотеку

@@ -4,6 +4,11 @@ public class OptionCount {
4 4
     private Long optionId;
5 5
     private int count;
6 6
 
7
+    public OptionCount(Long optionId, int count) {
8
+        this.optionId = optionId;
9
+        this.count = count;
10
+    }
11
+
7 12
     public Long getOptionId() {
8 13
         return optionId;
9 14
     }