William Brown 5 vuotta sitten
vanhempi
commit
9e768bf524

+ 8
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Näytä tiedosto

@@ -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.excepection.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repository.PollRepository;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpStatus;
@@ -41,22 +42,28 @@ 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
     }
47 49
 
48 50
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49 51
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50
-        // Save the entity
52
+        verifyPoll(pollId);
51 53
         Poll p = pollRepository.save(poll);
52 54
         return new ResponseEntity<>(HttpStatus.OK);
53 55
     }
54 56
 
55 57
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56 58
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
59
+        verifyPoll(pollId);
57 60
         pollRepository.delete(pollId);
58 61
         return new ResponseEntity<>(HttpStatus.OK);
59 62
     }
60 63
 
64
+    private void verifyPoll(Long pollId) {
65
+        Poll poll = pollRepository.findOne(pollId);
66
+        if(poll == null) throw new ResourceNotFoundException();
67
+    }
61 68
 
62 69
 }

+ 61
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Näytä tiedosto

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

+ 17
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/excepection/ResourceNotFoundException.java Näytä tiedosto

@@ -1,4 +1,19 @@
1 1
 package io.zipcoder.tc_spring_poll_application.excepection;
2 2
 
3
-public class ResourceNotFoundException {
4
-}
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
+    }
11
+
12
+    public ResourceNotFoundException(String message) {
13
+        super(message);
14
+    }
15
+
16
+    public ResourceNotFoundException(String message, Throwable cause) {
17
+        super(message, cause);
18
+    }
19
+}

+ 26
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/excepection/RestExceptionHandler.java Näytä tiedosto

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