|
@@ -4,11 +4,15 @@ import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.domain.repositories.PollRepository;
|
5
|
5
|
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
6
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
7
|
+import org.springframework.data.domain.Page;
|
|
8
|
+import org.springframework.data.domain.PageRequest;
|
|
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
|
/*
|
|
@@ -24,57 +28,116 @@ Create a `PollController` class in the `controller` sub package.
|
24
|
28
|
public class PollController {
|
25
|
29
|
|
26
|
30
|
|
27
|
|
- PollRepository pollRepository;
|
|
31
|
+ private PollRepository pollRepository;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+ // @Autowired
|
|
35
|
+// public PollController(PollRepository pollRepository1) {
|
|
36
|
+// this.pollRepository = pollRepository1;
|
|
37
|
+//
|
|
38
|
+// }
|
|
39
|
+//
|
|
40
|
+// @RequestMapping(value = "/polls", method = RequestMethod.GET)
|
|
41
|
+// public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
42
|
+// Iterable<Poll> allPolls = pollRepository.findAll();
|
|
43
|
+// return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
44
|
+// }
|
|
45
|
+//
|
|
46
|
+// @RequestMapping(value = "/polls", method = RequestMethod.POST)
|
|
47
|
+//
|
|
48
|
+// public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
49
|
+// poll = pollRepository.save(poll);
|
|
50
|
+//
|
|
51
|
+// URI newPollUri = ServletUriComponentsBuilder
|
|
52
|
+// .fromCurrentRequest()
|
|
53
|
+// .path("/{id}")
|
|
54
|
+// .buildAndExpand(poll.getId())
|
|
55
|
+// .toUri();
|
|
56
|
+//
|
|
57
|
+// return new ResponseEntity<>(null, HttpStatus.CREATED);
|
|
58
|
+// }
|
|
59
|
+//
|
|
60
|
+// @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
|
|
61
|
+// public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
62
|
+// Poll p = pollRepository.findOne(pollId);
|
|
63
|
+// return new ResponseEntity<>(p, HttpStatus.OK);
|
|
64
|
+// }
|
|
65
|
+//
|
|
66
|
+// @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
|
|
67
|
+// public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
68
|
+// // Save the entity
|
|
69
|
+// Poll p = pollRepository.save(poll);
|
|
70
|
+// return new ResponseEntity<>(HttpStatus.OK);
|
|
71
|
+// }
|
|
72
|
+//
|
|
73
|
+// @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
|
|
74
|
+// public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
75
|
+// pollRepository.delete(pollId);
|
|
76
|
+// return new ResponseEntity<>(HttpStatus.OK);
|
|
77
|
+// }
|
|
78
|
+//
|
|
79
|
+//// public void verifypoll() throws ResourceNotFoundException {
|
|
80
|
+//// throw new ResourceNotFoundException();
|
|
81
|
+//// }
|
|
82
|
+//}
|
|
83
|
+ public void pollExists(Long pollId) throws ResourceNotFoundException {
|
|
84
|
+ if (pollRepository.findOne(pollId) == null) {
|
|
85
|
+ throw new ResourceNotFoundException("The poll specified does not exist.");
|
|
86
|
+ }
|
28
|
87
|
|
|
88
|
+ }
|
29
|
89
|
|
30
|
90
|
@Autowired
|
31
|
|
- public PollController(PollRepository pollRepository1) {
|
32
|
|
- this.pollRepository = pollRepository1;
|
33
|
|
-
|
|
91
|
+ public PollController(PollRepository pollRepository) {
|
|
92
|
+ this.pollRepository = pollRepository;
|
34
|
93
|
}
|
35
|
94
|
|
36
|
|
- @RequestMapping(value = "/polls", method = RequestMethod.GET)
|
37
|
|
- public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
95
|
+ @GetMapping("/polls")
|
|
96
|
+ public ResponseEntity<Iterable<Poll>> getPolls() {
|
38
|
97
|
Iterable<Poll> allPolls = pollRepository.findAll();
|
39
|
98
|
return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
40
|
99
|
}
|
41
|
100
|
|
42
|
|
- @RequestMapping(value = "/polls", method = RequestMethod.POST)
|
43
|
|
-
|
44
|
|
- public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
45
|
|
- poll = pollRepository.save(poll);
|
46
|
|
-
|
47
|
|
- URI newPollUri = ServletUriComponentsBuilder
|
48
|
|
- .fromCurrentRequest()
|
49
|
|
- .path("/{id}")
|
50
|
|
- .buildAndExpand(poll.getId())
|
51
|
|
- .toUri();
|
52
|
|
-
|
53
|
|
- return new ResponseEntity<>(null, HttpStatus.CREATED);
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
- @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
|
57
|
|
- public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
58
|
|
- Poll p = pollRepository.findOne(pollId);
|
59
|
|
- return new ResponseEntity<>(p, HttpStatus.OK);
|
60
|
|
- }
|
|
101
|
+ @GetMapping("/polls/{size}/{pageNum}")
|
|
102
|
+ public ResponseEntity<Page<Poll>> getPollsPaged(@PathVariable int size, @PathVariable int pageNum) {
|
61
|
103
|
|
62
|
|
- @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
|
63
|
|
- public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
64
|
|
- // Save the entity
|
65
|
|
- Poll p = pollRepository.save(poll);
|
66
|
|
- return new ResponseEntity<>(HttpStatus.OK);
|
67
|
|
- }
|
|
104
|
+ PageRequest pageRequest = new PageRequest(pageNum, size);
|
|
105
|
+ Page<Poll> page = pollRepository.findAll(pageRequest);
|
68
|
106
|
|
69
|
|
- @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
|
70
|
|
- public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
71
|
|
- pollRepository.delete(pollId);
|
72
|
|
- return new ResponseEntity<>(HttpStatus.OK);
|
|
107
|
+ return new ResponseEntity<>(page, HttpStatus.OK);
|
73
|
108
|
}
|
74
|
|
-
|
75
|
|
- public void verifypoll() throws ResourceNotFoundException {
|
76
|
|
- throw new ResourceNotFoundException();
|
|
109
|
+ @PostMapping("/polls")
|
|
110
|
+ @Valid
|
|
111
|
+ public ResponseEntity<?> createPoll (@RequestBody Poll poll){
|
|
112
|
+ poll = pollRepository.save(poll);
|
|
113
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
114
|
+ httpHeaders.setLocation(ServletUriComponentsBuilder
|
|
115
|
+ .fromCurrentRequest()
|
|
116
|
+ .path("/{id}")
|
|
117
|
+ .buildAndExpand(poll.getId())
|
|
118
|
+ .toUri());
|
|
119
|
+ return new ResponseEntity<>(null, HttpStatus.CREATED);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @GetMapping("/polls/{pollId}")
|
|
123
|
+ public ResponseEntity<?> getPoll (@PathVariable Long pollId){
|
|
124
|
+ Poll p = pollRepository.findOne(pollId);
|
|
125
|
+ return new ResponseEntity<>(p, HttpStatus.OK);
|
|
126
|
+
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ @PutMapping("/polls/{pollId}")
|
|
130
|
+ @Valid
|
|
131
|
+ public ResponseEntity<?> updatePoll (@RequestBody Poll poll, @PathVariable Long pollId){
|
|
132
|
+ Poll p = pollRepository.save(poll);
|
|
133
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ @DeleteMapping("/polls/{pollId}")
|
|
137
|
+ public ResponseEntity<?> deletePoll (@PathVariable Long pollId){
|
|
138
|
+ pollRepository.delete(pollId);
|
|
139
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
140
|
+ }
|
77
|
141
|
}
|
78
|
|
-}
|
79
|
142
|
|
80
|
143
|
|