Browse Source

finally got my Post Vote to work without 500 error, the problem was inconsistent spelling of option and options

Keith Brinker 8 years ago
parent
commit
5828e0a052

+ 14
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
3 4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.springframework.http.HttpHeaders;
@@ -34,7 +35,14 @@ public class PollController {
34 35
                 .buildAndExpand(poll.getId())
35 36
                 .toUri();
36 37
         responseHeaders.setLocation(newPollUri);
38
+
39
+//        for (Option option: poll.getOption()){
40
+//            System.out.println(option.toString());
41
+//        }
42
+
37 43
         return new ResponseEntity<>(null,responseHeaders, HttpStatus.CREATED);
44
+
45
+
38 46
     }
39 47
 
40 48
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
@@ -50,4 +58,10 @@ public class PollController {
50 58
         return new ResponseEntity<>(HttpStatus.OK);
51 59
     }
52 60
 
61
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
62
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
63
+        Poll p = pollRepository.findOne(pollId);
64
+        return new ResponseEntity<> (p, HttpStatus.OK);
65
+    }
66
+
53 67
 }

+ 31
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java View File

@@ -0,0 +1,31 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
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.*;
9
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10
+
11
+import javax.inject.Inject;
12
+
13
+@RestController
14
+public class VoteController {
15
+
16
+        @Inject
17
+        private VoteRepository voteRepository;
18
+
19
+        @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
20
+        public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
21
+                vote) {
22
+            vote = voteRepository.save(vote);
23
+            // Set the headers for the newly created resource
24
+            HttpHeaders responseHeaders = new HttpHeaders();
25
+            responseHeaders.setLocation(ServletUriComponentsBuilder.
26
+                    fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
27
+            return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
28
+        }
29
+    }
30
+
31
+

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java View File

@@ -10,17 +10,17 @@ public class Option {
10 10
     @Id
11 11
     @GeneratedValue
12 12
     @Column(name  = "OPTION_ID")
13
-            Long id;
13
+            private Long id;
14 14
 
15 15
     @Column(name = "OPTION_VALUE")
16
-            String value;
16
+           private String value;
17 17
 
18 18
 
19 19
     public Long getId() {
20 20
         return id;
21 21
     }
22 22
 
23
-    public void setId(long id) {
23
+    public void setId(Long id) {
24 24
         this.id = id;
25 25
     }
26 26
 

+ 6
- 6
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

@@ -9,22 +9,22 @@ public class Poll {
9 9
         @Id
10 10
         @GeneratedValue
11 11
         @Column(name  = "POLL_ID")
12
-        long id;
12
+        private Long id;
13 13
 
14 14
         @Column(name = "QUESTION")
15
-        String question;
15
+        private String question;
16 16
 
17 17
 
18 18
         @OneToMany(cascade = CascadeType.ALL)
19 19
         @JoinColumn(name = "POLL_ID")
20 20
         @OrderBy
21
-        Set<Option> option;
21
+        private Set<Option> options;
22 22
 
23 23
     public long getId() {
24 24
         return id;
25 25
     }
26 26
 
27
-    public void setId(long id) {
27
+    public void setId(Long id) {
28 28
         this.id = id;
29 29
     }
30 30
 
@@ -37,11 +37,11 @@ public class Poll {
37 37
     }
38 38
 
39 39
     public Set<Option> getOption() {
40
-        return option;
40
+        return options;
41 41
     }
42 42
 
43 43
     public void setOption(Set<Option> option) {
44
-        this.option = option;
44
+        this.options = option;
45 45
     }
46 46
 }
47 47
 

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java View File

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