William Brown 5 лет назад
Родитель
Сommit
78c11cf854

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

@@ -0,0 +1,32 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5
+import io.zipcoder.tc_spring_poll_application.repository.VoteRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RequestParam;
12
+import org.springframework.web.bind.annotation.RestController;
13
+
14
+@RestController
15
+public class ComputeResultController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public ComputeResultController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
25
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
26
+        VoteResult voteResult = new VoteResult();
27
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28
+
29
+        //TODO: Implement algorithm to count votes
30
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
31
+    }
32
+}

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

@@ -1,4 +1,45 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repository.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.data.jpa.repository.Query;
7
+import org.springframework.data.repository.CrudRepository;
8
+import org.springframework.http.HttpHeaders;
9
+import org.springframework.http.HttpStatus;
10
+import org.springframework.http.ResponseEntity;
11
+import org.springframework.web.bind.annotation.*;
12
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
13
+
14
+@RestController
3 15
 public class VoteController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public VoteController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
25
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote vote) {
26
+        vote = voteRepository.save(vote);
27
+        // Set the headers for the newly created resource
28
+        HttpHeaders responseHeaders = new HttpHeaders();
29
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
30
+                fromCurrentRequest().path("/{id}")
31
+                .buildAndExpand(vote.getId())
32
+                .toUri());
33
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
34
+    }
35
+
36
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
37
+    public Iterable<Vote> getAllVotes() {
38
+        return voteRepository.findAll();
39
+    }
40
+
41
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
42
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
43
+        return voteRepository.findById(pollId);
44
+    }
4 45
 }

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

@@ -0,0 +1,22 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+    private Long optionId;
5
+    private int count;
6
+
7
+    public Long getOptionId() {
8
+        return optionId;
9
+    }
10
+
11
+    public void setOptionId(Long optionId) {
12
+        this.optionId = optionId;
13
+    }
14
+
15
+    public int getCount() {
16
+        return count;
17
+    }
18
+
19
+    public void setCount(int count) {
20
+        this.count = count;
21
+    }
22
+}

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

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+    private int totalVotes;
7
+    private Collection<OptionCount> results;
8
+
9
+    public int getTotalVotes() {
10
+        return totalVotes;
11
+    }
12
+
13
+    public void setTotalVotes(int totalVotes) {
14
+        this.totalVotes = totalVotes;
15
+    }
16
+
17
+    public Collection<OptionCount> getResults() {
18
+        return results;
19
+    }
20
+
21
+    public void setResults(Collection<OptionCount> results) {
22
+        this.results = results;
23
+    }
24
+}

+ 4
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/excepection/ResourceNotFoundException.java Просмотреть файл

@@ -0,0 +1,4 @@
1
+package io.zipcoder.tc_spring_poll_application.excepection;
2
+
3
+public class ResourceNotFoundException {
4
+}

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

@@ -1,10 +1,16 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repository;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
4 5
 import org.springframework.data.repository.CrudRepository;
5 6
 
6 7
 public interface VoteRepository extends CrudRepository<Vote, Long> {
7 8
 
9
+    @Query(value = "SELECT v.* " +
10
+            "FROM Option o, Vote v " +
11
+            "WHERE o.POLL_ID = ?1 " +
12
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
8 14
 
9
-
15
+    Iterable<Vote> findById(Long pollId);
10 16
 }