Demetrius Murray 5 years ago
parent
commit
b45577cb66
18 changed files with 658 additions and 0 deletions
  1. 12
    0
      pom.xml
  2. 59
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/RestExceptionHandler.java
  3. 78
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java
  4. 82
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java
  5. 67
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java
  6. 34
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java
  7. 50
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java
  8. 32
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java
  9. 22
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java
  10. 23
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java
  11. 64
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java
  12. 23
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java
  13. 18
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java
  14. 8
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java
  15. 9
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java
  16. 16
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java
  17. 59
    0
      src/main/resources/import.sql
  18. 2
    0
      src/main/resources/messages.properties

+ 12
- 0
pom.xml View File

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>spring-demo</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <parent>
11 23
         <groupId>org.springframework.boot</groupId>
12 24
         <artifactId>spring-boot-starter-parent</artifactId>

+ 59
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/RestExceptionHandler.java View File

@@ -0,0 +1,59 @@
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.beans.factory.annotation.Autowired;
7
+import org.springframework.context.MessageSource;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.validation.FieldError;
11
+import org.springframework.web.bind.MethodArgumentNotValidException;
12
+import org.springframework.web.bind.annotation.ControllerAdvice;
13
+import org.springframework.web.bind.annotation.ExceptionHandler;
14
+
15
+import javax.servlet.http.HttpServletRequest;
16
+import java.util.ArrayList;
17
+import java.util.Date;
18
+import java.util.List;
19
+
20
+@ControllerAdvice
21
+public class RestExceptionHandler {
22
+
23
+    @Autowired
24
+    MessageSource messageSource;
25
+
26
+
27
+    @ExceptionHandler(ResourceNotFoundException.class)
28
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
29
+        ErrorDetail ed = new ErrorDetail();
30
+        ed.setTimestamp(new Date().getTime());
31
+        ed.setDeveloperMessage(rnfe.getMessage());
32
+        ed.setDetail(rnfe.getCause().getMessage());
33
+
34
+        return new ResponseEntity<>(ed, HttpStatus.NOT_FOUND);
35
+    }
36
+
37
+    @ExceptionHandler(MethodArgumentNotValidException.class)
38
+    public ResponseEntity<?>
39
+    handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
40
+        ErrorDetail errorDetail = new ErrorDetail();
41
+        errorDetail.setTimestamp(new Date().getTime());
42
+        errorDetail.setDeveloperMessage(manve.getMessage());
43
+        errorDetail.setDetail(manve.getCause().getMessage());
44
+
45
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
46
+        for(FieldError fe : fieldErrors) {
47
+            List<ValidationError> validationErrorList = errorDetail.getErrors().computeIfAbsent(fe.getField(), k -> new ArrayList<>());
48
+
49
+            ValidationError validationError = new ValidationError();
50
+            validationError.setCode(fe.getCode());
51
+            validationError.setMessage(messageSource.getMessage(fe, null));
52
+            validationErrorList.add(validationError);
53
+        }
54
+
55
+        return new ResponseEntity<>(errorDetail,HttpStatus.BAD_REQUEST);
56
+    }
57
+
58
+
59
+}

+ 78
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java View File

