Kaitrina High 8 лет назад
Родитель
Сommit
6918522d43

+ 15
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

@@ -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.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
@@ -25,7 +26,7 @@ public class PollController {
25 26
     }
26 27
 
27 28
     @RequestMapping(value="/polls", method=RequestMethod.POST)
28
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
29
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
29 30
         poll = pollRepository.save(poll);
30 31
         HttpHeaders httpHeaders = new HttpHeaders();
31 32
         URI newPollUri = ServletUriComponentsBuilder
@@ -39,12 +40,14 @@ public class PollController {
39 40
 
40 41
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
41 42
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
43
+        verifyPoll(pollId);
42 44
         Poll p = pollRepository.findOne(pollId);
43 45
         return new ResponseEntity<> (p, HttpStatus.OK);
44 46
     }
45 47
 
46 48
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
47 49
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50
+        verifyPoll(pollId);
48 51
         // Save the entity
49 52
         Poll p = pollRepository.save(poll);
50 53
         return new ResponseEntity<>(HttpStatus.OK);
@@ -52,11 +55,21 @@ public class PollController {
52 55
 
53 56
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
54 57
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
+        verifyPoll(pollId);
55 59
         pollRepository.delete(pollId);
56 60
         return new ResponseEntity<>(HttpStatus.OK);
57 61
     }
58
-}
59 62
 
63
+    public void verifyPoll(@PathVariable Long pollId){
64
+       if(!pollRepository.exists(pollId)){
65
+           throw new ResourceNotFoundException("Poll with id does not exist");
66
+       }
67
+    }
68
+}
60 69
 
70
+//checks if a specific poll id exists and throws a
71
+//        ResourceNotFoundException if not. Use this in any
72
+//        method that searches for or updates an existing poll
73
+//        (eg: Get, Put, and Delete methods).
61 74
 
62 75
 

+ 11
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

@@ -1,6 +1,9 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
3 5
 import javax.persistence.*;
6
+import javax.validation.constraints.Size;
4 7
 import java.util.HashSet;
5 8
 import java.util.Set;
6 9
 
@@ -13,11 +16,13 @@ public class Poll {
13 16
     private long id;
14 17
 
15 18
     @Column(name = "QUESTION")
19
+    @NotEmpty
16 20
     private String questions;
17 21
 
18 22
     @OneToMany(cascade = CascadeType.ALL)
19 23
     @JoinColumn(name = "POLL_ID")
20 24
     @OrderBy
25
+    @Size(min=2, max = 6)
21 26
     private Set<Option> options;
22 27
 
23 28
     public long getId() {
@@ -45,3 +50,9 @@ public class Poll {
45 50
     }
46 51
 }
47 52
 
53
+// In the Poll class:
54
+// options should be @Size(min=2, max = 6)
55
+//question should be @NotEmpty
56
+//To enforce these validations, add @Valid annotations to Poll objects in
57
+//RequestMapping-annotated controller methods
58
+// (there should be 2)...only found one check this!!!!!

+ 72
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Просмотреть файл

@@ -0,0 +1,72 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.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
+    /**
12
+     *
13
+     * @return a brief title of the error condition, eg: "Validation Failure" or "Internal Server Error"
14
+     */
15
+    public String getTitle() {
16
+        return title;
17
+    }
18
+
19
+    public void setTitle(String title) {
20
+        this.title = title;
21
+    }
22
+
23
+    /**
24
+     *
25
+     * @return the HTTP status code for the current request; redundant but useful for client-side error handling
26
+     */
27
+    public int getStatus() {
28
+        return status;
29
+    }
30
+
31
+
32
+    public void setStatus(int status) {
33
+        this.status = status;
34
+    }
35
+
36
+    /**
37
+     *
38
+     * @return A short, human-readable description of the error that may be presented to a user
39
+     */
40
+    public String getDetail() {
41
+        return detail;
42
+    }
43
+
44
+    public void setDetail(String detail) {
45
+        this.detail = detail;
46
+    }
47
+
48
+    /**
49
+     *
50
+     * @return the time in milliseconds when the error occurred
51
+     */
52
+    public long getTimeStamp() {
53
+        return timeStamp;
54
+    }
55
+
56
+    public void setTimeStamp(long timeStamp) {
57
+        this.timeStamp = timeStamp;
58
+    }
59
+
60
+    /**
61
+     *
62
+     * @return detailed information such as exception class name or a stack trace useful for developers to debug
63
+     */
64
+    public String getDeveloperMessage() {
65
+        return developerMessage;
66
+    }
67
+
68
+    public void setDeveloperMessage(String developerMessage) {
69
+        this.developerMessage = developerMessage;
70
+    }
71
+}
72
+

+ 21
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Просмотреть файл

@@ -0,0 +1,21 @@
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
+    }
11
+
12
+    public ResourceNotFoundException(String message) {
13
+        super(message);
14
+    }
15
+
16
+    public ResourceNotFoundException(String message, Throwable cause) {
17
+        super(message, cause);
18
+    }
19
+
20
+}
21
+

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Просмотреть файл

@@ -0,0 +1,33 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dto.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
+    @ExceptionHandler(ResourceNotFoundException.class)
15
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
16
+        ErrorDetail customError = new ErrorDetail();
17
+        customError.setDetail(rnfe.getMessage());
18
+        customError.setDeveloperMessage(rnfe.getStackTrace().toString());
19
+        customError.setStatus(404);
20
+        customError.setTimeStamp(new Date().getTime());
21
+        customError.setTitle("Resource Not Found");
22
+
23
+        return new ResponseEntity<>(customError, null, HttpStatus.NOT_FOUND);
24
+    }
25
+}
26
+
27
+
28
+//Create RestExceptionHandler class annotated with @ControllerAdvice
29
+//Create a handler method with the header shown below
30
+// Populate an ErrorDetail object in the method, and
31
+//return a ResponseEntity containing the ErrorDetail and an HTTP NOT_FOUND status
32
+//Use java.util's new Date().getTime() for the timestamp
33
+//Provide the detail and developer messages from the ResourceNotFoundException