Trinh Tong 7 anos atrás
pai
commit
c0dd19bc5f

+ 12
- 0
pom.xml Ver arquivo

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>spring-demo</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <parent>
11 23
         <groupId>org.springframework.boot</groupId>
12 24
         <artifactId>spring-boot-starter-parent</artifactId>

+ 31
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Ver arquivo

@@ -1,6 +1,8 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
3 4
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
4 6
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5 7
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6 8
 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,6 +13,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
11 13
 import org.springframework.web.bind.annotation.RequestParam;
12 14
 import org.springframework.web.bind.annotation.RestController;
13 15
 
16
+import java.util.Collection;
17
+import java.util.HashMap;
18
+import java.util.HashSet;
19
+
14 20
 @RestController
15 21
 public class ComputeResultController {
16 22
 
@@ -26,8 +32,32 @@ public class ComputeResultController {
26 32
         VoteResult voteResult = new VoteResult();
27 33
         Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28 34
 
35
+        // for each vote in all votes, get the option ID and set to Option ID of OptionCount
36
+        // increase the optioncount count field accordingly for each vote
37
+
38
+        // create Collection Option Counts
39
+        int voteCount = 0;
40
+        HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
41
+
42
+        for (Vote v: allVotes
43
+             ) {
44
+            voteCount++;
45
+            Long name = (v.getOption().getId());
46
+            if (!optionCountsMap.keySet().contains(name)) {
47
+                optionCountsMap.put(name, new OptionCount());
48
+            } else {
49
+                OptionCount temp = optionCountsMap.get(name);
50
+                temp.setCount(temp.getCount()+1);
51
+                optionCountsMap.replace(name, temp);
52
+            }
53
+        }
54
+
55
+        voteResult.setTotalVotes(voteCount);
56
+
57
+        voteResult.setResults(optionCountsMap.values());
58
+
29 59
         //TODO: Implement algorithm to count votes
30
-        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
60
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
31 61
 
32 62
     }
33 63
 }