浏览代码

finished up to 3.2

Lauren Green 5 年前
父节点
当前提交
c803979a7a

+ 0
- 9
pom.xml 查看文件

@@ -13,15 +13,6 @@
13 13
         <version>1.5.3.RELEASE</version>
14 14
         <relativePath/> <!-- lookup parent from repository -->
15 15
     </parent>
16
-    <build>
17
-        <plugins>
18
-            <!-- Spring Boot Maven -->
19
-            <plugin>
20
-                <groupId>org.springframework.boot</groupId>
21
-                <artifactId>spring-boot-maven-plugin</artifactId>
22
-            </plugin>
23
-        </plugins>
24
-    </build>
25 16
     <properties>
26 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27 18
         <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>

+ 38
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

@@ -3,11 +3,13 @@ package io.zipcoder.tc_spring_poll_application.controller;
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 4
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
7 8
 import org.springframework.http.ResponseEntity;
8
-import org.springframework.web.bind.annotation.RequestMapping;
9
-import org.springframework.web.bind.annotation.RequestMethod;
10
-import org.springframework.web.bind.annotation.RestController;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
11 13
 
12 14
 @RestController
13 15
 public class PollController {
@@ -24,4 +26,37 @@ public class PollController {
24 26
         Iterable<Poll> allPolls = pollRepository.findAll();
25 27
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
26 28
     }
29
+
30
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
31
+    public ResponseEntity<HttpHeaders> 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
+        HttpHeaders loc = new HttpHeaders();
39
+        loc.setLocation(newPollUri);
40
+        return new ResponseEntity<>(loc, HttpStatus.CREATED);
41
+    }
42
+
43
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
44
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
45
+        Poll p = pollRepository.findOne(pollId);
46
+        return new ResponseEntity<> (p, HttpStatus.OK);
47
+    }
48
+
49
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
50
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
51
+        Poll p = pollRepository.save(poll);
52
+        return new ResponseEntity<>(HttpStatus.OK);
53
+    }
54
+
55
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
57
+        pollRepository.delete(pollId);
58
+        return new ResponseEntity<>(HttpStatus.OK);
59
+    }
60
+
61
+
27 62
 }

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java 查看文件

@@ -11,9 +11,9 @@ public class Option {
11 11
     @Id
12 12
     @GeneratedValue
13 13
     @Column(name = "OPTION_ID")
14
-    private long id;
14
+    public long id;
15 15
     @Column(name = "OPTION_VALUE")
16
-    private String value;
16
+    public String value;
17 17
 
18 18
     public long getId() {
19 19
         return id;

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java 查看文件

@@ -9,13 +9,13 @@ public class Poll {
9 9
     @Id
10 10
     @GeneratedValue
11 11
     @Column(name = "POLL_ID")
12
-    private long id;
12
+    public long id;
13 13
     @Column(name = "QUESTION")
14
-    private String question;
14
+    public String question;
15 15
     @OneToMany(cascade = CascadeType.ALL)
16 16
     @JoinColumn(name = "POLL_ID")
17 17
     @OrderBy
18
-    private Set<Option> options;
18
+    public Set<Option> options;
19 19
 
20 20
     public long getId() {
21 21
         return id;

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java 查看文件

@@ -8,10 +8,10 @@ public class Vote {
8 8
     @Id
9 9
     @GeneratedValue
10 10
     @Column(name = "VOTE_ID")
11
-    private long id;
11
+    public long id;
12 12
     @ManyToOne
13
-    @JoinColumn(name="P{TION_ID")
14
-    Option option;
13
+    @JoinColumn(name="OPTION_ID")
14
+    public Option option;
15 15
 
16 16
     public long getId() {
17 17
         return id;