Parcourir la source

Merge cefd0a95522f5b2e028102662838add27b68a8ac into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

Jessica Campbell il y a 8 ans
Parent
révision
1031c599a1
Aucun compte lié à l'adresse email de l'auteur
18 fichiers modifiés avec 552 ajouts et 0 suppressions
  1. 16
    0
      src/main/Resources/import.sql
  2. 2
    0
      src/main/Resources/messages.properties
  3. 22
    0
      src/main/java/dtos/OptionCount.java
  4. 22
    0
      src/main/java/dtos/VoteResult.java
  5. 46
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Controller/ComputeResultController.java
  6. 71
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Controller/PollController.java
  7. 40
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Controller/VoteController.java
  8. 37
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java
  9. 52
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java
  10. 36
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Vote.java
  11. 69
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/ErrorDetail.java
  12. 67
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/RestExceptionHandler.java
  13. 25
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/ValidationError.java
  14. 18
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Exception/ResourceNotFoundException.java
  15. 1
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java
  16. 7
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/OptionRepository.java
  17. 8
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/PollRepository.java
  18. 13
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/VoteRepository.java

+ 16
- 0
src/main/Resources/import.sql Voir le fichier

@@ -0,0 +1,16 @@
1
+insert into poll (poll_id, question) values (1, 'Who is your favorite music artist?');
2
+insert into option (option_id, option_value, poll_id) values (1, 'Beyonce', 1);
3
+insert into option (option_id, option_value, poll_id) values (2, 'The Weeknd', 1);
4
+insert into option (option_id, option_value, poll_id) values (3, 'Drake', 1);
5
+insert into option (option_id, option_value, poll_id) values (4, 'Rihanna', 1);
6
+insert into option (option_id, option_value, poll_id) values (5, 'Ariana Grande', 1);
7
+insert into option (option_id, option_value, poll_id) values (6, 'Miguel', 1);
8
+insert into option (option_id, option_value, poll_id) values (7, 'Ed Sheeran', 1);
9
+insert into option (option_id, option_value, poll_id) values (8, 'Demi Lovato', 1);
10
+insert into option (option_id, option_value, poll_id) values (9, 'Selena Gomez', 1);
11
+insert into option (option_id, option_value, poll_id) values (10, 'Justin Bieber', 1);
12
+insert into option (option_id, option_value, poll_id) values (11, 'Bruno Mars', 1);
13
+insert into option (option_id, option_value, poll_id) values (12, 'Imagine Dragons', 1);
14
+insert into option (option_id, option_value, poll_id) values (13, 'Halsey', 1);
15
+insert into option (option_id, option_value, poll_id) values (14, 'J. Cole', 1);
16
+insert into option (option_id, option_value, poll_id) values (15, 'Kendrick Lamar', 1);

+ 2
- 0
src/main/Resources/messages.properties Voir le fichier

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

+ 22
- 0
src/main/java/dtos/OptionCount.java Voir le fichier

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

+ 22
- 0
src/main/java/dtos/VoteResult.java Voir le fichier

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

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/ComputeResultController.java Voir le fichier

@@ -0,0 +1,46 @@
1
+package io.zipcoder.tc_spring_poll_application.Controller;
2
+
3
+import dtos.OptionCount;
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
+import java.util.HashMap;
16
+import java.util.Map;
17
+
18
+@RestController
19
+public class ComputeResultController {
20
+    @Inject
21
+    private VoteRepository voteRepository;
22
+
23
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
24
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
25
+        VoteResult voteResult = new VoteResult();
26
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
27
+
28
+        //TODO: Implement algorithm to count votes
29
+
30
+        int counter = 0;
31
+        Map<Long, OptionCount> tempMap = new HashMap<>(); // create new hashmap that contains the long id and option count that contains long id and count
32
+        for (Vote v: allVotes) { // for every vote add to the counter
33
+            counter++;
34
+            OptionCount optionCount = tempMap.get(v.getOption().getId()); // assign the option count to the id of the vote's option
35
+            if(optionCount == null){
36
+                optionCount = new OptionCount(); // if we dont have that option then create it
37
+                optionCount.setOptionId(v.getOption().getId());
38
+                tempMap.put(v.getOption().getId(), optionCount);
39
+            }
40
+            optionCount.setCount(optionCount.getCount()+1); // then set the count
41
+        }
42
+        voteResult.setTotalVotes(counter); // set the total votes to the counter we created
43
+        voteResult.setResults(tempMap.values()); // get the values of the results
44
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
45
+    }
46
+}

+ 71
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/PollController.java Voir le fichier

