Anthony Jordan 8 gadus atpakaļ
vecāks
revīzija
17f5f0650b

+ 62
- 0
src/main/java/dtos/error/ErrorDetail.java Parādīt failu

@@ -0,0 +1,62 @@
1
+package dtos.error;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+
6
+public class ErrorDetail {
7
+
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;
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
+}

+ 59
- 0
src/main/java/dtos/error/RestExceptionHandler.java Parādīt failu

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

+ 22
- 0
src/main/java/dtos/error/ValidationError.java Parādīt failu

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

+ 18
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Parādīt failu

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import dtos.OptionCount;
3 4
 import dtos.VoteResult;
4 5
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
5 6
 import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
@@ -11,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestParam;
11 12
 import org.springframework.web.bind.annotation.RestController;
12 13
 
13 14
 import javax.inject.Inject;
15
+import java.util.HashMap;
16
+import java.util.Map;
14 17
 
15 18
 @RestController
16 19
 public class ComputeResultController {
@@ -22,7 +25,21 @@ public class ComputeResultController {
22 25
         VoteResult voteResult = new VoteResult();
23 26
         Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
24 27
 
25
-        //TODO: Implement algorithm to count votes
28
+        int totalVotes = 0;
29
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
30
+        for(Vote v : allVotes) {
31
+            totalVotes ++;
32
+            // Get the OptionCount corresponding to this Option
33
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
34
+            if(optionCount == null) {
35
+                optionCount = new OptionCount();
36
+                optionCount.setOptionId(v.getOption().getId());
37
+                tempMap.put(v.getOption().getId(), optionCount);
38
+            }
39
+            optionCount.setCount(optionCount.getCount()+1);
40
+        }
41
+        voteResult.setTotalVotes(totalVotes);
42
+        voteResult.setResults(tempMap.values());
26 43
         return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
27 44
     }
28 45
 }

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Parādīt failu

@@ -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
@@ -25,10 +27,12 @@ public class PollController {
25 27
 
26 28
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
27 29
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
30
+        verifyPoll(pollId);
28 31
         Poll p = pollRepository.findOne(pollId);
29 32
         return new ResponseEntity<> (p, HttpStatus.OK);
30 33
     }
31 34
 
35
+    @Valid
32 36
     @RequestMapping(value="/polls", method=RequestMethod.POST)
33 37
     public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
34 38
         poll = pollRepository.save(poll);
@@ -45,13 +49,21 @@ public class PollController {
45 49
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
46 50
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
47 51
         // Save the entity
52
+        verifyPoll(pollId);
48 53
         Poll p = pollRepository.save(poll);
49 54
         return new ResponseEntity<>(HttpStatus.OK);
50 55
     }
51 56
 
52 57
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
53 58
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
59
+        verifyPoll(pollId);
54 60
         pollRepository.delete(pollId);
55 61
         return new ResponseEntity<>(HttpStatus.OK);
56 62
     }
63
+
64
+    public void verifyPoll(Long pollId){
65
+        if (pollRepository.findOne(pollId) == null){
66
+            throw new ResourceNotFoundException();
67
+        }
68
+    }
57 69
 }

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Parādīt failu

@@ -9,12 +9,14 @@ import org.springframework.web.bind.annotation.*;
9 9
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10 10
 
11 11
 import javax.inject.Inject;
12
+import javax.validation.Valid;
12 13
 
13 14
 @RestController
14 15
 public class VoteController {
15 16
     @Inject
16 17
     private VoteRepository voteRepository;
17 18
 
19
+    @Valid
18 20
     @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
19 21
     public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
20 22
             vote) {

+ 6
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Parādīt failu

@@ -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
-    @JoinColumn(name = "POLL_ID")
22
+    @JoinColumn(name = "OPTION_ID")
19 23
     @OrderBy
24
+    @Size(min=2, max = 6)
20 25
     private Set<Option> options;
21 26
 
22 27
     public long getId() {

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Parādīt failu

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

+ 0
- 0
src/main/resources/messages.properties Parādīt failu