@@ -0,0 +1,78 @@
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.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
6
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
7
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
8
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
9
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
10
+import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.http.HttpStatus;
12
+import org.springframework.http.ResponseEntity;
13
+import org.springframework.web.bind.annotation.RequestMapping;
14
+import org.springframework.web.bind.annotation.RequestMethod;
15
+import org.springframework.web.bind.annotation.RequestParam;
16
+import org.springframework.web.bind.annotation.RestController;
17
+
18
+import java.util.ArrayList;
19
+import java.util.Collection;
20
+import java.util.Iterator;
21
+
22
+@RestController
23
+public class ComputeResultController {
24
+
25
+    private VoteRepository voteRepository;
26
+    private PollRepository pollRepository;
27
+
28
+    @Autowired
29
+    public ComputeResultController(VoteRepository voteRepository, PollRepository pollRepository) {
30
+        this.voteRepository = voteRepository;
31
+        this.pollRepository = pollRepository;
32
+    }
33
+
34
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
35
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
36
+        verifyPoll(pollId);
37
+        VoteResult voteResult = new VoteResult();
38
+        voteResult.setResults(new ArrayList<>());
39
+        Collection<OptionCount> results = voteResult.getResults();
40
+
41
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
42
+        Iterator<Vote> voteIterator = allVotes.iterator();
43
+
44
+        while (voteIterator.hasNext()){
45
+            Vote vote = voteIterator.next();
46
+            if (results.size() == 0) {
47
+                results.add(newOptionCount(vote));
48
+            }
49
+
50
+            else {
51
+                boolean added = false;
52
+                for (OptionCount oc : results) {
53
+                    if (oc.getOptionId() == vote.getOption().getId()) {
54
+                        oc.setCount(oc.getCount() + 1);
55
+                        added = true;
56
+                        break;
57
+                    }
58
+                }
59
+                if (!added) results.add(newOptionCount(vote));
60
+            }
61
+        }
62
+
63
+        return new ResponseEntity<>(results, HttpStatus.OK);
64
+    }
65
+
66
+    private OptionCount newOptionCount(Vote vote) {
67
+        OptionCount oc = new OptionCount();
68
+        oc.setCount(1);
69
+        oc.setoptionId(vote.getOption().getId());
70
+        return oc;
71
+    }
72
+
73
+    public void verifyPoll(Long pollId){
74
+        Poll p = pollRepository.findOne(pollId);
75
+        if (p == null) throw new ResourceNotFoundException();
76
+    }
77
+
78
+}

+ 82
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

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

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java View File

@@ -0,0 +1,67 @@
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.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
6
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
7
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.data.domain.Page;
10
+import org.springframework.data.domain.PageRequest;
11
+import org.springframework.http.HttpHeaders;
12
+import org.springframework.http.HttpStatus;
13
+import org.springframework.http.ResponseEntity;
14
+import org.springframework.web.bind.annotation.*;
15
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
16
+
17
+import java.net.URI;
18
+
19
+@RestController
20
+public class VoteController {
21
+    VoteRepository voteRepository;
22
+    PollRepository pollRepository;
23
+
24
+    @Autowired
25
+    public VoteController(VoteRepository voteRepository, PollRepository pollRepository){
26
+        this.voteRepository = voteRepository;
27
+        this.pollRepository = pollRepository;
28
+    }
29
+
30
+    @RequestMapping(value = "/polls/votes", method = RequestMethod.GET)
31
+    public Iterable<Vote> getAllVotes(){
32
+        return voteRepository.findAll();
33
+    }
34
+
35
+    @RequestMapping(value = "/polls/votes", params = { "page", "size" }, method = RequestMethod.GET)
36
+    public Iterable<Vote> getPaginatedVotes(@RequestParam("page") int page, @RequestParam("size") int size) {
37
+        return voteRepository.findAll(new PageRequest(page,size));
38
+    }
39
+
40
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.GET)
41
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
42
+        verifyPoll(pollId);
43
+        return voteRepository.findVotesByPoll(pollId);
44
+    }
45
+
46
+    @RequestMapping(value = "/polls/{pollId}/votes", method= RequestMethod.POST)
47
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote vote){
48
+        verifyPoll(pollId);
49
+        vote = voteRepository.save(vote);
50
+
51
+        URI newVoteUri = ServletUriComponentsBuilder
52
+                .fromCurrentRequest()
53
+                .path("/{id}")
54
+                .buildAndExpand(vote.getId())
55
+                .toUri();
56
+
57
+        HttpHeaders responseHeaders = new HttpHeaders();
58
+        responseHeaders.setLocation(newVoteUri);
59
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
60
+    }
61
+
62
+    public void verifyPoll(Long pollId){
63
+        Poll p = pollRepository.findOne(pollId);
64
+        if (p == null) throw new ResourceNotFoundException();
65
+    }
66
+
67
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java View File

@@ -0,0 +1,34 @@
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
+    @GeneratedValue
12
+    @Column(name = "OPTION_ID")
13
+    long id;
14
+
15
+    @Column(name = "OPTION_VALUE")
16
+    String value;
17
+
18
+    public long getId() {
19
+        return id;
20
+    }
21
+
22
+    public void setId(long id) {
23
+        this.id = id;
24
+    }
25
+
26
+    public String getValue() {
27
+        return value;
28
+    }
29
+
30
+    public void setValue(String value) {
31
+        this.value = value;
32
+    }
33
+
34
+}

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

