Parcourir la source

Delete a post

vvmk il y a 8 ans
Parent
révision
38f0431741

+ 18
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java Voir le fichier

@@ -29,13 +29,13 @@ public class PollController {
29 29
         this.pollRepository = pollRepository;
30 30
     }
31 31
 
32
-    @RequestMapping(name = "/polls", method = RequestMethod.GET)
32
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
33 33
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
34 34
         Iterable<Poll> allPolls = pollRepository.findAll();
35 35
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
36 36
     }
37 37
 
38
-    @RequestMapping(name = "/polls", method = RequestMethod.POST)
38
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
39 39
     public ResponseEntity<Poll> createPoll(@RequestBody Poll poll) {
40 40
         Poll createdPoll = pollRepository.save(poll);
41 41
 
@@ -61,9 +61,24 @@ public class PollController {
61 61
         return new ResponseEntity<>(createdPoll, headers, HttpStatus.CREATED);
62 62
     }
63 63
 
64
-    @RequestMapping(name = "/polls/{pollId}", method = RequestMethod.GET)
64
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
65 65
     public ResponseEntity<Poll> getPoll(@PathVariable Long pollId) {
66 66
         Optional<Poll> poll = pollRepository.findById(pollId);
67 67
         return new ResponseEntity<>(poll.orElse(new Poll()), HttpStatus.OK);
68 68
     }
69
+
70
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
71
+    public ResponseEntity<Poll> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
72
+        //TODO: check ids
73
+        //if (pollId != poll.getId())
74
+        //return BAD_REQUEST
75
+
76
+        return new ResponseEntity<>(pollRepository.save(poll), HttpStatus.OK);
77
+    }
78
+
79
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
80
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
81
+        pollRepository.deleteById(pollId);
82
+        return new ResponseEntity<>(null, HttpStatus.OK);
83
+    }
69 84
 }

+ 20
- 1
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/PollControllerTest.java Voir le fichier

@@ -47,7 +47,8 @@ public class PollControllerTest {
47 47
         // GET one poll
48 48
         when(pollRepo.findById(anyLong())).thenReturn(Optional.of(new Poll()));
49 49
 
50
-        //POST create poll
50
+        // POST create poll
51
+        // PUT update poll
51 52
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
52 53
 
53 54
 //        when(pollRepo::save)
@@ -106,4 +107,22 @@ public class PollControllerTest {
106 107
         verify(pollRepo).findById(anyLong());
107 108
         assertEquals(expected, actual);
108 109
     }
110
+
111
+    @Test
112
+    public void updatesPoll() {
113
+        HttpStatus expected = HttpStatus.OK;
114
+        HttpStatus actual = pollCtrl.updatePoll(mock(Poll.class), anyLong()).getStatusCode();
115
+
116
+        verify(pollRepo).save(any(Poll.class));
117
+        assertEquals(expected, actual);
118
+    }
119
+
120
+    @Test
121
+    public void deletePoll() {
122
+        HttpStatus expected = HttpStatus.OK;
123
+        HttpStatus actual = pollCtrl.deletePoll(anyLong()).getStatusCode();
124
+
125
+        verify(pollRepo).deleteById(anyLong());
126
+        assertEquals(expected, actual);
127
+    }
109 128
 }