Seth 5 gadus atpakaļ
vecāks
revīzija
ed994c7362

+ 1
- 1
README.md Parādīt failu

@@ -332,7 +332,7 @@ public interface VoteRepository extends CrudRepository<Vote, Long> {
332 332
 * At runtime, Spring Data JPA replaces the `?1` placeholder with the passed-in `pollId` parameter value.
333 333
 
334 334
 
335
-### Part 3.2.3 - Modify `VoteController`
335
+### Part 3.2.3 - Modify `VoteCo*_****_*ntroller`
336 336
 
337 337
 * Create a `getAllVotes` method in the `VoteController`
338 338
 

+ 9
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Parādīt failu

@@ -43,6 +43,7 @@ public class PollController {
43 43
 
44 44
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
45 45
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
+        verifyPoll(pollId);
46 47
         Poll p = pollRepository.findOne(pollId);
47 48
         return new ResponseEntity<> (p, HttpStatus.OK);
48 49
     }
@@ -50,17 +51,24 @@ public class PollController {
50 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51 52
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52 53
         // Save the entity
54
+        verifyPoll(pollId);
53 55
         Poll p = pollRepository.save(poll);
54 56
         return new ResponseEntity<>(HttpStatus.OK);
55 57
     }
56 58
 
57 59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
58 60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        verifyPoll(pollId);
59 62
         pollRepository.delete(pollId);
60 63
         return new ResponseEntity<>(HttpStatus.OK);
61 64
     }
62 65
 
63
-    public void verifyPoll(){
66
+   // @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
67
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException{
68
+
69
+        if(pollRepository.findOne(pollId) == null){
70
+            throw new ResourceNotFoundException();
71
+        }
64 72
     }
65 73
 
66 74
 }

+ 60
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java Parādīt failu

@@ -0,0 +1,60 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+public class ErrorDetail {
4
+    private String title;
5
+    private int status;
6
+    private String detail;
7
+    private long timestamp;
8
+    private String developerMessage;
9
+
10
+    public ErrorDetail(String title, int status, String detail, long timestamp, String developerMessage) {
11
+        this.title = title;
12
+        this.status = status;
13
+        this.detail = detail;
14
+        this.timestamp = timestamp;
15
+        this.developerMessage = developerMessage;
16
+    }
17
+
18
+    public ErrorDetail() {
19
+    }
20
+
21
+    public String getTitle() {
22
+        return title;
23
+    }
24
+
25
+    public void setTitle(String title) {
26
+        this.title = title;
27
+    }
28
+
29
+    public int getStatus() {
30
+        return status;
31
+    }
32
+
33
+    public void setStatus(int status) {
34
+        this.status = status;
35
+    }
36
+
37
+    public String getDetail() {
38
+        return detail;
39
+    }
40
+
41
+    public void setDetail(String detail) {
42
+        this.detail = detail;
43
+    }
44
+
45
+    public long getTimestamp() {
46
+        return timestamp;
47
+    }
48
+
49
+    public void setTimestamp(long timestamp) {
50
+        this.timestamp = timestamp;
51
+    }
52
+
53
+    public String getDeveloperMessage() {
54
+        return developerMessage;
55
+    }
56
+
57
+    public void setDeveloperMessage(String developerMessage) {
58
+        this.developerMessage = developerMessage;
59
+    }
60
+}

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/RestExceptionHandler.java Parādīt failu

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.http.ResponseEntity;
5
+import org.springframework.web.bind.annotation.ControllerAdvice;
6
+import org.springframework.web.bind.annotation.ExceptionHandler;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import java.util.Date;
10
+
11
+@ControllerAdvice
12
+public class RestExceptionHandler {
13
+
14
+    @ExceptionHandler(ResourceNotFoundException.class)
15
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
16
+        ErrorDetail errorDetail = new ErrorDetail();
17
+        errorDetail.setTimestamp(new Date().getTime());
18
+        errorDetail.setDeveloperMessage(rnfe.getMessage());
19
+        errorDetail.setDetail(rnfe.getStackTrace().toString());
20
+
21
+
22
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
23
+    }
24
+}