Kaynağa Gözat

Up to 5.6 completed

PeterMcCormick 8 yıl önce
ebeveyn
işleme
596cc4a2d7

+ 18
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Dosyayı Görüntüle

@@ -1,20 +1,22 @@
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;
6
+import org.springframework.beans.factory.annotation.Autowired;
5 7
 import org.springframework.http.HttpHeaders;
6 8
 import org.springframework.http.HttpStatus;
7 9
 import org.springframework.http.ResponseEntity;
8 10
 import org.springframework.web.bind.annotation.*;
9 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10 12
 
11
-import javax.inject.Inject;
13
+import javax.validation.Valid;
12 14
 import java.net.URI;
13 15
 
14 16
 @RestController
15 17
 public class PollController {
16 18
 
17
-    @Inject
19
+    @Autowired
18 20
     private PollRepository pollRepository;
19 21
 
20 22
     @RequestMapping(value="/polls", method= RequestMethod.GET)
@@ -24,7 +26,7 @@ public class PollController {
24 26
     }
25 27
 
26 28
     @RequestMapping(value="/polls", method=RequestMethod.POST)
27
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
29
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
28 30
 
29 31
         URI newPollUri = ServletUriComponentsBuilder
30 32
                 .fromCurrentRequest()
@@ -38,22 +40,34 @@ public class PollController {
38 40
 
39 41
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
40 42
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
43
+        verifyPoll(pollId);
41 44
         Poll p = pollRepository.findOne(pollId);
42 45
         return new ResponseEntity<> (p, HttpStatus.OK);
43 46
     }
44 47
 
45 48
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
46
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
49
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
47 50
         // Save the entity
51
+        verifyPoll(pollId);
48 52
         Poll p = pollRepository.save(poll);
49 53
         return new ResponseEntity<>(HttpStatus.OK);
50 54
     }
51 55
 
52 56
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
53 57
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
+        verifyPoll(pollId);
54 59
         pollRepository.delete(pollId);
55 60
         return new ResponseEntity<>(HttpStatus.OK);
56 61
     }
57 62
 
63
+    protected void verifyPoll(Long pollId) throws ResourceNotFoundException {
64
+        Poll poll = pollRepository.findOne((pollId));
65
+        if(poll == null) {
66
+            throw new ResourceNotFoundException(("Poll with id" + pollId + " not found"));
67
+
68
+        }
69
+
70
+    }
71
+
58 72
 
59 73
 }

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Dosyayı Görüntüle

@@ -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
 

+ 63
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Dosyayı Görüntüle

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

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ValidationError.java Dosyayı Görüntüle

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

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Dosyayı Görüntüle

@@ -0,0 +1,20 @@
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
+    private static final long serialVersionUID = 1L;
10
+
11
+    public ResourceNotFoundException() {}
12
+
13
+    public ResourceNotFoundException(String message) {
14
+        super(message);
15
+    }
16
+
17
+    public ResourceNotFoundException(String message, Throwable cause) {
18
+        super(message, cause);
19
+    }
20
+}

+ 64
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Dosyayı Görüntüle

@@ -0,0 +1,64 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.error.ValidationError;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.validation.FieldError;
8
+import org.springframework.web.bind.MethodArgumentNotValidException;
9
+import org.springframework.web.bind.annotation.ControllerAdvice;
10
+import org.springframework.web.bind.annotation.ExceptionHandler;
11
+import sun.tools.tree.FieldExpression;
12
+
13
+import javax.servlet.http.HttpServletRequest;
14
+import java.lang.reflect.Field;
15
+import java.util.ArrayList;
16
+import java.util.Date;
17
+import java.util.List;
18
+
19
+@ControllerAdvice
20
+public class RestExceptionHandler {
21
+
22
+    //    @ExceptionHandler(ResourceNotFoundException.class)
23
+//    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
24
+//        ErrorDetail errorDetail = new ErrorDetail();
25
+//        errorDetail.setTimeStamp(new Date().getTime());
26
+//        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
27
+//        errorDetail.setTitle("Resource Not Found");
28
+//        errorDetail.setDetail(rnfe.getMessage());
29
+//        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
30
+//        return  new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
31
+//        }
32
+    @ExceptionHandler(MethodArgumentNotValidException.class)
33
+    public ResponseEntity<?>
34
+    handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
35
+
36
+        ErrorDetail errorDetail = new ErrorDetail();
37
+        // Populate errorDetail instance
38
+        errorDetail.setTimeStamp(new Date().getTime());
39
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
40
+        String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
41
+        if (requestPath == null) {
42
+            requestPath = request.getRequestURI();
43
+        }
44
+        errorDetail.setTitle("Validation Failed");
45
+        errorDetail.setDetail("Input validation failed");
46
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
47
+
48
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
49
+        for (FieldError fe : fieldErrors) {
50
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
51
+            if (validationErrorList == null) {
52
+                validationErrorList = new ArrayList<ValidationError>();
53
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
54
+            }
55
+            ValidationError validationError = new ValidationError();
56
+            validationError.setCode(fe.getCode());
57
+            validationError.setMessage(fe.getDefaultMessage());
58
+            validationErrorList.add(validationError);
59
+        }
60
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
61
+
62
+    }
63
+    /** handleResourceNotFoundException method removed **/
64
+}