Lauren Green 7 лет назад
Родитель
Сommit
ebf49c91af

+ 25
- 17
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Просмотреть файл

1
 package io.zipcoder.tc_spring_poll_application.controller;
1
 package io.zipcoder.tc_spring_poll_application.controller;
2
 
2
 
3
 import io.zipcoder.tc_spring_poll_application.domain.Option;
3
 import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
 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.OptionCount;
6
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
7
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
13
 import org.springframework.web.bind.annotation.RequestParam;
14
 import org.springframework.web.bind.annotation.RequestParam;
14
 import org.springframework.web.bind.annotation.RestController;
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
 @RestController
19
 @RestController
21
 public class ComputeResultController {
20
 public class ComputeResultController {
31
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
30
     public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
32
         VoteResult voteResult = new VoteResult();
31
         VoteResult voteResult = new VoteResult();
33
         Iterable<Vote> allVotes = voteRepository.findById(pollId);
32
         Iterable<Vote> allVotes = voteRepository.findById(pollId);
34
-        Iterator it = allVotes.iterator();
33
+        Map<Long, Integer> mapOptions = new HashMap<>();
35
         Collection<OptionCount> optionCounts = new ArrayList<>();
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
     private Long optionId;
4
     private Long optionId;
5
     private int count;
5
     private int count;
6
 
6
 
7
+    public OptionCount(Long optionId, int count) {
8
+        this.optionId = optionId;
9
+        this.count = count;
10
+    }
11
+
7
     public Long getOptionId() {
12
     public Long getOptionId() {
8
         return optionId;
13
         return optionId;
9
     }
14
     }