Sfoglia il codice sorgente

Working on computeresult

Lauren Green 5 anni fa
parent
commit
fefb46ad23

+ 51
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Vedi File

@@ -0,0 +1,51 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
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.OptionCount;
6
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
7
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.http.HttpStatus;
10
+import org.springframework.http.ResponseEntity;
11
+import org.springframework.web.bind.annotation.RequestMapping;
12
+import org.springframework.web.bind.annotation.RequestMethod;
13
+import org.springframework.web.bind.annotation.RequestParam;
14
+import org.springframework.web.bind.annotation.RestController;
15
+
16
+import java.util.ArrayList;
17
+import java.util.Collection;
18
+import java.util.Iterator;
19
+
20
+@RestController
21
+public class ComputeResultController {
22
+
23
+    private VoteRepository voteRepository;
24
+
25
+    @Autowired
26
+    public ComputeResultController(VoteRepository voteRepository) {
27
+        this.voteRepository = voteRepository;
28
+    }
29
+
30
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
31
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
32
+        VoteResult voteResult = new VoteResult();
33
+        Iterable<Vote> allVotes = voteRepository.findById(pollId);
34
+        Iterator it = allVotes.iterator();
35
+        Collection<OptionCount> optionCounts = new ArrayList<>();
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);
50
+    }
51
+}

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Vedi File

@@ -28,7 +28,7 @@ public class PollController {
28 28
     }
29 29
 
30 30
     @RequestMapping(value="/polls", method=RequestMethod.POST)
31
-    public ResponseEntity<HttpHeaders> createPoll(@RequestBody Poll poll) {
31
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
32 32
         poll = pollRepository.save(poll);
33 33
         URI newPollUri = ServletUriComponentsBuilder
34 34
                 .fromCurrentRequest()
@@ -37,7 +37,7 @@ public class PollController {
37 37
                 .toUri();
38 38
         HttpHeaders loc = new HttpHeaders();
39 39
         loc.setLocation(newPollUri);
40
-        return new ResponseEntity<>(loc, HttpStatus.CREATED);
40
+        return new ResponseEntity<>(null, loc, HttpStatus.CREATED);
41 41
     }
42 42
 
43 43
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)

+ 42
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Vedi File

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

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Vedi File

@@ -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
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Vedi File

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Vedi File

@@ -1,7 +1,14 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
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> {
8
+    @Query(value = "SELECT v.* " +
9
+            "FROM Option o, Vote v " +
10
+            "WHERE o.POLL_ID = ?1 " +
11
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
+    Iterable<Vote> findById(Long pollId);
13
+
7 14
 }