Kaynağa Gözat

up to 3.1.4

Keith Brinker 8 yıl önce
ebeveyn
işleme
5dc48ce00c

+ 43
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Dosyayı Görüntüle

@@ -0,0 +1,43 @@
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.RequestBody;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RestController;
12
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
13
+
14
+import javax.inject.Inject;
15
+import java.net.URI;
16
+
17
+@RestController
18
+public class PollController {
19
+
20
+    @Inject
21
+    PollRepository pollRepository;
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
+        HttpHeaders responseHeaders = new HttpHeaders();
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{id}")
37
+                .buildAndExpand(poll.getId())
38
+                .toUri();
39
+        responseHeaders.setLocation(newPollUri);
40
+        return new ResponseEntity<>(null,responseHeaders, HttpStatus.CREATED);
41
+    }
42
+
43
+}