|
|
@@ -2,13 +2,15 @@ package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
4
|
4
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
|
5
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
6
|
+import org.springframework.http.HttpHeaders;
|
|
5
|
7
|
import org.springframework.http.HttpStatus;
|
|
6
|
8
|
import org.springframework.http.ResponseEntity;
|
|
7
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
8
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
9
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
9
|
+import org.springframework.web.bind.annotation.*;
|
|
|
10
|
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
|
10
|
11
|
|
|
11
|
12
|
import javax.inject.Inject;
|
|
|
13
|
+import java.net.URI;
|
|
12
|
14
|
|
|
13
|
15
|
@RestController
|
|
14
|
16
|
public class PollController {
|
|
|
@@ -16,10 +18,49 @@ public class PollController {
|
|
16
|
18
|
@Inject
|
|
17
|
19
|
private PollRepository pollRepository;
|
|
18
|
20
|
|
|
|
21
|
+ @Autowired //The @Autowired annotation allows you to skip configurations elsewhere of what to inject and just does it for you
|
|
|
22
|
+
|
|
|
23
|
+ public PollController(PollRepository pollRepository){
|
|
|
24
|
+ this.pollRepository = pollRepository;
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
19
|
27
|
@RequestMapping(value="/polls", method= RequestMethod.GET)
|
|
20
|
28
|
public ResponseEntity<Iterable<Poll>> getAllPolls() {
|
|
21
|
29
|
Iterable<Poll> allPolls = pollRepository.findAll();
|
|
22
|
30
|
return new ResponseEntity<>(allPolls, HttpStatus.OK);
|
|
23
|
31
|
}
|
|
24
|
32
|
|
|
|
33
|
+ @RequestMapping(value="/polls", method=RequestMethod.POST)
|
|
|
34
|
+ public ResponseEntity<?> createPoll(@RequestBody Poll poll) { //@RequestBody tells Spring that the entire request body needs to be converted to an instance of Poll
|
|
|
35
|
+ poll = pollRepository.save(poll);
|
|
|
36
|
+ HttpHeaders newHeaders = new HttpHeaders();
|
|
|
37
|
+
|
|
|
38
|
+ URI newPollUri = ServletUriComponentsBuilder
|
|
|
39
|
+ .fromCurrentRequest()
|
|
|
40
|
+ .path("/{id}")
|
|
|
41
|
+ .buildAndExpand(poll.getId())
|
|
|
42
|
+ .toUri();
|
|
|
43
|
+ newHeaders.setLocation(newPollUri);
|
|
|
44
|
+ return new ResponseEntity<>(newHeaders, HttpStatus.CREATED);
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
|
|
|
48
|
+ public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
|
|
|
49
|
+ Poll p = pollRepository.findOne(pollId);
|
|
|
50
|
+ return new ResponseEntity<> (p, HttpStatus.OK);
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
|
|
|
54
|
+ public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
|
|
|
55
|
+ // Save the entity
|
|
|
56
|
+ Poll p = pollRepository.save(poll);
|
|
|
57
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
|
|
|
61
|
+ public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
|
|
|
62
|
+ pollRepository.delete(pollId);
|
|
|
63
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
25
|
66
|
}
|