Bladeren bron

part 5.7 finished

William Brown 5 jaren geleden
bovenliggende
commit
bac5f54589

+ 3
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Bestand weergeven

@@ -9,6 +9,7 @@ import org.springframework.http.ResponseEntity;
9 9
 import org.springframework.web.bind.annotation.*;
10 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 11
 
12
+import javax.validation.Valid;
12 13
 import java.net.URI;
13 14
 
14 15
 @RestController
@@ -28,7 +29,7 @@ public class PollController {
28 29
     }
29 30
 
30 31
     @RequestMapping(value="/polls", method=RequestMethod.POST)
31
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
32
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
32 33
         poll = pollRepository.save(poll);
33 34
 
34 35
         URI newPollUri = ServletUriComponentsBuilder
@@ -48,7 +49,7 @@ public class PollController {
48 49
     }
49 50
 
50 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
52 53
         verifyPoll(pollId);
53 54
         Poll p = pollRepository.save(poll);
54 55
         return new ResponseEntity<>(HttpStatus.OK);

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Bestand weergeven

@@ -42,4 +42,4 @@ public class VoteController {
42 42
     public Iterable<Vote> getVote(@PathVariable Long pollId) {
43 43
         return voteRepository.findById(pollId);
44 44
     }
45
-}
45
+}

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Bestand weergeven

@@ -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.Set;
5 8
 
6 9
 @Entity
@@ -12,11 +15,13 @@ public class Poll {
12 15
     private long id;
13 16
 
14 17
     @Column(name = "QUESTION")
18
+    @NotEmpty
15 19
     private String question;
16 20
 
17 21
     @OneToMany(cascade = CascadeType.ALL)
18 22
     @JoinColumn(name = "POLL_ID")
19 23
     @OrderBy
24
+    @Size(min=2, max = 6)
20 25
     private Set<Option> options;
21 26
 
22 27
     public long getId() {

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Bestand weergeven

@@ -1,5 +1,8 @@
1 1
 package io.zipcoder.tc_spring_poll_application.error;
2 2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3 6
 public class ErrorDetail {
4 7
 
5 8
     private String title;
@@ -7,6 +10,7 @@ public class ErrorDetail {
7 10
     private String detail;
8 11
     private long timeStamp;
9 12
     private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
10 14
 
11 15
     public ErrorDetail() {
12 16
     }
@@ -58,4 +62,12 @@ public class ErrorDetail {
58 62
     public void setDeveloperMessage(String developerMessage) {
59 63
         this.developerMessage = developerMessage;
60 64
     }
65
+
66
+    public Map<String, List<ValidationError>> getErrors() {
67
+        return errors;
68
+    }
69
+
70
+    public void setErrors(Map<String, List<ValidationError>> errors) {
71
+        this.errors = errors;
72
+    }
61 73
 }

+ 58
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/RestExceptionHandler.java Bestand weergeven

@@ -0,0 +1,58 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+import io.zipcoder.tc_spring_poll_application.excepection.ResourceNotFoundException;
4
+import javafx.concurrent.Task;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.MessageSource;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.validation.FieldError;
10
+import org.springframework.web.bind.MethodArgumentNotValidException;
11
+import org.springframework.web.bind.annotation.ControllerAdvice;
12
+import org.springframework.web.bind.annotation.ExceptionHandler;
13
+
14
+import javax.servlet.http.HttpServletRequest;
15
+import java.util.ArrayList;
16
+import java.util.Date;
17
+import java.util.List;
18
+
19
+@ControllerAdvice
20
+public class RestExceptionHandler {
21
+
22
+    @Autowired
23
+    MessageSource messageSource;
24
+
25
+    @ExceptionHandler(ResourceNotFoundException.class)
26
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request){
27
+        ErrorDetail errorDetail = new ErrorDetail();
28
+        errorDetail.setTimeStamp(new Date().getTime());
29
+        errorDetail.setDeveloperMessage(rnfe.getMessage());
30
+        errorDetail.setDetail(rnfe.getCause().getMessage());
31
+
32
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
33
+    }
34
+
35
+    @ExceptionHandler(MethodArgumentNotValidException.class)
36
+    public ResponseEntity<?> handleValidationError(  MethodArgumentNotValidException manve, HttpServletRequest request){
37
+        ErrorDetail errorDetail = new ErrorDetail();
38
+        errorDetail.setTimeStamp(new Date().getTime());
39
+        errorDetail.setDeveloperMessage(new ResourceNotFoundException().getMessage());
40
+
41
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
42
+        for(FieldError fe : fieldErrors) {
43
+
44
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
45
+            if(validationErrorList == null) {
46
+                validationErrorList = new ArrayList<>();
47
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
48
+            }
49
+            ValidationError validationError = new ValidationError();
50
+            validationError.setCode(fe.getCode());
51
+            validationError.setMessage(messageSource.getMessage(fe, null));
52
+            validationErrorList.add(validationError);
53
+        }
54
+
55
+        return new ResponseEntity<>(fieldErrors, HttpStatus.NOT_FOUND);
56
+    }
57
+
58
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ValidationError.java Bestand weergeven

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+public class ValidationError {
4
+
5
+    private String code;
6
+    private String message;
7
+
8
+    public String getCode() {
9
+        return code;
10
+    }
11
+
12
+    public void setCode(String code) {
13
+        this.code = code;
14
+    }
15
+
16
+    public String getMessage() {
17
+        return message;
18
+    }
19
+
20
+    public void setMessage(String message) {
21
+        this.message = message;
22
+    }
23
+}

+ 0
- 26
src/main/java/io/zipcoder/tc_spring_poll_application/excepection/RestExceptionHandler.java Bestand weergeven

@@ -1,26 +0,0 @@
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
-}