Просмотр исходного кода

Merge d865a9530c8405383431adc6458204af1de26819 into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

KATRINAHIGH 8 лет назад
Родитель
Сommit
f9d05baf86
Аккаунт пользователя с таким Email не найден
17 измененных файлов: 613 добавлений и 1 удалений
  1. 23
    0
      src/main/java/dtos/OptionCount.java
  2. 25
    0
      src/main/java/dtos/VoteResult.java
  3. 24
    1
      src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java
  4. 46
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java
  5. 75
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java
  6. 48
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java
  7. 36
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java
  8. 58
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java
  9. 32
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java
  10. 85
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java
  11. 29
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java
  12. 21
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java
  13. 78
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java
  14. 8
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java
  15. 8
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java
  16. 15
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java
  17. 2
    0
      src/main/resources/messages.properties

+ 23
- 0
src/main/java/dtos/OptionCount.java Просмотреть файл

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

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

+ 24
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java Просмотреть файл

@@ -2,10 +2,33 @@ package io.zipcoder.tc_spring_poll_application;
2 2
 
3 3
 import org.springframework.boot.SpringApplication;
4 4
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+import org.springframework.context.annotation.ComponentScan;
5 6
 
6 7
 @SpringBootApplication
8
+@ComponentScan
7 9
 public class QuickPollApplication {
8 10
     public static void main(String[] args) {
9 11
         SpringApplication.run(QuickPollApplication.class, args);
10 12
     }
11
-}
13
+}
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Просмотреть файл

@@ -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
+
21
+    @Inject
22
+    private VoteRepository voteRepository;
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
+        //TODO: Implement algorithm to count votes
29
+        int totalVotes = 0;
30
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
31
+        for(Vote v : allVotes) {
32
+            totalVotes ++;
33
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
34
+            if(optionCount == null) {
35
+                optionCount = new OptionCount();
36
+                optionCount.setOptionId(v.getOption().getId());
37
+                tempMap.put(v.getOption().getId(), optionCount);
38
+            }
39
+            optionCount.setCount(optionCount.getCount()+1);
40
+        }
41
+        voteResult.setTotalVotes(totalVotes);
42
+        voteResult.setResults(tempMap.values());
43
+
44
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
45
+    }
46
+}

+ 75
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

@@ -0,0 +1,75 @@
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.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
+import javax.inject.Inject;
13
+import javax.validation.Valid;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    @Inject
20
+    private PollRepository pollRepository;
21
+
22
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
23
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
24
+        Iterable<Poll> allPolls = pollRepository.findAll();
25
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
26
+    }
27
+
28
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
29
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
30
+        poll = pollRepository.save(poll);
31
+        HttpHeaders httpHeaders = new HttpHeaders();
32
+        URI newPollUri = ServletUriComponentsBuilder
33
+                .fromCurrentRequest()
34
+                .path("/{id}")
35
+                .buildAndExpand(poll.getId())
36
+                .toUri();
37
+        httpHeaders.setLocation(newPollUri);
38
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
39
+    }
40
+
41
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
42
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
43
+        verifyPoll(pollId);
44
+        Poll p = pollRepository.findOne(pollId);
45
+        return new ResponseEntity<> (p, HttpStatus.OK);
46
+    }
47
+
48
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50
+        verifyPoll(pollId);
51
+        // Save the entity
52
+        Poll p = pollRepository.save(poll);
53
+        return new ResponseEntity<>(HttpStatus.OK);
54
+    }
55
+
56
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
57
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
+        verifyPoll(pollId);
59
+        pollRepository.delete(pollId);
60
+        return new ResponseEntity<>(HttpStatus.OK);
61
+    }
62
+
63
+    public void verifyPoll(@PathVariable Long pollId){
64
+       if(!pollRepository.exists(pollId)){
65
+           throw new ResourceNotFoundException("Poll with id does not exist");
66
+       }
67
+    }
68
+}
69
+
70
+//checks if a specific poll id exists and throws a
71
+//        ResourceNotFoundException if not. Use this in any
72
+//        method that searches for or updates an existing poll
73
+//        (eg: Get, Put, and Delete methods).
74
+
75
+

+ 48
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Просмотреть файл

