Michelle DiMarino 5 år sedan
förälder
incheckning
d61b330d9c

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Visa fil

@@ -0,0 +1,33 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.dto.VoteResult;
5
+import io.zipcoder.tc_spring_poll_application.repository.VoteRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
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
+@RestController
15
+public class ComputeResultController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public ComputeResultController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
25
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
26
+        VoteResult voteResult = new VoteResult();
27
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28
+
29
+        //TODO: Implement algorithm to count votes
30
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
31
+    }
32
+}
33
+

+ 72
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Visa fil

@@ -0,0 +1,72 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5
+import io.zipcoder.tc_spring_poll_application.repository.PollRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpHeaders;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12
+
13
+import javax.validation.Valid;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    PollRepository pollRepository;
20
+
21
+    @Autowired
22
+    public PollController(PollRepository pollRepository){
23
+        this.pollRepository = pollRepository;
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+    }
31
+
32
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
33
+    public ResponseEntity<?> createPoll(@RequestBody @Valid Poll poll) {
34
+        poll = pollRepository.save(poll);
35
+        URI newPollUri = ServletUriComponentsBuilder
36
+                .fromCurrentRequest()
37
+                .path("/{id}")
38
+                .buildAndExpand(poll.getId())
39
+                .toUri();
40
+        HttpHeaders httpHeaders = new HttpHeaders();
41
+        httpHeaders.setLocation(newPollUri);
42
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
43
+    }
44
+
45
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
46
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
48
+        Poll p = pollRepository.findOne(pollId);
49
+        return new ResponseEntity<> (p, HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
53
+    public ResponseEntity<?> updatePoll(@RequestBody @Valid Poll poll, @PathVariable Long pollId) {
54
+        verifyPoll(pollId);
55
+        Poll p = pollRepository.save(poll);
56
+        return new ResponseEntity<>(HttpStatus.OK);
57
+    }
58
+
59
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
60
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        verifyPoll(pollId);
62
+        pollRepository.delete(pollId);
63
+        return new ResponseEntity<>(HttpStatus.OK);
64
+    }
65
+
66
+    public void verifyPoll(Long pollID) throws ResourceNotFoundException{
67
+        if (pollRepository.findOne(pollID) == null){
68
+            throw new ResourceNotFoundException();
69
+        }
70
+    }
71
+
72
+}

+ 42
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Visa fil

@@ -0,0 +1,42 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repository.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+@RestController
13
+public class VoteController {
14
+
15
+    private VoteRepository voteRepository;
16
+
17
+    @Autowired
18
+    public VoteController(VoteRepository voteRepository) {
19
+        this.voteRepository = voteRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
23
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
24
+            vote) {
25
+        vote = voteRepository.save(vote);
26
+        // Set the headers for the newly created resource
27
+        HttpHeaders responseHeaders = new HttpHeaders();
28
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
29
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
30
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
31
+    }
32
+
33
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
34
+    public Iterable<Vote> getAllVotes() {
35
+        return voteRepository.findAll();
36
+    }
37
+
38
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
39
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
40
+        return voteRepository.findVotesByPoll(pollId);
41
+    }
42
+}

+ 43
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Visa fil

@@ -0,0 +1,43 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.Entity;
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Option {
10
+    @Id
11
+    @Column(name = "OPTION_ID")
12
+    @GeneratedValue
13
+    private Long id;
14
+
15
+
16
+    @Column(name = "OPTION_VALUE")
17
+    private String value;
18
+
19
+    public Option(){ }
20
+
21
+    public Option(Long id, String value){
22
+        this.id = id;
23
+        this.value = value;
24
+    }
25
+
26
+    public Long getId() {
27
+        return id;
28
+    }
29
+
30
+    public void setId(Long id) {
31
+        this.id = id;
32
+    }
33
+
34
+    public String getValue() {
35
+        return value;
36
+    }
37
+
38
+    public void setValue(String value) {
39
+        this.value = value;
40
+    }
41
+
42
+
43
+}

+ 60
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Visa fil

