William Brown 6 anos atrás
pai
commit
9e768bf524

+ 8
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver arquivo

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.excepection.ResourceNotFoundException;
4
 import io.zipcoder.tc_spring_poll_application.repository.PollRepository;
5
 import io.zipcoder.tc_spring_poll_application.repository.PollRepository;
5
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
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
     }
47
 
49
 
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
+        verifyPoll(pollId);
51
         Poll p = pollRepository.save(poll);
53
         Poll p = pollRepository.save(poll);
52
         return new ResponseEntity<>(HttpStatus.OK);
54
         return new ResponseEntity<>(HttpStatus.OK);
53
     }
55
     }
54
 
56
 
55
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
57
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
59
+        verifyPoll(pollId);
57
         pollRepository.delete(pollId);
60
         pollRepository.delete(pollId);
58
         return new ResponseEntity<>(HttpStatus.OK);
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 Ver arquivo

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 Ver arquivo

1
 package io.zipcoder.tc_spring_poll_application.excepection;
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 Ver arquivo

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