@@ -0,0 +1,50 @@
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
+    long id;
16
+
17
+    @Column(name = "QUESTION")
18
+    @NotEmpty
19
+    String question;
20
+
21
+    @OneToMany(cascade = CascadeType.ALL)
22
+    @JoinColumn(name = "POLL_ID")
23
+    @OrderBy
24
+    @Size(min=2, max=6)
25
+    Set<Option> options;
26
+
27
+    public long getId() {
28
+        return id;
29
+    }
30
+
31
+    public void setId(long id) {
32
+        this.id = id;
33
+    }
34
+
35
+    public String getQuestion() {
36
+        return question;
37
+    }
38
+
39
+    public void setQuestion(String question) {
40
+        this.question = question;
41
+    }
42
+
43
+    public Set<Option> getOptions() {
44
+        return options;
45
+    }
46
+
47
+    public void setOptions(Set<Option> options) {
48
+        this.options = options;
49
+    }
50
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java View File

@@ -0,0 +1,32 @@
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
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
15
+    Option option;
16
+
17
+    public long getId() {
18
+        return id;
19
+    }
20
+
21
+    public void setId(long id) {
22
+        this.id = id;
23
+    }
24
+
25
+    public Option getOption() {
26
+        return option;
27
+    }
28
+
29
+    public void setOption(Option option) {
30
+        this.option = option;
31
+    }
32
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java View File

@@ -0,0 +1,22 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
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/dtos/VoteResult.java View File

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

+ 64
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java View File