@@ -0,0 +1,48 @@
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.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
+import javax.inject.Inject;
13
+
14
+@RestController
15
+public class VoteController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public VoteController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
25
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
26
+            vote) {
27
+        vote = voteRepository.save(vote);
28
+        HttpHeaders responseHeaders = new HttpHeaders();
29
+        responseHeaders.setLocation(
30
+                ServletUriComponentsBuilder.
31
+                        fromCurrentRequest().path("/{id}").
32
+                        buildAndExpand(vote.getId()).
33
+                        toUri());
34
+        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
35
+    }
36
+
37
+    @RequestMapping(value = "/polls/votes", method = RequestMethod.GET)
38
+    public Iterable<Vote> getAllVotes() {
39
+        return voteRepository.findAll();
40
+    }
41
+
42
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
43
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
44
+        return voteRepository.findVotesByPoll(pollId);
45
+    }
46
+
47
+}
48
+

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Просмотреть файл

@@ -0,0 +1,36 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.Id;
6
+import javax.persistence.Column;
7
+
8
+@Entity
9
+public class Option {
10
+
11
+    @Id  //denotes primary key of this entity
12
+    @GeneratedValue //configures increment of the specified column(field)
13
+    @Column(name = "OPTION_ID")  //specifies mapped column for a persistent property or field
14
+    private long id;
15
+    @Column(name = "OPTION_VALUE")
16
+    private 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
+
35
+
36
+

+ 58
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

@@ -0,0 +1,58 @@
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.HashSet;
8
+import java.util.Set;
9
+
10
+@Entity
11
+public class Poll {
12
+
13
+    @Id
14
+    @GeneratedValue
15
+    @Column(name = "POLL_ID")
16
+    private long id;
17
+
18
+    @Column(name = "QUESTION")
19
+    @NotEmpty
20
+    private String questions;
21
+
22
+    @OneToMany(cascade = CascadeType.ALL)
23
+    @JoinColumn(name = "POLL_ID")
24
+    @OrderBy
25
+    @Size(min=2, max = 6)
26
+    private Set<Option> options;
27
+
28
+    public long getId() {
29
+        return id;
30
+    }
31
+
32
+    public void setId(long id) {
33
+        this.id = id;
34
+    }
35
+
36
+    public String getQuestions() {
37
+        return questions;
38
+    }
39
+
40
+    public void setQuestions(String questions) {
41
+        this.questions = questions;
42
+    }
43
+
44
+    public Set<Option> getOptions() {
45
+        return options;
46
+    }
47
+
48
+    public void setOptions(Set<Option> options) {
49
+        this.options = options;
50
+    }
51
+}
52
+
53
+// In the Poll class:
54
+// options should be @Size(min=2, max = 6)
55
+//question should be @NotEmpty
56
+//To enforce these validations, add @Valid annotations to Poll objects in
57
+//RequestMapping-annotated controller methods
58
+// (there should be 2)...only found one check this!!!!!

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Просмотреть файл

@@ -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
+    private long id;
12
+
13
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
15
+    private 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
+}

+ 85
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Просмотреть файл

@@ -0,0 +1,85 @@
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
+    private String title;
9
+    private int status;
10
+    private String detail;
11
+    private long timeStamp;
12
+    private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
14
+
15
+    /**
16
+     *
17
+     * @return a brief title of the error condition, eg: "Validation Failure" or "Internal Server Error"
18
+     */
19
+    public String getTitle() {
20
+        return title;
21
+    }
22
+
23
+    public void setTitle(String title) {
24
+        this.title = title;
25
+    }
26
+
27
+    /**
28
+     *
29
+     * @return the HTTP status code for the current request; redundant but useful for client-side error handling
30
+     */
31
+    public int getStatus() {
32
+        return status;
33
+    }
34
+
35
+
36
+    public void setStatus(int status) {
37
+        this.status = status;
38
+    }
39
+
40
+    /**
41
+     *
42
+     * @return A short, human-readable description of the error that may be presented to a user
43
+     */
44
+    public String getDetail() {
45
+        return detail;
46
+    }
47
+
48
+    public void setDetail(String detail) {
49
+        this.detail = detail;
50
+    }
51
+
52
+    /**
53
+     *
54
+     * @return the time in milliseconds when the error occurred
55
+     */
56
+    public long getTimeStamp() {
57
+        return timeStamp;
58
+    }
59
+
60
+    public void setTimeStamp(long timeStamp) {
61
+        this.timeStamp = timeStamp;
62
+    }
63
+
64
+    /**
65
+     *
66
+     * @return detailed information such as exception class name or a stack trace useful for developers to debug
67
+     */
68
+    public String getDeveloperMessage() {
69
+        return developerMessage;
70
+    }
71
+
72
+    public void setDeveloperMessage(String developerMessage) {
73
+        this.developerMessage = developerMessage;
74
+    }
75
+
76
+
77
+    public Map<String, List<ValidationError>> getErrors() {
78
+        return errors;
79
+    }
80
+
81
+    public void setErrors(Map<String, List<ValidationError>> errors) {
82
+        this.errors = errors;
83
+    }
84
+}
85
+

