vvmk 8 лет назад
Родитель
Сommit
032eeeb347

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

@@ -0,0 +1,49 @@
1
+package io.zipcoder.tc_spring_poll_application.controllers;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
6
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.RequestMapping;
11
+import org.springframework.web.bind.annotation.RequestMethod;
12
+import org.springframework.web.bind.annotation.RequestParam;
13
+import org.springframework.web.bind.annotation.RestController;
14
+
15
+import java.util.Iterator;
16
+
17
+/**
18
+ * project: spring-demo
19
+ * package: io.zipcoder.tc_spring_poll_application.controllers
20
+ * author: https://github.com/vvmk
21
+ * date: 4/6/18
22
+ */
23
+@RestController
24
+public class ComputeResultController {
25
+
26
+    private VoteRepository voteRepository;
27
+
28
+    @Autowired
29
+    public ComputeResultController(VoteRepository voteRepository) {
30
+        this.voteRepository = voteRepository;
31
+    }
32
+
33
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
34
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
35
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
36
+        VoteResult voteResult = computeResults(allVotes);
37
+
38
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
39
+    }
40
+
41
+    protected VoteResult computeResults(Iterable<Vote> votes) {
42
+        Iterator<Vote> voterator = votes.iterator();
43
+        VoteResult result = new VoteResult();
44
+        while(voterator.hasNext()) {
45
+            Vote v = voterator.next();
46
+        }
47
+        return result;
48
+    }
49
+}

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

@@ -10,6 +10,11 @@ public class OptionCount {
10 10
     private Long optionId;
11 11
     private int count;
12 12
 
13
+    public OptionCount(Long optionId) {
14
+        this.optionId = optionId;
15
+        count = 0;
16
+    }
17
+
13 18
     public Long getOptionId() {
14 19
         return optionId;
15 20
     }

+ 52
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/utils/ResultCalculator.java Просмотреть файл

@@ -0,0 +1,52 @@
1
+package io.zipcoder.tc_spring_poll_application.utils;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
5
+
6
+import java.util.AbstractCollection;
7
+import java.util.HashMap;
8
+import java.util.Iterator;
9
+import java.util.Optional;
10
+
11
+/**
12
+ * project: spring-demo
13
+ * package: io.zipcoder.tc_spring_poll_application.utils
14
+ * author: https://github.com/vvmk
15
+ * date: 4/6/18
16
+ */
17
+public class ResultCalculator extends AbstractCollection<OptionCount> {
18
+
19
+    private HashMap<Long, OptionCount> optionCounts;
20
+    private int size;
21
+
22
+    public ResultCalculator() {
23
+        optionCounts = new HashMap<>();
24
+    }
25
+
26
+    public void add(Option option) {
27
+        size++;
28
+        Long oId = option.getId();
29
+
30
+        Optional<OptionCount> check = Optional.ofNullable(getOptionCountById(oId));
31
+        OptionCount workingCount = check.orElse(new OptionCount(oId));
32
+
33
+        //an exercise in restraint to maintain the integrity of OptionCount as a pure DTO
34
+        workingCount.setCount(workingCount.getCount() + 1);
35
+
36
+        optionCounts.put(oId, workingCount);
37
+    }
38
+
39
+    public OptionCount getOptionCountById(Long id) {
40
+        return optionCounts.get(id);
41
+    }
42
+
43
+    @Override
44
+    public Iterator<OptionCount> iterator() {
45
+        return optionCounts.values().iterator();
46
+    }
47
+
48
+    @Override
49
+    public int size() {
50
+        return size;
51
+    }
52
+}

+ 70
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/utils/ResultCalculatorTest.java Просмотреть файл

@@ -0,0 +1,70 @@
1
+package io.zipcoder.tc_spring_poll_application.utils;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import static junit.framework.TestCase.assertEquals;
8
+
9
+/**
10
+ * project: spring-demo
11
+ * package: io.zipcoder.tc_spring_poll_application.utils
12
+ * author: https://github.com/vvmk
13
+ * date: 4/6/18
14
+ */
15
+public class ResultCalculatorTest {
16
+
17
+    private ResultCalculator rc = new ResultCalculator();
18
+    private Option o1 = new Option();
19
+    private Option o2 = new Option();
20
+
21
+    @Before
22
+    public void setup() {
23
+        o1.setId(1L);
24
+        o1.setValue("Kirk");
25
+
26
+        o2.setId(2L);
27
+        o2.setValue("Picard");
28
+    }
29
+
30
+    @Test
31
+    public void addShouldCountOptions() {
32
+        int expected = rc.size() + 2;
33
+
34
+        rc.add(o1);
35
+        rc.add(o2);
36
+
37
+        int actual = rc.size();
38
+
39
+        assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void addShouldCountDuplicateVotes() {
44
+        int expected = rc.size() + 3;
45
+
46
+        rc.add(o1);
47
+        rc.add(o2);
48
+        rc.add(o1);
49
+
50
+        int actual = rc.size();
51
+
52
+        assertEquals(expected, actual);
53
+    }
54
+
55
+    @Test
56
+    public void addShouldTallyVotes() {
57
+        int expected = 3;
58
+
59
+        rc.add(o1);
60
+        rc.add(o1);
61
+        rc.add(o1);
62
+
63
+        rc.add(o2);
64
+        rc.add(o2);
65
+
66
+        int actual = rc.getOptionCountById(o1.getId()).getCount();
67
+
68
+        assertEquals(expected, actual);
69
+    }
70
+}