@@ -0,0 +1,64 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+
6
+public class ErrorDetail {
7
+    String title;
8
+    int status;
9
+    String detail;
10
+    long timestamp;
11
+    String developerMessage;
12
+
13
+    public Map<String, List<ValidationError>> getErrors() {
14
+        return errors;
15
+    }
16
+
17
+    public void setErrors(Map<String, List<ValidationError>> errors) {
18
+        this.errors = errors;
19
+    }
20
+
21
+    Map<String, List<ValidationError>> errors;
22
+
23
+
24
+
25
+    public String getTitle() {
26
+        return title;
27
+    }
28
+
29
+    public void setTitle(String title) {
30
+        this.title = title;
31
+    }
32
+
33
+    public int getStatus() {
34
+        return status;
35
+    }
36
+
37
+    public void setStatus(int status) {
38
+        this.status = status;
39
+    }
40
+
41
+    public String getDetail() {
42
+        return detail;
43
+    }
44
+
45
+    public void setDetail(String detail) {
46
+        this.detail = detail;
47
+    }
48
+
49
+    public long getTimestamp() {
50
+        return timestamp;
51
+    }
52
+
53
+    public void setTimestamp(long timestamp) {
54
+        this.timestamp = timestamp;
55
+    }
56
+
57
+    public String getDeveloperMessage() {
58
+        return developerMessage;
59
+    }
60
+
61
+    public void setDeveloperMessage(String developerMessage) {
62
+        this.developerMessage = developerMessage;
63
+    }
64
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java View File

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

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java View File

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java View File

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

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java View File

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

+ 16
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java View File

@@ -0,0 +1,16 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
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.data.repository.PagingAndSortingRepository;
7
+
8
+public interface VoteRepository extends PagingAndSortingRepository<Vote, Long> {
9
+    Iterable<Vote> findById(Long pollId);
10
+
11
+    @Query(value = "SELECT v.* " +
12
+            "FROM Option o, Vote v " +
13
+            "WHERE o.POLL_ID = ?1 " +
14
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
15
+    public Iterable<Vote> findVotesByPoll(Long pollId);
16
+}

+ 59
- 0
src/main/resources/import.sql View File

@@ -0,0 +1,59 @@
1
+insert into poll (poll_id, question) values (1, 'What is your favorite color?');
2
+insert into poll (poll_id, question) values (2, 'What is your favorite movie');
3
+insert into poll (poll_id, question) values (3, 'What generation are you');
4
+insert into poll (poll_id, question) values (4, 'Whats your favorite meal of day?');
5
+insert into poll (poll_id, question) values (5, 'Whats your favorite device');
6
+insert into poll (poll_id, question) values (6, 'Whats your favorite source of entertainment');
7
+insert into poll (poll_id, question) values (7, 'How many siblings do you have');
8
+insert into poll (poll_id, question) values (8, 'What is your favorite language');
9
+insert into poll (poll_id, question) values (9, 'Favorite program language');
10
+insert into poll (poll_id, question) values (11, 'What is your favorite color?');
11
+insert into poll (poll_id, question) values (12, 'What is your favorite color?');
12
+insert into poll (poll_id, question) values (13, 'What is your favorite color?');
13
+insert into poll (poll_id, question) values (14, 'What is your favorite color?');
14
+insert into poll (poll_id, question) values (15, 'What is your favorite color?');
15
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
16
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
17
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
18
+insert into option (option_id, option_value, poll_id) values (4, '300', 2);
19
+insert into option (option_id, option_value, poll_id) values (5, 'Titanic', 2);
20
+insert into option (option_id, option_value, poll_id) values (6, 'UP', 2);
21
+insert into option (option_id, option_value, poll_id) values (7, 'Millennial', 3);
22
+insert into option (option_id, option_value, poll_id) values (8, 'Gen-X', 3);
23
+insert into option (option_id, option_value, poll_id) values (9, 'Gen-Z', 3);
24
+insert into option (option_id, option_value, poll_id) values (10, 'Breakfast', 4);
25
+insert into option (option_id, option_value, poll_id) values (11, 'Lunch', 4);
26
+insert into option (option_id, option_value, poll_id) values (12, 'Dinner', 4);
27
+insert into option (option_id, option_value, poll_id) values (13, 'Mobile', 5);
28
+insert into option (option_id, option_value, poll_id) values (14, 'Desktop', 5);
29
+insert into option (option_id, option_value, poll_id) values (15, 'Tablet', 5);
30
+insert into option (option_id, option_value, poll_id) values (16, 'Books', 6);
31
+insert into option (option_id, option_value, poll_id) values (17, 'TV', 6);
32
+insert into option (option_id, option_value, poll_id) values (18, 'Adventure', 6);
33
+insert into option (option_id, option_value, poll_id) values (19, '0', 7);
34
+insert into option (option_id, option_value, poll_id) values (20, '1 to 2', 7);
35
+insert into option (option_id, option_value, poll_id) values (21, '3 or more', 7);
36
+insert into option (option_id, option_value, poll_id) values (22, 'Spanish', 8);
37
+insert into option (option_id, option_value, poll_id) values (23, 'French', 8);
38
+insert into option (option_id, option_value, poll_id) values (24, 'English', 8);
39
+insert into option (option_id, option_value, poll_id) values (25, 'Java', 9);
40
+insert into option (option_id, option_value, poll_id) values (26, 'Python', 9);
41
+insert into option (option_id, option_value, poll_id) values (27, 'JavaScript', 9);
42
+insert into option (option_id, option_value, poll_id) values (28, 'Red', 10);
43
+insert into option (option_id, option_value, poll_id) values (29, 'Blue', 10);
44
+insert into option (option_id, option_value, poll_id) values (30, 'Yellow', 10);
45
+insert into option (option_id, option_value, poll_id) values (31, 'Red', 11);
46
+insert into option (option_id, option_value, poll_id) values (32, 'Blue', 11);
47
+insert into option (option_id, option_value, poll_id) values (33, 'Yellow', 11);
48
+insert into option (option_id, option_value, poll_id) values (34, 'Red', 12);
49
+insert into option (option_id, option_value, poll_id) values (35, 'Blue', 12);
50
+insert into option (option_id, option_value, poll_id) values (36, 'Yellow', 12);
51
+insert into option (option_id, option_value, poll_id) values (37, 'Red', 13);
52
+insert into option (option_id, option_value, poll_id) values (38, 'Blue', 13);
53
+insert into option (option_id, option_value, poll_id) values (39, 'Yellow', 13);
54
+insert into option (option_id, option_value, poll_id) values (40, 'Red', 14);
55
+insert into option (option_id, option_value, poll_id) values (41, 'Blue', 14);
56
+insert into option (option_id, option_value, poll_id) values (42, 'Yellow', 14);
57
+insert into option (option_id, option_value, poll_id) values (43, 'Red', 15);
58
+insert into option (option_id, option_value, poll_id) values (44, 'Blue', 15);
59
+insert into option (option_id, option_value, poll_id) values (45, 'Yellow', 15);

+ 2
- 0
src/main/resources/messages.properties View File

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