@@ -0,0 +1,71 @@
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.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.inject.Inject;
14
+import javax.validation.Valid;
15
+import java.net.URI;
16
+
17
+@RestController
18
+public class PollController {
19
+    @Inject
20
+    PollRepository pollRepository;
21
+
22
+    @Autowired
23
+    public PollController() {
24
+        this.pollRepository = pollRepository;
25
+    }
26
+
27
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
28
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
29
+        Iterable<Poll> allPolls = pollRepository.findAll(); // reads all the polls using the poll repository
30
+        return new ResponseEntity<>(allPolls, HttpStatus.OK); // poll data becomes part of the response body & responds as OK (200)
31
+    }
32
+
33
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
34
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) { // ensures client has some way of knowing the URI of the newly created poll
35
+        poll = (Poll) pollRepository.save(poll);
36
+        URI newPollUri = ServletUriComponentsBuilder
37
+                .fromCurrentRequest()
38
+                .path("/{id}")
39
+                .buildAndExpand(poll.getId())
40
+                .toUri();
41
+        HttpHeaders httpHeaders = new HttpHeaders();
42
+        httpHeaders.setLocation(newPollUri);
43
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
44
+    }
45
+
46
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET) // enables us to access an individual poll
47
+    public ResponseEntity<?> getPoll(@PathVariable Long 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(@Valid @RequestBody Poll poll, @PathVariable Long pollId) { // enables us to update a poll
54
+        // Save the entity
55
+        Poll p = pollRepository.save(poll);
56
+        return new ResponseEntity<>(HttpStatus.OK);
57
+    }
58
+
59
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE) // enables us to delete a poll
60
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        pollRepository.delete(pollId);
62
+        return new ResponseEntity<>(HttpStatus.OK);
63
+    }
64
+
65
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException {
66
+        Poll p = pollRepository.findOne(pollId);
67
+        if (p == null) {
68
+            throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
69
+        }
70
+    }
71
+}

+ 40
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/VoteController.java Voir le fichier

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

+ 37
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java Voir le fichier

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

+ 52
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java Voir le fichier

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

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Vote.java Voir le fichier

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

+ 69
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/ErrorDetail.java Voir le fichier

@@ -0,0 +1,69 @@
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(){
16
+        this.detail = detail;
17
+        this.developerMessage = developerMessage;
18
+        this.status = status;
19
+        this.title = title;
20
+        this.timeStamp = timeStamp;
21
+        this.errors = errors;
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
+    public Map<String, List<ValidationError>> getErrors() {
63
+        return errors;
64
+    }
65
+
66
+    public void setErrors(Map<String, List<ValidationError>> errors) {
67
+        this.errors = errors;
68
+    }
69
+}

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/RestExceptionHandler.java Voir le fichier

@@ -0,0 +1,67 @@
1
+package io.zipcoder.tc_spring_poll_application.Dto.Error;
2
+
3
+import io.zipcoder.tc_spring_poll_application.Exception.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
+
12
+import javax.inject.Inject;
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
+    private final MessageSource messageSource;
21
+
22
+    @Inject
23
+    public RestExceptionHandler(MessageSource messageSource){
24
+        this.messageSource = messageSource;
25
+    }
26
+
27
+    @ExceptionHandler(ResourceNotFoundException.class)
28
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
29
+
30
+        ErrorDetail errorDetail = new ErrorDetail();
31
+        errorDetail.setTimeStamp(new Date().getTime());
32
+        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
33
+        errorDetail.setTitle("Resource Not Found");
34
+        errorDetail.setDetail(rnfe.getMessage());
35
+        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
36
+
37
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
38
+    }
39
+
40
+    @ExceptionHandler(MethodArgumentNotValidException.class)
41
+    public ResponseEntity<?>
42
+    handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
43
+
44
+        ErrorDetail errorDetail = new ErrorDetail();
45
+        errorDetail.setTitle("Bad Request");
46
+        errorDetail.setDetail(manve.getMessage());
47
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
48
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
49
+        errorDetail.setTimeStamp(new Date().getTime());
50
+
51
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
52
+        for (FieldError fe : fieldErrors) {
53
+
54
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
55
+            if (validationErrorList == null) {
56
+                validationErrorList = new ArrayList<>();
57
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
58
+            }
59
+            ValidationError validationError = new ValidationError();
60
+            validationError.setCode(fe.getCode());
61
+            validationError.setMessage(messageSource.getMessage(fe, null));
62
+            validationErrorList.add(validationError);
63
+        }
64
+        return new ResponseEntity<>(errorDetail, HttpStatus.BAD_REQUEST);
65
+    }
66
+
67
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Dto/Error/ValidationError.java Voir le fichier

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.Dto.Error;
2
+
3
+public class ValidationError {
4
+    String code;
5
+    String message;
6
+
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
+
24
+
25
+}

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Exception/ResourceNotFoundException.java Voir le fichier

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

+ 1
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java Voir le fichier

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.tc_spring_poll_application;
2 2
 
3
+
3 4
 import org.springframework.boot.SpringApplication;
4 5
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 6
 

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/OptionRepository.java Voir le fichier

@@ -0,0 +1,7 @@
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
+
6
+public interface OptionRepository extends CrudRepository<Option, Long> {
7
+}

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/PollRepository.java Voir le fichier

@@ -0,0 +1,8 @@
1
+package io.zipcoder.tc_spring_poll_application.Repositories;
2
+
3
+import com.sun.xml.internal.bind.v2.model.core.ID;
4
+import io.zipcoder.tc_spring_poll_application.Domain.Poll;
5
+import org.springframework.data.repository.CrudRepository;
6
+
7
+public interface PollRepository extends CrudRepository<Poll, Long> {
8
+}

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/VoteRepository.java Voir le fichier

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