jacob andersen 5 anni fa
parent
commit
06b43ce26e

+ 10
- 7
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Vedi File

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

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

+ 16
- 11
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java Vedi File

18
 @ControllerAdvice
18
 @ControllerAdvice
19
 public class RestExceptionHandler {
19
 public class RestExceptionHandler {
20
 
20
 
21
-    ErrorDetail errorDetail;
21
+    private final MessageSource messageSource;
22
 
22
 
23
     @Autowired
23
     @Autowired
24
-    MessageSource messageSource;
24
+    public RestExceptionHandler(MessageSource messageSource) {
25
+        this.messageSource = messageSource;
26
+    }
27
+
28
+
29
+
25
 
30
 
26
     @ExceptionHandler(ResourceNotFoundException.class)
31
     @ExceptionHandler(ResourceNotFoundException.class)
27
     public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request)
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
     @ExceptionHandler(MethodArgumentNotValidException.class)
42
     @ExceptionHandler(MethodArgumentNotValidException.class)
39
     public ResponseEntity<?> handleValidationError(  MethodArgumentNotValidException manve,
43
     public ResponseEntity<?> handleValidationError(  MethodArgumentNotValidException manve,
40
                             HttpServletRequest request)
44
                             HttpServletRequest request)
41
     {
45
     {
46
+        ErrorDetail errDetail = new ErrorDetail();
42
         List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
47
         List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
43
         for(FieldError fe : fieldErrors) {
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
             if(validationErrorList == null) {
51
             if(validationErrorList == null) {
47
                 validationErrorList = new ArrayList<>();
52
                 validationErrorList = new ArrayList<>();
48
-                errorDetail.getErrors().put(fe.getField(), validationErrorList);
53
+                errDetail.getErrors().put(fe.getField(), validationErrorList);
49
             }
54
             }
50
             ValidationError validationError = new ValidationError();
55
             ValidationError validationError = new ValidationError();
51
             validationError.setCode(fe.getCode());
56
             validationError.setCode(fe.getCode());
52
             validationError.setMessage(messageSource.getMessage(fe, null));
57
             validationError.setMessage(messageSource.getMessage(fe, null));
53
             validationErrorList.add(validationError);
58
             validationErrorList.add(validationError);
54
         }
59
         }
55
-        return null;
60
+        return new ResponseEntity<>(fieldErrors, HttpStatus.NOT_FOUND);
56
     }
61
     }
57
 }
62
 }