Bladeren bron

loaded dummy data, 6.1

Lawrence Wu 8 jaren geleden
bovenliggende
commit
f1efb87961

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

@@ -0,0 +1,68 @@
1
+package io.zipcoder.tc_spring_poll_application;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dtos.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.dtos.error.ValidationError;
5
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
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
+import org.springframework.web.bind.annotation.ResponseBody;
14
+import org.springframework.web.bind.annotation.ResponseStatus;
15
+
16
+import javax.inject.Inject;
17
+import javax.servlet.http.HttpServletRequest;
18
+import java.util.ArrayList;
19
+import java.util.Date;
20
+import java.util.List;
21
+
22
+@ControllerAdvice
23
+public class RestExceptionHandler {
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.setStatus(HttpStatus.NOT_FOUND.value());
29
+//        errorDetail.setTitle("Resource Not Found");
30
+//        errorDetail.setDetail(rnfe.getMessage());
31
+//        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
32
+//        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
33
+//    }
34
+    @Inject
35
+    private MessageSource messageSource;
36
+
37
+    @ExceptionHandler(MethodArgumentNotValidException.class)
38
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
39
+    public @ResponseBody
40
+    ErrorDetail handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
41
+        ErrorDetail errorDetail = new ErrorDetail();
42
+        // Populate errorDetail instance
43
+        errorDetail.setTimeStamp(new Date().getTime());
44
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
45
+        String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
46
+        if (requestPath == null) {
47
+            requestPath = request.getRequestURI();
48
+        }
49
+        errorDetail.setTitle("Validation Failed");
50
+        errorDetail.setDetail("Input validation failed");
51
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
52
+    // Create ValidationError instances
53
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
54
+        for (FieldError fe : fieldErrors) {
55
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
56
+            if (validationErrorList == null) {
57
+                validationErrorList = new ArrayList<ValidationError>();
58
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
59
+            }
60
+            ValidationError validationError = new ValidationError();
61
+            validationError.setCode(fe.getCode());
62
+            validationError.setMessage(fe.getDefaultMessage());
63
+            validationErrorList.add(validationError);
64
+            }
65
+    //    return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
66
+            return errorDetail;
67
+        }
68
+}

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

@@ -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.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
9 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10 11
 
11 12
 import javax.inject.Inject;
13
+import javax.validation.Valid;
12 14
 import java.net.URI;
13 15
 
14 16
 @RestController
@@ -23,11 +25,22 @@ public class PollController {
23 25
   }
24 26
 
25 27
   @RequestMapping(value ="/polls", method= RequestMethod.POST)
26
-  public ResponseEntity<?> createPoll(@RequestBody Poll poll){
28
+  public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll){
27 29
     poll = pollRepository.save(poll);
30
+
31
+    //Set the location header for the newly created resource
32
+    HttpHeaders responseHeaders = new HttpHeaders();
33
+
28 34
     URI newPollUri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(poll.getId()).toUri();
35
+    responseHeaders.setLocation(newPollUri);
29 36
     return new ResponseEntity<>(new HttpHeaders(), HttpStatus.CREATED);
30 37
   }
38
+  public void verifyPoll(Long pollId) throws ResourceNotFoundException {
39
+    Poll poll = pollRepository.findOne(pollId);
40
+    if(poll == null) {
41
+      throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
42
+    }
43
+  }
31 44
   @RequestMapping(value="/polls/{pollId}", method= RequestMethod.GET)
32 45
   public ResponseEntity<?> getPoll(@PathVariable Long pollId){
33 46
     Poll poll = pollRepository.findOne(pollId);

+ 11
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Bestand weergeven

@@ -15,6 +15,9 @@ public class Option {
15 15
     @Column(name = "OPTION_VALUE")
16 16
     private String value;
17 17
 
18
+    @Column(name = "POLL_ID")
19
+    private Long pollId;
20
+
18 21
     public Long getId() {
19 22
         return id;
20 23
     }
@@ -30,4 +33,12 @@ public class Option {
30 33
     public void setValue(String value) {
31 34
         this.value = value;
32 35
     }
36
+
37
+    public Long getPollId() {
38
+        return pollId;
39
+    }
40
+
41
+    public void setPollId(Long pollId) {
42
+        this.pollId = pollId;
43
+    }
33 44
 }

+ 6
- 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() {
@@ -42,4 +47,5 @@ public class Poll {
42 47
     public void setOptions(Set<Option> options) {
43 48
         this.options = options;
44 49
     }
50
+
45 51
 }

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

@@ -0,0 +1,62 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+import java.util.HashMap;
4
+import java.util.List;
5
+import java.util.Map;
6
+
7
+public class ErrorDetail {
8
+    private String title;
9
+    private int status;
10
+    private String detail;
11
+    private long timeStamp;
12
+    private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>();
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
+}

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

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

+ 21
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Bestand weergeven

@@ -0,0 +1,21 @@
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
+
14
+    public ResourceNotFoundException(String message) {
15
+        super(message);
16
+    }
17
+
18
+    public ResourceNotFoundException(String message, Throwable cause) {
19
+        super(message, cause);
20
+    }
21
+}

+ 4
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Bestand weergeven

@@ -1,6 +1,10 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
2 2
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
3 3
 import org.springframework.data.repository.CrudRepository;
4
+import org.springframework.data.repository.PagingAndSortingRepository;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.RequestMethod;
4 7
 
5 8
 public interface PollRepository extends CrudRepository<Poll, Long> {
9
+
6 10
 }

+ 7
- 0
src/main/resources/import.sql Bestand weergeven

@@ -0,0 +1,7 @@
1
+insert into poll (poll_id, question) values (1, 'What is your favorite color?');
2
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
3
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
4
+insert into option (option_id, option_value, poll_id) values (3, 'Yellow', 1);
5
+
6
+
7
+

+ 3
- 0
src/main/resources/messages.properties Bestand weergeven

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