소스 검색

Delete a post

vvmk 8 년 전
부모
커밋
38f0431741

+ 18
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java 파일 보기

29
         this.pollRepository = pollRepository;
29
         this.pollRepository = pollRepository;
30
     }
30
     }
31
 
31
 
32
-    @RequestMapping(name = "/polls", method = RequestMethod.GET)
32
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
33
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
33
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
34
         Iterable<Poll> allPolls = pollRepository.findAll();
34
         Iterable<Poll> allPolls = pollRepository.findAll();
35
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
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
     public ResponseEntity<Poll> createPoll(@RequestBody Poll poll) {
39
     public ResponseEntity<Poll> createPoll(@RequestBody Poll poll) {
40
         Poll createdPoll = pollRepository.save(poll);
40
         Poll createdPoll = pollRepository.save(poll);
41
 
41
 
61
         return new ResponseEntity<>(createdPoll, headers, HttpStatus.CREATED);
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
     public ResponseEntity<Poll> getPoll(@PathVariable Long pollId) {
65
     public ResponseEntity<Poll> getPoll(@PathVariable Long pollId) {
66
         Optional<Poll> poll = pollRepository.findById(pollId);
66
         Optional<Poll> poll = pollRepository.findById(pollId);
67
         return new ResponseEntity<>(poll.orElse(new Poll()), HttpStatus.OK);
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 파일 보기

47
         // GET one poll
47
         // GET one poll
48
         when(pollRepo.findById(anyLong())).thenReturn(Optional.of(new Poll()));
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
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
52
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
52
 
53
 
53
 //        when(pollRepo::save)
54
 //        when(pollRepo::save)
106
         verify(pollRepo).findById(anyLong());
107
         verify(pollRepo).findById(anyLong());
107
         assertEquals(expected, actual);
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
 }