|
@@ -0,0 +1,44 @@
|
|
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.HttpHeaders;
|
|
7
|
+import org.springframework.http.HttpStatus;
|
|
8
|
+import org.springframework.http.ResponseEntity;
|
|
9
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
10
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
11
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
12
|
+import org.springframework.web.bind.annotation.RestController;
|
|
13
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
14
|
+import java.net.URI;
|
|
15
|
+
|
|
16
|
+@RestController
|
|
17
|
+public class PollController {
|
|
18
|
+
|
|
19
|
+ private PollRepository pollRepository;
|
|
20
|
+
|
|
21
|
+ @Autowired
|
|
22
|
+ public PollController(PollRepository pollRepository) {
|
|
23
|
+ this.pollRepository = pollRepository;
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ @RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
27
|
+ public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
28
|
+ Iterable<Poll> allPolls = pollRepository.findAll();
|
|
29
|
+ return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
33
|
+ public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
|
|
34
|
+ poll = pollRepository.save(poll);
|
|
35
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
36
|
+ .fromCurrentRequest()
|
|
37
|
+ .path("/{id}")
|
|
38
|
+ .buildAndExpand(poll.getId())
|
|
39
|
+ .toUri();
|
|
40
|
+ HttpHeaders header = new HttpHeaders().setLocation(newPollUri);
|
|
41
|
+ return new ResponseEntity<>(null, HttpStatus.CREATED);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+}
|