@@ -0,0 +1,60 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
5
+import javax.persistence.*;
6
+import javax.validation.constraints.Size;
7
+import java.util.Set;
8
+
9
+@Entity
10
+public class Poll {
11
+
12
+    @Id
13
+    @GeneratedValue
14
+    @Column(name = "POLL_ID")
15
+    private Long id;
16
+
17
+    @NotEmpty
18
+    @Column(name = "QUESTION")
19
+    String question;
20
+
21
+    @Size(min=2, max = 6)
22
+    @OneToMany(cascade = CascadeType.ALL)
23
+    @JoinColumn(name = "POLL_ID")
24
+    @OrderBy
25
+    Set<Option> options;
26
+
27
+    public Poll(){ }
28
+
29
+    public Poll(Long id, String question, Set<Option> options){
30
+        this.id = id;
31
+        this.question = question;
32
+        this.options = options;
33
+    }
34
+
35
+    public Long getId() {
36
+        return id;
37
+    }
38
+
39
+    public void setId(Long id) {
40
+        this.id = id;
41
+    }
42
+
43
+    public String getQuestion() {
44
+        return question;
45
+    }
46
+
47
+    public void setQuestion(String question) {
48
+        this.question = question;
49
+    }
50
+
51
+    public Set<Option> getOptions() {
52
+        return options;
53
+    }
54
+
55
+    public void setOptions(Set<Option> options) {
56
+        this.options = options;
57
+    }
58
+
59
+
60
+}

+ 43
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Visa fil

@@ -0,0 +1,43 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+
8
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "VOTE_ID")
11
+    Long id;
12
+
13
+
14
+    @ManyToOne
15
+    @JoinColumn(name = "OPTION_ID")
16
+    Option option;
17
+
18
+    public Vote(){}
19
+
20
+    public Vote(Long id, Option option){
21
+        this.id = id;
22
+        this.option = option;
23
+    }
24
+
25
+    public Long getId() {
26
+        return id;
27
+    }
28
+
29
+    public void setId(Long id) {
30
+        this.id = id;
31
+    }
32
+
33
+    public Option getOption() {
34
+        return option;
35
+    }
36
+
37
+    public void setOption(Option option) {
38
+        this.option = option;
39
+    }
40
+
41
+
42
+
43
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/OptionCount.java Visa fil

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

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/VoteResult.java Visa fil

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

+ 70
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Visa fil

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

+ 30
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java Visa fil

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

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Visa fil

@@ -0,0 +1,33 @@
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
+    String message;
10
+    Throwable cause;
11
+
12
+    public ResourceNotFoundException(){}
13
+
14
+    public ResourceNotFoundException(String message){
15
+        super(message);
16
+    }
17
+
18
+    public ResourceNotFoundException(String message, Throwable cause){
19
+        super(message, cause);
20
+    }
21
+
22
+    @Override
23
+    public String getMessage() {
24
+        return message;
25
+    }
26
+
27
+    @Override
28
+    public Throwable getCause() {
29
+        return cause;
30
+    }
31
+
32
+
33
+}

+ 48
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Visa fil

@@ -0,0 +1,48 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dto.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.dto.error.ValidationError;
5
+import org.springframework.beans.factory.annotation.Autowired;
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 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 ed = new ErrorDetail("ResourceNotFoundException",404, rnfe.getCause().toString(), new Date().getTime(),rnfe.getMessage());
27
+        return new ResponseEntity<>(ed, HttpStatus.NOT_FOUND);
28
+    }
29
+
30
+    @ExceptionHandler(MethodArgumentNotValidException.class)
31
+    public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
32
+        ErrorDetail errorDetail = new ErrorDetail("Method Argument Not Valid", 400, manve.getCause().toString(), new Date().getTime(), manve.getMessage());
33
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
34
+        for(FieldError fe : fieldErrors) {
35
+
36
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
37
+            if(validationErrorList == null) {
38
+                validationErrorList = new ArrayList<>();
39
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
40
+            }
41
+            ValidationError validationError = new ValidationError();
42
+            validationError.setCode(fe.getCode());
43
+            validationError.setMessage(messageSource.getMessage(fe,null));
44
+            validationErrorList.add(validationError);
45
+        }
46
+        return new ResponseEntity<>(errorDetail, HttpStatus.BAD_REQUEST);
47
+    }
48
+}

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/OptionRepository.java Visa fil

@@ -0,0 +1,9 @@
1
+package io.zipcoder.tc_spring_poll_application.repository;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+@Repository
8
+interface OptionRepository extends CrudRepository<Option, Long> {
9
+}

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/PollRepository.java Visa fil

@@ -0,0 +1,9 @@
1
+package io.zipcoder.tc_spring_poll_application.repository;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+@Repository
8
+public interface PollRepository extends CrudRepository<Poll, Long> {
9
+}

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/VoteRepository.java Visa fil

@@ -0,0 +1,15 @@
1
+package io.zipcoder.tc_spring_poll_application.repository;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
5
+import org.springframework.data.repository.CrudRepository;
6
+import org.springframework.stereotype.Repository;
7
+
8
+@Repository
9
+public interface VoteRepository extends CrudRepository<Vote, Long> {
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
+}

+ 2
- 0
src/resources/messages.properties Visa fil

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