|
@@ -4,12 +4,15 @@ import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
5
|
5
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
6
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
7
|
+import org.springframework.data.domain.Page;
|
|
8
|
+import org.springframework.data.domain.Pageable;
|
7
|
9
|
import org.springframework.http.HttpHeaders;
|
8
|
10
|
import org.springframework.http.HttpStatus;
|
9
|
11
|
import org.springframework.http.ResponseEntity;
|
10
|
12
|
import org.springframework.web.bind.annotation.*;
|
11
|
13
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
12
|
14
|
|
|
15
|
+import javax.validation.Valid;
|
13
|
16
|
import java.net.URI;
|
14
|
17
|
|
15
|
18
|
@RestController
|
|
@@ -18,19 +21,19 @@ public class PollController {
|
18
|
21
|
private PollRepository pollRepository;
|
19
|
22
|
|
20
|
23
|
@Autowired
|
21
|
|
- public PollController(PollRepository p) {
|
22
|
|
- this.pollRepository = p;
|
|
24
|
+ public PollController(PollRepository pollRepository) {
|
|
25
|
+ this.pollRepository = pollRepository;
|
23
|
26
|
|
24
|
27
|
}
|
25
|
28
|
|
26
|
29
|
@RequestMapping(value="/polls", method= RequestMethod.GET)
|
27
|
|
- public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
28
|
|
- Iterable<Poll> allPolls = pollRepository.findAll();
|
|
30
|
+ public ResponseEntity<Page<Poll>> getAllPolls(Pageable pageable) {
|
|
31
|
+ Page<Poll> allPolls = pollRepository.findAll(pageable);
|
29
|
32
|
return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
30
|
33
|
}
|
31
|
34
|
|
32
|
35
|
@RequestMapping(value="/polls", method=RequestMethod.POST)
|
33
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
36
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
|
34
|
37
|
poll = pollRepository.save(poll);
|
35
|
38
|
|
36
|
39
|
URI newPollUri = ServletUriComponentsBuilder
|
|
@@ -52,7 +55,7 @@ public class PollController {
|
52
|
55
|
}
|
53
|
56
|
|
54
|
57
|
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
55
|
|
- public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
58
|
+ public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
|
56
|
59
|
// Save the entity
|
57
|
60
|
Poll p = pollRepository.save(poll);
|
58
|
61
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
@@ -67,6 +70,6 @@ public class PollController {
|
67
|
70
|
public void verifyPoll(Long id)
|
68
|
71
|
{
|
69
|
72
|
if (!pollRepository.exists(id))
|
70
|
|
- throw new ResourceNotFoundException();
|
|
73
|
+ throw new ResourceNotFoundException("Doesn't Exist");
|
71
|
74
|
}
|
72
|
75
|
}
|