|
|
@@ -2,14 +2,14 @@ package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
|
5
|
+import org.springframework.http.HttpHeaders;
|
|
5
|
6
|
import org.springframework.http.HttpStatus;
|
|
6
|
7
|
import org.springframework.http.ResponseEntity;
|
|
7
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
8
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
9
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
10
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
8
|
+import org.springframework.web.bind.annotation.*;
|
|
|
9
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
11
|
10
|
|
|
12
|
11
|
import javax.inject.Inject;
|
|
|
12
|
+import java.net.URI;
|
|
13
|
13
|
|
|
14
|
14
|
@RestController
|
|
15
|
15
|
public class PollController {
|
|
|
@@ -25,8 +25,34 @@ public class PollController {
|
|
25
|
25
|
|
|
26
|
26
|
@RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
27
|
27
|
public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
|
28
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
|
29
|
+ .fromCurrentRequest()
|
|
|
30
|
+ .path("/{id}")
|
|
|
31
|
+ .buildAndExpand(poll.getId())
|
|
|
32
|
+ .toUri();
|
|
|
33
|
+ HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
34
|
+ responseHeaders.setLocation(newPollUri);
|
|
28
|
35
|
poll = pollRepository.save(poll);
|
|
29
|
|
- return new ResponseEntity<>(null, HttpStatus.CREATED);
|
|
|
36
|
+ return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
|
40
|
+ public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
41
|
+ Poll p = pollRepository.findOne(pollId);
|
|
|
42
|
+ return new ResponseEntity<> (p, HttpStatus.OK);
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
|
46
|
+ public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
47
|
+ // Save the entity
|
|
|
48
|
+ Poll p = pollRepository.save(poll);
|
|
|
49
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
|
53
|
+ public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
54
|
+ pollRepository.delete(pollId);
|
|
|
55
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
30
|
56
|
}
|
|
31
|
57
|
|
|
32
|
58
|
}
|