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

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

1
 package io.zipcoder.tc_spring_poll_application.controllers;
1
 package io.zipcoder.tc_spring_poll_application.controllers;
2
 
2
 
3
-import io.zipcoder.tc_spring_poll_application.domain.Option;
4
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
4
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
6
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import io.zipcoder.tc_spring_poll_application.utils.ResultCalculator;
7
 import org.springframework.beans.factory.annotation.Autowired;
7
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.http.HttpStatus;
8
 import org.springframework.http.HttpStatus;
9
 import org.springframework.http.ResponseEntity;
9
 import org.springframework.http.ResponseEntity;
38
         return new ResponseEntity<>(voteResult, HttpStatus.OK);
38
         return new ResponseEntity<>(voteResult, HttpStatus.OK);
39
     }
39
     }
40
 
40
 
41
-    protected VoteResult computeResults(Iterable<Vote> votes) {
41
+    private VoteResult computeResults(Iterable<Vote> votes) {
42
         Iterator<Vote> voterator = votes.iterator();
42
         Iterator<Vote> voterator = votes.iterator();
43
-        VoteResult result = new VoteResult();
44
-        while(voterator.hasNext()) {
43
+
44
+        ResultCalculator rc = new ResultCalculator();
45
+        while (voterator.hasNext()) {
45
             Vote v = voterator.next();
46
             Vote v = voterator.next();
47
+            rc.add(v.getOption());
46
         }
48
         }
47
-        return result;
49
+        return rc.calculate();
48
     }
50
     }
49
 }
51
 }

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

52
         return new ResponseEntity<>(vote, responseHeaders, HttpStatus.CREATED);
52
         return new ResponseEntity<>(vote, responseHeaders, HttpStatus.CREATED);
53
     }
53
     }
54
 
54
 
55
-    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
55
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.GET)
56
     public ResponseEntity<Iterable<Vote>> getVotes() {
56
     public ResponseEntity<Iterable<Vote>> getVotes() {
57
         return new ResponseEntity<>(voteRepository.findAll(), HttpStatus.OK);
57
         return new ResponseEntity<>(voteRepository.findAll(), HttpStatus.OK);
58
     }
58
     }
59
 
59
 
60
-    @RequestMapping(value="/polls/{pollId}/votes/", method=RequestMethod.GET)
60
+    @RequestMapping(value = "/polls/{pollId}/votes/", method = RequestMethod.GET)
61
     public ResponseEntity<Iterable<Vote>> getVotesForPoll(@PathVariable Long pollId) {
61
     public ResponseEntity<Iterable<Vote>> getVotesForPoll(@PathVariable Long pollId) {
62
         return new ResponseEntity<>(voteRepository.findVotesByPoll(pollId), HttpStatus.OK);
62
         return new ResponseEntity<>(voteRepository.findVotesByPoll(pollId), HttpStatus.OK);
63
     }
63
     }

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

16
 
16
 
17
     @Id
17
     @Id
18
     @GeneratedValue
18
     @GeneratedValue
19
-    @Column(name="OPTION_ID")
19
+    @Column(name = "OPTION_ID")
20
     private Long Id;
20
     private Long Id;
21
 
21
 
22
-    @Column(name="OPTION_VALUE")
22
+    @Column(name = "OPTION_VALUE")
23
     private String value;
23
     private String value;
24
 
24
 
25
     public Option() {
25
     public Option() {

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

12
     private int totalVotes;
12
     private int totalVotes;
13
     private Collection<OptionCount> results;
13
     private Collection<OptionCount> results;
14
 
14
 
15
+    public VoteResult(int totalVotes, Collection<OptionCount> results) {
16
+        this.totalVotes = totalVotes;
17
+        this.results = results;
18
+    }
19
+
15
     public int getTotalVotes() {
20
     public int getTotalVotes() {
16
         return totalVotes;
21
         return totalVotes;
17
     }
22
     }

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Просмотреть файл

9
  * author: https://github.com/vvmk
9
  * author: https://github.com/vvmk
10
  * date: 4/5/18
10
  * date: 4/5/18
11
  */
11
  */
12
-public interface OptionRepository extends CrudRepository<Option, Long>{
12
+public interface OptionRepository extends CrudRepository<Option, Long> {
13
 }
13
 }

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

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.dtos.OptionCount;
4
 import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
5
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5
 
6
 
6
 import java.util.AbstractCollection;
7
 import java.util.AbstractCollection;
7
 import java.util.HashMap;
8
 import java.util.HashMap;
36
         optionCounts.put(oId, workingCount);
37
         optionCounts.put(oId, workingCount);
37
     }
38
     }
38
 
39
 
40
+    public VoteResult calculate() {
41
+        return new VoteResult(size(), optionCounts.values());
42
+    }
43
+
39
     public OptionCount getOptionCountById(Long id) {
44
     public OptionCount getOptionCountById(Long id) {
40
         return optionCounts.get(id);
45
         return optionCounts.get(id);
41
     }
46
     }

+ 3
- 5
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/VoteControllerTest.java Просмотреть файл

14
 import static junit.framework.TestCase.assertEquals;
14
 import static junit.framework.TestCase.assertEquals;
15
 import static org.mockito.ArgumentMatchers.any;
15
 import static org.mockito.ArgumentMatchers.any;
16
 import static org.mockito.ArgumentMatchers.anyLong;
16
 import static org.mockito.ArgumentMatchers.anyLong;
17
-import static org.mockito.Mockito.verify;
18
-import static org.mockito.Mockito.when;
19
-import static org.mockito.Mockito.mock;
17
+import static org.mockito.Mockito.*;
20
 
18
 
21
 /**
19
 /**
22
  * project: spring-demo
20
  * project: spring-demo
38
 
36
 
39
         //create vote
37
         //create vote
40
         when(voteRepo.save(any(Vote.class))).thenAnswer(inv -> inv.getArgument(0));
38
         when(voteRepo.save(any(Vote.class))).thenAnswer(inv -> inv.getArgument(0));
41
-        when(voteRepo.findAll()).thenReturn((Iterable<Vote>)mock(List.class));
42
-        when(voteRepo.findVotesByPoll(anyLong())).thenReturn((Iterable<Vote>)mock(List.class));
39
+        when(voteRepo.findAll()).thenReturn((Iterable<Vote>) mock(List.class));
40
+        when(voteRepo.findVotesByPoll(anyLong())).thenReturn((Iterable<Vote>) mock(List.class));
43
     }
41
     }
44
 
42
 
45
     @Test
43
     @Test