|
|
@@ -0,0 +1,46 @@
|
|
|
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.http.HttpHeaders;
|
|
|
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 javax.inject.Inject;
|
|
|
12
|
+import java.net.URI;
|
|
|
13
|
+
|
|
|
14
|
+@RestController
|
|
|
15
|
+public class PollController {
|
|
|
16
|
+ @Inject
|
|
|
17
|
+ private PollRepository pollRepository;
|
|
|
18
|
+
|
|
|
19
|
+ @RequestMapping(value ="/polls", method= RequestMethod.GET)
|
|
|
20
|
+ public ResponseEntity<Iterable<Poll>> getAllPolls(){
|
|
|
21
|
+ Iterable<Poll> allPolls = pollRepository.findAll();
|
|
|
22
|
+ return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ @RequestMapping(value ="/polls", method= RequestMethod.POST)
|
|
|
26
|
+ public ResponseEntity<?> createPoll(@RequestBody Poll poll){
|
|
|
27
|
+ poll = pollRepository.save(poll);
|
|
|
28
|
+ URI newPollUri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(poll.getId()).toUri();
|
|
|
29
|
+ return new ResponseEntity<>(new HttpHeaders(), HttpStatus.CREATED);
|
|
|
30
|
+ }
|
|
|
31
|
+ @RequestMapping(value="/polls/{pollId}", method= RequestMethod.GET)
|
|
|
32
|
+ public ResponseEntity<?> getPoll(@PathVariable Long pollId){
|
|
|
33
|
+ Poll poll = pollRepository.findOne(pollId);
|
|
|
34
|
+ return new ResponseEntity<>(poll, HttpStatus.OK);
|
|
|
35
|
+ }
|
|
|
36
|
+ @RequestMapping(value="/polls/{pollId}", method= RequestMethod.PUT)
|
|
|
37
|
+ public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId){
|
|
|
38
|
+ Poll p = pollRepository.save(poll);
|
|
|
39
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
40
|
+ }
|
|
|
41
|
+ @RequestMapping(value="/polls/{pollId}", method= RequestMethod.DELETE)
|
|
|
42
|
+ public ResponseEntity<?> deletePoll(@PathVariable Long pollId){
|
|
|
43
|
+ pollRepository.delete(pollId);
|
|
|
44
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
45
|
+ }
|
|
|
46
|
+}
|