瀏覽代碼

finished up to part 6

Keith Brinker 8 年之前
父節點
當前提交
437ba24f0f

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

@@ -2,7 +2,9 @@ package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Option;
4 4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFound;
5 6
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
7
+import org.springframework.boot.context.config.ResourceNotFoundException;
6 8
 import org.springframework.http.HttpHeaders;
7 9
 import org.springframework.http.HttpStatus;
8 10
 import org.springframework.http.ResponseEntity;
@@ -64,4 +66,12 @@ public class PollController {
64 66
         return new ResponseEntity<> (p, HttpStatus.OK);
65 67
     }
66 68
 
69
+
70
+    void verifyPoll(Long pollId) throws ResourceNotFoundException {
71
+                Poll poll = pollRepository.findOne(pollId);
72
+                if (poll == null){
73
+                        throw new ResourceNotFound("Poll with id "+ pollId + " not found");
74
+                    }
75
+            }
76
+
67 77
 }

+ 12
- 6
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

@@ -29,15 +29,21 @@ public class VoteController {
29 29
             return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
30 30
         }
31 31
 
32
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
33
+    public Iterable<Vote> getAllVotes() {
34
+        return voteRepository.findAll();
35
+    }
32 36
 
33
-    public interface VoteRepository extends CrudRepository<Vote, Long> {
34
-        @Query(value = "SELECT v.* " +
35
-                "FROM Option o, Vote v " +
36
-                "WHERE o.POLL_ID = ?1 " +
37
-                "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
38
-        public Iterable<Vote> findVotesByPoll(Long pollId);
37
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
38
+    public Iterable<Vote> getAllVotes(@PathVariable Long pollId) {
39
+        return voteRepository.findVotesByPoll(pollId);
39 40
     }
40 41
 
42
+
43
+
44
+
45
+
46
+
41 47
 }
42 48
 
43 49
 

+ 49
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/ComputeResultController.java 查看文件

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

+ 26
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/OptionCount.java 查看文件

@@ -0,0 +1,26 @@
1
+package io.zipcoder.tc_spring_poll_application.dto;
2
+
3
+public class OptionCount {
4
+
5
+
6
+        private Long optionId;
7
+        private int count;
8
+
9
+        public Long getOptionId() {
10
+            return optionId;
11
+        }
12
+
13
+        public void setOptionId(Long optionId) {
14
+            this.optionId = optionId;
15
+        }
16
+
17
+        public int getCount() {
18
+            return count;
19
+        }
20
+
21
+        public void setCount(int count) {
22
+            this.count = count;
23
+        }
24
+    }
25
+
26
+

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/VoteResult.java 查看文件

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.dto;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+
7
+    private int totalVotes;
8
+    private Collection<OptionCount> results;
9
+
10
+    public int getTotalVotes() {
11
+        return totalVotes;
12
+    }
13
+
14
+    public void setTotalVotes(int totalVotes) {
15
+        this.totalVotes = totalVotes;
16
+    }
17
+
18
+    public Collection<OptionCount> getResults() {
19
+        return results;
20
+    }
21
+
22
+    public void setResults(Collection<OptionCount> results) {
23
+        this.results = results;
24
+    }
25
+}

+ 64
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java 查看文件

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

+ 73
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java 查看文件

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

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java 查看文件

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.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
+

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFound.java 查看文件

@@ -0,0 +1,22 @@
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 ResourceNotFound extends RuntimeException{
8
+
9
+
10
+
11
+    public ResourceNotFound(){
12
+
13
+    }
14
+
15
+    public ResourceNotFound(String message){
16
+        super(message);
17
+    }
18
+
19
+    public ResourceNotFound (String message, Throwable cause){
20
+        super(message, cause);
21
+    }
22
+}

+ 9
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java 查看文件

@@ -1,8 +1,16 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
4 5
 import org.springframework.data.repository.CrudRepository;
5 6
 
6 7
 public interface VoteRepository extends CrudRepository<Vote, Long> {
7 8
 
8
-}
9
+
10
+        @Query(value = "SELECT v.* " +
11
+                "FROM Option o, Vote v " +
12
+                "WHERE o.POLL_ID = ?1 " +
13
+                "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
14
+        public Iterable<Vote> findVotesByPoll(Long pollId);
15
+    }
16
+

+ 2
- 0
src/resources/messages.properties 查看文件

@@ -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}