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