mpierse 6 lat temu
rodzic
commit
f1a87fb58a

+ 1
- 1
README.md Wyświetl plik

@@ -358,7 +358,7 @@ public Iterable<Vote> getVote(@PathVariable Long pollId) {
358 358
 
359 359
 * The final piece remaining for us is the implementation of the ComputeResult resource.
360 360
 * Because we don’t have any domain objects that can directly help generate this resource representation, we implement two Data Transfer Objects or DTOs—OptionCount and VoteResult
361
-* Create a sub package of `java` named `dtos`
361
+* Create a sub package of `java` named `io.zipcoder.tc_spring_poll_application.dtos`
362 362
 
363 363
 
364 364
 ## Part 4.1 - Create class `OptionCount`

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Wyświetl plik

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpHeaders;
@@ -41,6 +42,7 @@ public class PollController {
41 42
 
42 43
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
43 44
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
45
+        verifyPoll(pollId);
44 46
         Poll p = pollRepository.findOne(pollId);
45 47
         return new ResponseEntity<> (p, HttpStatus.OK);
46 48
     }
@@ -48,13 +50,19 @@ public class PollController {
48 50
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49 51
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50 52
         // Save the entity
53
+        verifyPoll(pollId);
51 54
         Poll p = pollRepository.save(poll);
52 55
         return new ResponseEntity<>(HttpStatus.OK);
53 56
     }
54 57
 
55 58
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56 59
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
60
+        verifyPoll(pollId);
57 61
         pollRepository.delete(pollId);
58 62
         return new ResponseEntity<>(HttpStatus.OK);
59 63
     }
64
+
65
+    public void verifyPoll(Long id){
66
+        if(!pollRepository.exists(id)) throw ResourceNotFoundException;
67
+    }
60 68
 }

+ 1
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Wyświetl plik

@@ -3,8 +3,6 @@ package io.zipcoder.tc_spring_poll_application.controller;
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4 4
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6
-import org.springframework.data.jpa.repository.Query;
7
-import org.springframework.data.repository.CrudRepository;
8 6
 import org.springframework.http.HttpHeaders;
9 7
 import org.springframework.http.HttpStatus;
10 8
 import org.springframework.http.ResponseEntity;
@@ -43,5 +41,5 @@ public class VoteController {
43 41
     public Iterable<Vote> getVote(@PathVariable Long pollId) {
44 42
         return voteRepository.findVotesByPoll(pollId);
45 43
     }
46
-    
44
+
47 45
 }

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ComputeResultController.java Wyświetl plik

@@ -0,0 +1,44 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
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.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
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
14
+
15
+import java.net.URI;
16
+import java.util.Spliterator;
17
+
18
+@RestController
19
+public class ComputeResultController {
20
+
21
+    private VoteRepository voteRepository;
22
+
23
+    @Autowired
24
+    public ComputeResultController(VoteRepository voteRepository) {
25
+        this.voteRepository = voteRepository;
26
+    }
27
+
28
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
29
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
30
+        VoteResult voteResult = new VoteResult();
31
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
32
+        Spliterator split = allVotes.spliterator();
33
+        voteResult.setTotalVotes((int)split.getExactSizeIfKnown());
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{computeresult}")
37
+                .buildAndExpand(voteResult)
38
+                .toUri();
39
+        HttpHeaders header = new HttpHeaders();
40
+        header.setLocation(newPollUri);
41
+        //TODO: Implement algorithm to count votes
42
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
43
+    }
44
+}

src/main/java/dtos/OptionCount.java → src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Wyświetl plik

@@ -1,4 +1,4 @@
1
-package dtos;
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2 2
 
3 3
 public class OptionCount {
4 4
 

src/main/java/dtos/VoteResult.java → src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Wyświetl plik

@@ -1,4 +1,4 @@
1
-package dtos;
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2 2
 
3 3
 import java.util.Collection;
4 4
 

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Wyświetl plik

@@ -0,0 +1,20 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+@ResponseStatus(HttpStatus.NOT_FOUND)
7
+public class ResourceNotFoundException extends RuntimeException {
8
+
9
+    public ResourceNotFoundException(){
10
+        super();
11
+    }
12
+
13
+    public ResourceNotFoundException(String message) {
14
+        super(message);
15
+    }
16
+
17
+    public ResourceNotFoundException(String message, Throwable cause) {
18
+        super(message, cause);
19
+    }
20
+}