|
|
@@ -1,5 +1,8 @@
|
|
1
|
1
|
package quickpoll.io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
4
|
+import org.springframework.data.domain.Page;
|
|
|
5
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
3
|
6
|
import quickpoll.io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
4
|
7
|
import quickpoll.io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
5
|
8
|
import quickpoll.io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
|
@@ -8,10 +11,14 @@ 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;
|
|
|
14
|
+import quickpoll.io.zipcoder.tc_spring_poll_application.service.PollService;
|
|
11
|
15
|
|
|
12
|
16
|
import javax.inject.Inject;
|
|
|
17
|
+import javax.servlet.http.HttpServletResponse;
|
|
13
|
18
|
import javax.validation.Valid;
|
|
|
19
|
+import javax.xml.ws.Service;
|
|
14
|
20
|
import java.net.URI;
|
|
|
21
|
+import java.util.List;
|
|
15
|
22
|
|
|
16
|
23
|
|
|
17
|
24
|
@RestController
|
|
|
@@ -19,6 +26,11 @@ public class PollController {
|
|
19
|
26
|
@Inject
|
|
20
|
27
|
private PollRepository pollRepository;
|
|
21
|
28
|
|
|
|
29
|
+ @Inject
|
|
|
30
|
+ private PollService service;
|
|
|
31
|
+
|
|
|
32
|
+
|
|
|
33
|
+
|
|
22
|
34
|
@RequestMapping(value="/polls", method = RequestMethod.GET)
|
|
23
|
35
|
public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
24
|
36
|
Iterable<Poll> allPolls = pollRepository.findAll();
|
|
|
@@ -36,6 +48,18 @@ public class PollController {
|
|
36
|
48
|
return new ResponseEntity<>(header, HttpStatus.CREATED);
|
|
37
|
49
|
}
|
|
38
|
50
|
|
|
|
51
|
+ @RequestMapping(value = "/polls", params = {"page", "size"}, method = RequestMethod.GET)
|
|
|
52
|
+ @ResponseBody
|
|
|
53
|
+ public List<Poll> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size, UriComponentsBuilder uriBuilder, HttpServletResponse response) {
|
|
|
54
|
+ Page<Poll> resultPage = service.findPaginated(page,size);
|
|
|
55
|
+ if(page> resultPage.getTotalPages()){
|
|
|
56
|
+ throw new ResourceNotFoundException();
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+
|
|
|
60
|
+ return resultPage.getContent();
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
39
|
63
|
@RequestMapping(value="/polls/{pollId}", method = RequestMethod.GET)
|
|
40
|
64
|
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
41
|
65
|
verifyPoll(pollId);
|