|
|
@@ -3,14 +3,17 @@ package io.zipcoder.tc_spring_poll_application.controller;
|
|
3
|
3
|
|
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
5
|
5
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
|
6
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
7
|
+import org.springframework.context.annotation.Bean;
|
|
|
8
|
+import org.springframework.http.HttpHeaders;
|
|
6
|
9
|
import org.springframework.http.HttpStatus;
|
|
7
|
10
|
import org.springframework.http.ResponseEntity;
|
|
8
|
|
-import org.springframework.web.bind.annotation.InitBinder;
|
|
9
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
10
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
11
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
11
|
+import org.springframework.web.bind.annotation.*;
|
|
|
12
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
12
|
13
|
|
|
13
|
14
|
import javax.inject.Inject;
|
|
|
15
|
+import javax.validation.Valid;
|
|
|
16
|
+import java.net.URI;
|
|
14
|
17
|
|
|
15
|
18
|
@RestController
|
|
16
|
19
|
public class PollController {
|
|
|
@@ -18,8 +21,51 @@ public class PollController {
|
|
18
|
21
|
@Inject
|
|
19
|
22
|
private PollRepository pollRepository;
|
|
20
|
23
|
|
|
21
|
|
- @RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
|
24
|
+ @RequestMapping(value = "/polls", method = RequestMethod.GET)
|
|
22
|
25
|
public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
23
|
26
|
Iterable<Poll> allPolls = pollRepository.findAll();
|
|
24
|
27
|
return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
25
|
28
|
}
|
|
|
29
|
+
|
|
|
30
|
+
|
|
|
31
|
+ @RequestMapping(value = "/polls", method = RequestMethod.POST)
|
|
|
32
|
+ public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
|
|
|
33
|
+ poll = pollRepository.save(poll);
|
|
|
34
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
35
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
|
36
|
+ .fromCurrentRequest()
|
|
|
37
|
+ .path("/{id}")
|
|
|
38
|
+ .buildAndExpand(poll.getId())
|
|
|
39
|
+ .toUri();
|
|
|
40
|
+ httpHeaders.setLocation(newPollUri);
|
|
|
41
|
+ return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
|
45
|
+ public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
46
|
+ Poll p = pollRepository.findOne(pollId);
|
|
|
47
|
+ return new ResponseEntity<> (p, HttpStatus.OK);
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
|
51
|
+ public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
52
|
+ // Save the entity
|
|
|
53
|
+ Poll p = pollRepository.save(poll);
|
|
|
54
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
|
58
|
+ public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
59
|
+ pollRepository.delete(pollId);
|
|
|
60
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ public void verifyPoll(Long pollId) throws ResourceNotFoundException {
|
|
|
64
|
+ Poll p = pollRepository.findOne(pollId);
|
|
|
65
|
+ if (p == null) {
|
|
|
66
|
+ throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
|
|
|
67
|
+ }
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+
|
|
|
71
|
+}
|