Преглед на файлове

on part 5, getError method part

ahsonali преди 8 години
родител
ревизия
e86d4f59b1

+ 24
- 0
src/main/java/dtos/OptionCount.java Целия файл

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

+ 25
- 0
src/main/java/dtos/VoteResult.java Целия файл

@@ -0,0 +1,25 @@
1
+package dtos;
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
+}

+ 56
- 0
src/main/java/dtos/error/ErrorDetail.java Целия файл

@@ -0,0 +1,56 @@
1
+package dtos.error;
2
+
3
+import java.util.HashMap;
4
+import java.util.List;
5
+import java.util.Map;
6
+
7
+
8
+public class ErrorDetail {
9
+
10
+    private String title;
11
+    private int status;
12
+    private String detail;
13
+    private long timeStamp;
14
+    private String developerMessage;
15
+    private Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>();
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
- 0
src/main/java/dtos/error/RestExceptionHandler.java Целия файл

@@ -0,0 +1,57 @@
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.Date;
13
+import java.util.List;
14
+
15
+@ControllerAdvice
16
+public class RestExceptionHandler {
17
+
18
+    @ExceptionHandler(MethodArgumentNotValidException.class)
19
+    public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
20
+
21
+        ErrorDetail errorDetail = new ErrorDetail();
22
+        //Populate
23
+        errorDetail.setTimeStamp(new Date().getTime());
24
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
25
+        String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
26
+        if(requestPath == null){
27
+            requestPath = request.getRequestURI();
28
+        }
29
+        errorDetail.setTitle("Validation Failed");
30
+        errorDetail.setDetail("Input validation failed");
31
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
32
+
33
+        //Create Validation Error instances
34
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
35
+        for(FieldError fe: fieldErrors){
36
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
37
+
38
+        }
39
+    }
40
+
41
+
42
+//    @ExceptionHandler(ResourceNotFoundException.class)
43
+//    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
44
+//
45
+//        ErrorDetail errorDetail = new ErrorDetail();
46
+//        errorDetail.setTimeStamp(new Date().getTime());
47
+//        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
48
+//        errorDetail.setTitle("Resource Not Found");
49
+//        errorDetail.setDetail(rnfe.getMessage());
50
+//        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
51
+//        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
52
+//        }
53
+
54
+
55
+
56
+
57
+}

+ 23
- 0
src/main/java/dtos/error/ValidationError.java Целия файл

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

+ 30
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Целия файл

@@ -0,0 +1,30 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+
4
+import dtos.VoteResult;
5
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
6
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RequestParam;
12
+import org.springframework.web.bind.annotation.RestController;
13
+
14
+import javax.inject.Inject;
15
+
16
+@RestController
17
+public class ComputeResultController {
18
+
19
+    @Inject
20
+    private VoteRepository voteRepository;
21
+
22
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
23
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
24
+        VoteResult voteResult = new VoteResult();
25
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
26
+
27
+        //TODO: Implement algorithm to count votes
28
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
29
+    }
30
+}

+ 8
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Целия файл

@@ -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;
@@ -11,6 +12,7 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 12
 
12 13
 
13 14
 import javax.inject.Inject;
15
+import javax.validation.Valid;
14 16
 import java.net.URI;
15 17
 
16 18
 @RestController
@@ -32,7 +34,7 @@ public class PollController {
32 34
     }
33 35
 
34 36
     @RequestMapping(value="/polls", method=RequestMethod.POST)
35
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
37
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
36 38
         poll = pollRepository.save(poll);
37 39
 
38 40
         HttpHeaders responseHeaders = new HttpHeaders();
@@ -67,7 +69,11 @@ public class PollController {
67 69
     }
68 70
 
69 71
 
70
-
72
+    public void verifyPoll(Long pollId){
73
+        if (pollRepository.findOne(pollId) == null){
74
+                        throw new ResourceNotFoundException();
75
+        }
76
+    }
71 77
 
72 78
 
73 79
 

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Целия файл

@@ -10,6 +10,8 @@ import org.springframework.http.ResponseEntity;
10 10
 import org.springframework.web.bind.annotation.*;
11 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 12
 
13
+import javax.inject.Inject;
14
+
13 15
 @RestController
14 16
 public class VoteController {
15 17
 
@@ -31,5 +33,15 @@ public class VoteController {
31 33
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
32 34
     }
33 35
 
36
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
37
+    public Iterable<Vote> getAllVotes() {
38
+        return voteRepository.findAll();
39
+    }
40
+
41
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
42
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
43
+        return voteRepository.findVotesByPoll(pollId);
44
+    }
45
+
34 46
 
35 47
 }

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Целия файл

@@ -16,6 +16,8 @@ public class Option {
16 16
     @Column(name="OPTION_VALUE")
17 17
     private String value;
18 18
 
19
+    public Option () {}
20
+
19 21
     public Long getId() {
20 22
         return id;
21 23
     }

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Целия файл

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

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Целия файл

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

+ 8
- 0
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
 
9
+    @Query(value = "SELECT v.* " +
10
+            "FROM Option o, Vote v " +
11
+            "WHERE o.POLL_ID = ?1 " +
12
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
14
+
15
+
8 16
 }