+ 29
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java Просмотреть файл

@@ -0,0 +1,29 @@
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 ValidationError {
7
+
8
+    private String code;
9
+    private String message;
10
+
11
+    public String getCode() {
12
+        return code;
13
+    }
14
+
15
+    public void setCode(String code) {
16
+        this.code = code;
17
+    }
18
+
19
+    public String getMessage() {
20
+        return message;
21
+    }
22
+
23
+    public void setMessage(String message) {
24
+        this.message = message;
25
+    }
26
+
27
+}
28
+
29
+

+ 21
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Просмотреть файл

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

+ 78
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Просмотреть файл

@@ -0,0 +1,78 @@
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.context.MessageSource;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.validation.FieldError;
9
+import org.springframework.web.bind.MethodArgumentNotValidException;
10
+import org.springframework.web.bind.annotation.ControllerAdvice;
11
+import org.springframework.web.bind.annotation.ExceptionHandler;
12
+
13
+import javax.inject.Inject;
14
+import javax.servlet.http.HttpServletRequest;
15
+import java.util.ArrayList;
16
+import java.util.Date;
17
+import java.util.List;
18
+
19
+@ControllerAdvice
20
+public class RestExceptionHandler {
21
+
22
+    @Inject
23
+     private MessageSource messageSource;
24
+
25
+    @ExceptionHandler(ResourceNotFoundException.class)
26
+    public ResponseEntity<?> handleResourceNotFoundException(
27
+            ResourceNotFoundException rnfe,
28
+            HttpServletRequest request) {
29
+        ErrorDetail customError = new ErrorDetail();
30
+        customError.setDetail(rnfe.getMessage());
31
+        customError.setDeveloperMessage(rnfe.getStackTrace().toString());
32
+        customError.setStatus(404);
33
+        customError.setTimeStamp(new Date().getTime());
34
+        customError.setTitle("Resource Not Found");
35
+
36
+        return new ResponseEntity<>(customError, null, HttpStatus.NOT_FOUND);
37
+    }
38
+
39
+    @ExceptionHandler(MethodArgumentNotValidException.class)
40
+    public ResponseEntity<?> handleValidationError(
41
+            MethodArgumentNotValidException manve,
42
+            HttpServletRequest request){
43
+        ErrorDetail customError = new ErrorDetail();
44
+
45
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
46
+        for(FieldError fe : fieldErrors) {
47
+
48
+            List<ValidationError> validationErrorList = customError.getErrors().get(fe.getField());
49
+            if(validationErrorList == null) {
50
+                validationErrorList = new ArrayList<>();
51
+                customError.getErrors().put(fe.getField(), validationErrorList);
52
+            }
53
+            ValidationError validationError = new ValidationError();
54
+            validationError.setCode(fe.getCode());
55
+            validationError.setMessage(messageSource.getMessage(fe, null));
56
+            validationErrorList.add(validationError);
57
+        }
58
+
59
+        return new ResponseEntity<>(customError, null, HttpStatus.NOT_FOUND);
60
+        }
61
+}
62
+
63
+
64
+//Create RestExceptionHandler class annotated with @ControllerAdvice
65
+//Create a handler method with the header shown below
66
+// Populate an ErrorDetail object in the method, and
67
+//return a ResponseEntity containing the ErrorDetail and an HTTP NOT_FOUND status
68
+//Use java.util's new Date().getTime() for the timestamp
69
+//Provide the detail and developer messages from the ResourceNotFoundException
70
+
71
+
72
+//In this handler we need to do the following:
73
+//
74
+// Create the ErrorDetail object (similar to before)
75
+//  Get the list of field validation errors
76
+// For each field error, add it to the appropriate list in the ErrorDetail (see below)
77
+// Return a ResponseEntity containing the error detail and the appropriate HTTP status code (400 Bad Request)
78
+

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Просмотреть файл

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Просмотреть файл

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

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Просмотреть файл

@@ -0,0 +1,15 @@
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
+
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
+

+ 2
- 0
src/main/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}