Katrice Williams-Dredden 8 years ago
parent
commit
7117284d14

+ 2
- 2
pom.xml View File

@@ -10,12 +10,12 @@
10 10
     <parent>
11 11
         <groupId>org.springframework.boot</groupId>
12 12
         <artifactId>spring-boot-starter-parent</artifactId>
13
-        <version>1.5.3.RELEASE</version>
13
+        <version>2.0.0.RELEASE</version>
14 14
         <relativePath/> <!-- lookup parent from repository -->
15 15
     </parent>
16 16
     <properties>
17 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
-        <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
18
+        <start-class>io.zipcoder.springdemo.QuickPollApplication</start-class>
19 19
         <java.version>1.7</java.version>
20 20
     </properties>
21 21
     <dependencies>

+ 51
- 5
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

@@ -3,14 +3,17 @@ package io.zipcoder.tc_spring_poll_application.controller;
3 3
 
4 4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
5 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.context.annotation.Bean;
8
+import org.springframework.http.HttpHeaders;
6 9
 import org.springframework.http.HttpStatus;
7 10
 import org.springframework.http.ResponseEntity;
8
-import org.springframework.web.bind.annotation.InitBinder;
9
-import org.springframework.web.bind.annotation.RequestMapping;
10
-import org.springframework.web.bind.annotation.RequestMethod;
11
-import org.springframework.web.bind.annotation.RestController;
11
+import org.springframework.web.bind.annotation.*;
12
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 13
 
13 14
 import javax.inject.Inject;
15
+import javax.validation.Valid;
16
+import java.net.URI;
14 17
 
15 18
 @RestController
16 19
 public class PollController {
@@ -18,8 +21,51 @@ public class PollController {
18 21
     @Inject
19 22
     private PollRepository pollRepository;
20 23
 
21
-    @RequestMapping(value="/polls", method= RequestMethod.GET)
24
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
22 25
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
23 26
         Iterable<Poll> allPolls = pollRepository.findAll();
24 27
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
25 28
     }
29
+
30
+
31
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
32
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
33
+        poll = pollRepository.save(poll);
34
+        HttpHeaders httpHeaders = new HttpHeaders();
35
+        URI newPollUri = ServletUriComponentsBuilder
36
+                .fromCurrentRequest()
37
+                .path("/{id}")
38
+                .buildAndExpand(poll.getId())
39
+                .toUri();
40
+        httpHeaders.setLocation(newPollUri);
41
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
42
+    }
43
+
44
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
45
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
+        Poll p = pollRepository.findOne(pollId);
47
+        return new ResponseEntity<> (p, HttpStatus.OK);
48
+    }
49
+
50
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52
+        // Save the entity
53
+        Poll p = pollRepository.save(poll);
54
+        return new ResponseEntity<>(HttpStatus.OK);
55
+    }
56
+
57
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
58
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
59
+        pollRepository.delete(pollId);
60
+        return new ResponseEntity<>(HttpStatus.OK);
61
+    }
62
+
63
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException {
64
+        Poll p = pollRepository.findOne(pollId);
65
+        if (p == null) {
66
+            throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
67
+        }
68
+    }
69
+
70
+
71
+}

+ 6
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ResourceNotFoundException.java View File

@@ -0,0 +1,6 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+public class ResourceNotFoundException extends Throwable {
4
+    public ResourceNotFoundException(String s) {
5
+    }
6
+}

+ 3
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java View File

@@ -5,4 +5,7 @@ import org.springframework.data.repository.CrudRepository;
5 5
 
6 6
 public interface PollRepository extends CrudRepository<Poll, Long> {
7 7
 
8
+    Poll findOne(Long pollId);
9
+
10
+    void delete(Long pollId);
8 11
 }