Browse Source

up to part 6

Lauren Green 6 years ago
parent
commit
e23e752e3b

+ 18
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

@@ -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.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpHeaders;
@@ -9,6 +10,7 @@ import org.springframework.http.ResponseEntity;
9 10
 import org.springframework.web.bind.annotation.*;
10 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 12
 
13
+import javax.validation.Valid;
12 14
 import java.net.URI;
13 15
 
14 16
 @RestController
@@ -28,7 +30,7 @@ public class PollController {
28 30
     }
29 31
 
30 32
     @RequestMapping(value="/polls", method=RequestMethod.POST)
31
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
33
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
32 34
         poll = pollRepository.save(poll);
33 35
         URI newPollUri = ServletUriComponentsBuilder
34 36
                 .fromCurrentRequest()
@@ -42,21 +44,34 @@ public class PollController {
42 44
 
43 45
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
44 46
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
45 48
         Poll p = pollRepository.findOne(pollId);
46 49
         return new ResponseEntity<> (p, HttpStatus.OK);
47 50
     }
48 51
 
49 52
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
50
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
53
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
54
+        verifyPoll(pollId);
51 55
         Poll p = pollRepository.save(poll);
52 56
         return new ResponseEntity<>(HttpStatus.OK);
53 57
     }
54 58
 
55 59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56 60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        verifyPoll(pollId);
57 62
         pollRepository.delete(pollId);
58 63
         return new ResponseEntity<>(HttpStatus.OK);
59 64
     }
60 65
 
61
-
66
+    public void verifyPoll(Long id) {
67
+        Iterable<Poll> allPolls = pollRepository.findAll();
68
+        boolean verified = false;
69
+        for(Poll poll : allPolls){
70
+            if(poll.getId() == id) {
71
+                verified = true;
72
+            }
73
+        }
74
+        if(!verified)
75
+            throw new ResourceNotFoundException();
76
+    }
62 77
 }

+ 6
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

@@ -2,6 +2,10 @@ package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3 3
 import javax.persistence.*;
4 4
 import java.util.Set;
5
+import org.hibernate.validator.constraints.NotEmpty;
6
+import javax.validation.constraints.Size;
7
+import javax.validation.Valid;
8
+
5 9
 
6 10
 @Entity
7 11
 public class Poll {
@@ -11,10 +15,12 @@ public class Poll {
11 15
     @Column(name = "POLL_ID")
12 16
     public long id;
13 17
     @Column(name = "QUESTION")
18
+    @NotEmpty
14 19
     public String question;
15 20
     @OneToMany(cascade = CascadeType.ALL)
16 21
     @JoinColumn(name = "POLL_ID")
17 22
     @OrderBy
23
+    @Size(min=2, max=6)
18 24
     public Set<Option> options;
19 25
 
20 26
     public long getId() {

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java View File

@@ -0,0 +1,62 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.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 String getTitle() {
16
+        return title;
17
+    }
18
+
19
+    public void setTitle(String title) {
20
+        this.title = title;
21
+    }
22
+
23
+    public int getStatus() {
24
+        return status;
25
+    }
26
+
27
+    public void setStatus(int status) {
28
+        this.status = status;
29
+    }
30
+
31
+    public String getDetail() {
32
+        return detail;
33
+    }
34
+
35
+    public void setDetail(String detail) {
36
+        this.detail = detail;
37
+    }
38
+
39
+    public long getTimeStamp() {
40
+        return timeStamp;
41
+    }
42
+
43
+    public void setTimeStamp(long timeStamp) {
44
+        this.timeStamp = timeStamp;
45
+    }
46
+
47
+    public String getDeveloperMessage() {
48
+        return developerMessage;
49
+    }
50
+
51
+    public void setDeveloperMessage(String developerMessage) {
52
+        this.developerMessage = developerMessage;
53
+    }
54
+
55
+    public Map<String, List<ValidationError>> getErrors() {
56
+        return errors;
57
+    }
58
+
59
+    public void setErrors(Map<String, List<ValidationError>> errors) {
60
+        this.errors = errors;
61
+    }
62
+}

+ 55
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java View File

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

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java View File

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+public class ValidationError {
4
+
5
+    String code;
6
+    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
+}

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java View File

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

+ 0
- 0
src/main/resources/import.sql View File


+ 2
- 0
src/main/resources/messages.properties View File

@@ -0,0 +1,2 @@
1
+NotEmpty.poll.question=Question is a required field
2
+Size.poll.options=Options must be greater than {2} and less than {1}