jacob andersen преди 5 години
родител
ревизия
06b43ce26e

+ 10
- 7
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Целия файл

@@ -4,12 +4,15 @@ import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 4
 import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
6 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.data.domain.Page;
8
+import org.springframework.data.domain.Pageable;
7 9
 import org.springframework.http.HttpHeaders;
8 10
 import org.springframework.http.HttpStatus;
9 11
 import org.springframework.http.ResponseEntity;
10 12
 import org.springframework.web.bind.annotation.*;
11 13
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 14
 
15
+import javax.validation.Valid;
13 16
 import java.net.URI;
14 17
 
15 18
 @RestController
@@ -18,19 +21,19 @@ public class PollController {
18 21
     private PollRepository pollRepository;
19 22
 
20 23
     @Autowired
21
-    public PollController(PollRepository p) {
22
-        this.pollRepository = p;
24
+    public PollController(PollRepository pollRepository) {
25
+        this.pollRepository = pollRepository;
23 26
 
24 27
     }
25 28
 
26 29
     @RequestMapping(value="/polls", method= RequestMethod.GET)
27
-    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
-        Iterable<Poll> allPolls = pollRepository.findAll();
30
+    public ResponseEntity<Page<Poll>> getAllPolls(Pageable pageable) {
31
+        Page<Poll> allPolls = pollRepository.findAll(pageable);
29 32
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
30 33
     }
31 34
 
32 35
     @RequestMapping(value="/polls", method=RequestMethod.POST)
33
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
36
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
34 37
         poll = pollRepository.save(poll);
35 38
 
36 39
         URI newPollUri = ServletUriComponentsBuilder
@@ -52,7 +55,7 @@ public class PollController {
52 55
     }
53 56
 
54 57
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
55
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
58
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
56 59
         // Save the entity
57 60
         Poll p = pollRepository.save(poll);
58 61
         return new ResponseEntity<>(HttpStatus.OK);
@@ -67,6 +70,6 @@ public class PollController {
67 70
     public void verifyPoll(Long id)
68 71
     {
69 72
         if (!pollRepository.exists(id))
70
-            throw new ResourceNotFoundException();
73
+            throw new ResourceNotFoundException("Doesn't Exist");
71 74
     }
72 75
 }

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Целия файл

@@ -44,7 +44,8 @@ public class ErrorDetail
44 44
         this.timeStamp = timeStamp;
45 45
     }
46 46
 
47
-    public String getDeveloperMessage() {
47
+    public String getDeveloperMessage()
48
+    {
48 49
         return developerMessage;
49 50
     }
50 51
 

+ 16
- 11
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java Целия файл

@@ -18,40 +18,45 @@ import java.util.List;
18 18
 @ControllerAdvice
19 19
 public class RestExceptionHandler {
20 20
 
21
-    ErrorDetail errorDetail;
21
+    private final MessageSource messageSource;
22 22
 
23 23
     @Autowired
24
-    MessageSource messageSource;
24
+    public RestExceptionHandler(MessageSource messageSource) {
25
+        this.messageSource = messageSource;
26
+    }
27
+
28
+
29
+
25 30
 
26 31
     @ExceptionHandler(ResourceNotFoundException.class)
27 32
     public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request)
28 33
     {
29
-        ErrorDetail errdetail = new ErrorDetail();
30
-
31
-        errdetail.setTimeStamp(new Date().getTime());
32
-        errdetail.setStatus(404);
33
-        errdetail.setDetail("Resource couldn't be found");
34
+        ErrorDetail errDetail = new ErrorDetail();
34 35
 
35
-        return null;
36
+        errDetail.setTimeStamp(new Date().getTime());
37
+        errDetail.getDeveloperMessage();
38
+        errDetail.setDetail(rnfe.getLocalizedMessage());
39
+        return new ResponseEntity<>(errDetail, HttpStatus.NOT_FOUND);
36 40
     }
37 41
 
38 42
     @ExceptionHandler(MethodArgumentNotValidException.class)
39 43
     public ResponseEntity<?> handleValidationError(  MethodArgumentNotValidException manve,
40 44
                             HttpServletRequest request)
41 45
     {
46
+        ErrorDetail errDetail = new ErrorDetail();
42 47
         List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
43 48
         for(FieldError fe : fieldErrors) {
44 49
 
45
-            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
50
+            List<ValidationError> validationErrorList = errDetail.getErrors().get(fe.getField());
46 51
             if(validationErrorList == null) {
47 52
                 validationErrorList = new ArrayList<>();
48
-                errorDetail.getErrors().put(fe.getField(), validationErrorList);
53
+                errDetail.getErrors().put(fe.getField(), validationErrorList);
49 54
             }
50 55
             ValidationError validationError = new ValidationError();
51 56
             validationError.setCode(fe.getCode());
52 57
             validationError.setMessage(messageSource.getMessage(fe, null));
53 58
             validationErrorList.add(validationError);
54 59
         }
55
-        return null;
60
+        return new ResponseEntity<>(fieldErrors, HttpStatus.NOT_FOUND);
56 61
     }
57 62
 }