Browse Source

exceptions

mpierse 6 years ago
parent
commit
f1a87fb58a

+ 1
- 1
README.md View File

358
 
358
 
359
 * The final piece remaining for us is the implementation of the ComputeResult resource.
359
 * The final piece remaining for us is the implementation of the ComputeResult resource.
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
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
 ## Part 4.1 - Create class `OptionCount`
364
 ## Part 4.1 - Create class `OptionCount`

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

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

3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
4
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
 import org.springframework.beans.factory.annotation.Autowired;
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;
6
 import org.springframework.http.HttpHeaders;
9
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
10
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
43
     public Iterable<Vote> getVote(@PathVariable Long pollId) {
41
     public Iterable<Vote> getVote(@PathVariable Long pollId) {
44
         return voteRepository.findVotesByPoll(pollId);
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 View File

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 View File

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

src/main/java/dtos/VoteResult.java → src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java View File

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

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java View File

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