Nick Satinover 5 vuotta sitten
vanhempi
commit
e0c3e844e5

+ 24
- 7
pom.xml Näytä tiedosto

@@ -7,17 +7,28 @@
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>
13
-        <version>1.5.3.RELEASE</version>
25
+        <version>2.0.4.RELEASE</version>
14 26
         <relativePath/> <!-- lookup parent from repository -->
15 27
     </parent>
16 28
     <properties>
17 29
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
-        <start-class>io.zipcoder.springdemo.QuickPollApplication</start-class>
19
-        <!--<start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class> -->
20
-        <java.version>1.7</java.version>
30
+        <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
31
+        <java.version>1.8</java.version>
21 32
     </properties>
22 33
     <dependencies>
23 34
         <dependency>
@@ -34,8 +45,8 @@
34 45
             <scope>test</scope>
35 46
         </dependency>
36 47
         <dependency>
37
-            <groupId>org.hsqldb</groupId>
38
-            <artifactId>hsqldb</artifactId>
48
+            <groupId>com.h2database</groupId>
49
+            <artifactId>h2</artifactId>
39 50
             <scope>runtime</scope>
40 51
         </dependency>
41 52
 
@@ -44,7 +55,13 @@
44 55
             <artifactId>javax.inject</artifactId>
45 56
             <version>1</version>
46 57
         </dependency>
58
+        <!-- Jackson JSON Mapper -->
59
+        <dependency>
60
+            <groupId>org.codehaus.jackson</groupId>
61
+            <artifactId>jackson-mapper-asl</artifactId>
62
+            <version>1.9.7</version>
63
+        </dependency>
47 64
     </dependencies>
48 65
 
49 66
 
50
-</project>
67
+</project>

+ 66
- 0
src/main/java/dtos/ErrorDetail.java Näytä tiedosto

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

+ 22
- 0
src/main/java/dtos/OptionCount.java Näytä tiedosto

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

+ 57
- 0
src/main/java/dtos/RestExceptionHandler.java Näytä tiedosto

@@ -0,0 +1,57 @@
1
+package dtos;
2
+
3
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4
+import org.springframework.beans.factory.annotation.Autowired;
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
+import javax.servlet.http.HttpServletRequest;
13
+import java.util.ArrayList;
14
+import java.util.Date;
15
+import java.util.List;
16
+
17
+@ControllerAdvice
18
+public class RestExceptionHandler {
19
+
20
+    MessageSource messageSource;
21
+
22
+    RestExceptionHandler(@Autowired MessageSource messageSource){
23
+        this.messageSource = messageSource;
24
+    }
25
+
26
+    @ExceptionHandler(ResourceNotFoundException.class)
27
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request){
28
+        ErrorDetail detail = new ErrorDetail();
29
+        detail.setTitle(rnfe.getMessage());
30
+        detail.setStatus(rnfe.hashCode());
31
+        detail.setDetail(rnfe.getMessage());
32
+        detail.setDeveloperMessage(rnfe.getLocalizedMessage());
33
+        detail.setTimeStamp(new Date().getTime());
34
+        return new ResponseEntity<>(detail, HttpStatus.NOT_FOUND);
35
+    }
36
+
37
+    @ExceptionHandler(MethodArgumentNotValidException.class)
38
+    public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
39
+        ErrorDetail detail = new ErrorDetail();
40
+
41
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
42
+
43
+        for (FieldError f: fieldErrors) {
44
+
45
+            List<ValidationError> validationErrorList = detail.getErrors().get(f.getField());
46
+            if (validationErrorList == null) {
47
+                validationErrorList = new ArrayList<>();
48
+                detail.getErrors().put(f.getField(), validationErrorList);
49
+            }
50
+            ValidationError validationError = new ValidationError();
51
+            validationError.setCode(f.getCode());
52
+            validationError.setMessage(messageSource.getMessage(f, null));
53
+            validationErrorList.add(validationError);
54
+        }
55
+        return new ResponseEntity<>(detail, HttpStatus.BAD_REQUEST);
56
+    }
57
+}

+ 23
- 0
src/main/java/dtos/ValidationError.java Näytä tiedosto

@@ -0,0 +1,23 @@
1
+package dtos;
2
+
3
+public class ValidationError {
4
+
5
+    private String code;
6
+    private String message;
7
+
8
+    public String getCode() {
9
+        return code;
10
+    }
11
+
12
+    public void setCode(String code) {
13
+        this.code = code;
14
+    }
15
+
16
+    public String getMessage() {
17
+        return message;
18
+    }
19
+
20
+    public void setMessage(String message) {
21
+        this.message = message;
22
+    }
23
+}

