Mitch Taylor 8 лет назад
Родитель
Сommit
8318b2e08c
2 измененных файлов: 41 добавлений и 1 удалений
  1. 1
    1
      README.md
  2. 40
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java

+ 1
- 1
README.md Просмотреть файл

132
 
132
 
133
 ### Part 3.1.2 - Testing via Postman
133
 ### Part 3.1.2 - Testing via Postman
134
 
134
 
135
-* Ensure that the `start-class` tag in your `pom.xml` encapsulates `io.zipcoder.springdemo.QuickPollApplication`
135
+* Ensure that the `start-class` tag in your `pom.xml` encapsulates `io.zipcoder.tc_spring_poll_application.QuickPollApplication`
136
 * Open a command line and navigate to the project's root directory and run this command:
136
 * Open a command line and navigate to the project's root directory and run this command:
137
 	* `mvn spring-boot:run`
137
 	* `mvn spring-boot:run`
138
 * Launch the [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) app and enter the URI `http://localhost:8080/polls` and hit Send.
138
 * Launch the [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) app and enter the URI `http://localhost:8080/polls` and hit Send.

+ 40
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

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
+    private PollRepository pollRepository;
22
+
23
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
24
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
25
+        Iterable<Poll> allPolls = pollRepository.findAll();
26
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
27
+    }
28
+
29
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
30
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
31
+        poll = pollRepository.save(poll);
32
+        URI newPollUri = ServletUriComponentsBuilder
33
+                .fromCurrentRequest()
34
+                .path("/{id}")
35
+                .buildAndExpand(poll.getId())
36
+                .toUri();
37
+        return new ResponseEntity<>(new HttpHeaders().setLocation(newPollUri), HttpStatus.CREATED);
38
+    }
39
+
40
+}