+ 23
- 0
src/main/java/dtos/VoteResult.java Näytä tiedosto

@@ -0,0 +1,23 @@
1
+package dtos;
2
+import java.util.Collection;
3
+
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
+}

+ 42
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Näytä tiedosto

@@ -0,0 +1,42 @@
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.beans.factory.annotation.Autowired;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+
12
+import java.util.ArrayList;
13
+import java.util.Collection;
14
+
15
+@RestController
16
+public class ComputeResultController {
17
+
18
+    private VoteRepository voteRepository;
19
+
20
+    @Autowired
21
+    public ComputeResultController(VoteRepository voteRepository) {
22
+        this.voteRepository = voteRepository;
23
+    }
24
+
25
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
26
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
27
+        VoteResult voteResult = new VoteResult();
28
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
29
+//        Collection<OptionCount> countCollection = new H
30
+
31
+        int count = 0;
32
+        for (Vote v: allVotes) {
33
+            count++;
34
+//            countCollection.add(v);
35
+        }
36
+        voteResult.setTotalVotes(count);
37
+//        voteResult.setResults(allVotes);
38
+
39
+        //TODO: Implement algorithm to count votes
40
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
41
+    }
42
+}

+ 21
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/OptionsController.java Näytä tiedosto

@@ -0,0 +1,21 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.repositories.OptionRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.GetMapping;
9
+import org.springframework.web.bind.annotation.RestController;
10
+
11
+@RestController
12
+public class OptionsController {
13
+
14
+    @Autowired
15
+    OptionRepository optionRepository;
16
+
17
+    @GetMapping
18
+    public ResponseEntity<Iterable<Option>> index() {
19
+        return new ResponseEntity<>(optionRepository.findAll(), HttpStatus.OK);
20
+    }
21
+}

src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java → src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Näytä tiedosto

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

src/main/java/io/zipcoder/tc_spring_poll_application/controllers/VoteController.java → src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Näytä tiedosto

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

+ 1
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Näytä tiedosto

@@ -9,6 +9,7 @@ public class Option {
9 9
     @GeneratedValue(strategy = GenerationType.AUTO)
10 10
     @Column(name = "OPTION_ID")
11 11
     private Long id;
12
+
12 13
     @Column(name = "OPTION_VALUE")
13 14
     private String value;
14 15
 

+ 8
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Näytä tiedosto

@@ -1,6 +1,9 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3 3
 import javax.persistence.*;
4
+import javax.validation.constraints.NotEmpty;
5
+import javax.validation.constraints.Size;
6
+import java.util.List;
4 7
 import java.util.Set;
5 8
 
6 9
 @Entity
@@ -11,13 +14,14 @@ public class Poll {
11 14
     @Column(name = "POLL_ID")
12 15
     private Long id;
13 16
 
17
+    @NotEmpty
14 18
     @Column(name = "QUESTION")
15 19
     private String question;
16 20
 
21
+    @Size(min = 2, max = 6)
17 22
     @OneToMany(cascade = CascadeType.ALL)
18 23
     @JoinColumn(name = "POLL_ID")
19
-    @OrderBy
20
-    private Set<Option> options;
24
+    private List<Option> options;
21 25
 
22 26
     public Poll() {
23 27
     }
@@ -38,11 +42,11 @@ public class Poll {
38 42
         this.question = question;
39 43
     }
40 44
 
41
-    public Set<Option> getOptions() {
45
+    public List<Option> getOptions() {
42 46
         return options;
43 47
     }
44 48
 
45
-    public void setOptions(Set<Option> options) {
49
+    public void setOptions(List<Option> options) {
46 50
         this.options = options;
47 51
     }
48 52
 }

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Näytä tiedosto

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

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Näytä tiedosto

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

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Näytä tiedosto

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

+ 8
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Näytä tiedosto

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

+ 2
- 0
src/main/java/resources/import.sql Näytä tiedosto

@@ -0,0 +1,2 @@
1
+INSERT INTO poll (poll_id, question) VALUES (1, 'What is your favorite color?');
2
+INSERT INTO OPTION (option_id, option_value, poll_id) VALUES (1, 'Red', 1);

+ 2
- 0
src/main/java/resources/messages.properties Näytä tiedosto

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