diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 11:31:00 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-05-01 09:26:12 +0200 |
commit | a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a (patch) | |
tree | 4f4c3212fbcaee839e0fa4910b32a6f4f72f6c43 | |
parent | 1eca7831a69b9470b92dcc72e1ce51b42b291338 (diff) | |
download | wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.tar.gz wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.tar.zst wallabag-a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a.zip |
Splitted the endpoint in two
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 96 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 55 |
2 files changed, 88 insertions, 63 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 3833ce3c..0c98c242 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -173,68 +173,96 @@ class EntryRestController extends WallabagRestController | |||
173 | } | 173 | } |
174 | 174 | ||
175 | /** | 175 | /** |
176 | * Handles an entries list and create or remove URL. | 176 | * Handles an entries list and delete URL. |
177 | * | 177 | * |
178 | * @ApiDoc( | 178 | * @ApiDoc( |
179 | * parameters={ | 179 | * parameters={ |
180 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} | 180 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} |
181 | * } | 181 | * } |
182 | * ) | 182 | * ) |
183 | * | 183 | * |
184 | * @return JsonResponse | 184 | * @return JsonResponse |
185 | */ | 185 | */ |
186 | public function postEntriesListAction(Request $request) | 186 | public function deleteEntriesListAction(Request $request) |
187 | { | 187 | { |
188 | $this->validateAuthentication(); | 188 | $this->validateAuthentication(); |
189 | 189 | ||
190 | $list = json_decode($request->query->get('list', [])); | 190 | $urls = json_decode($request->query->get('urls', [])); |
191 | $results = []; | 191 | $results = []; |
192 | 192 | ||
193 | // handle multiple urls | 193 | // handle multiple urls |
194 | if (!empty($list)) { | 194 | if (!empty($urls)) { |
195 | $results = []; | 195 | $results = []; |
196 | foreach ($list as $key => $element) { | 196 | foreach ($urls as $key => $url) { |
197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | 197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
198 | $element->url, | 198 | $url, |
199 | $this->getUser()->getId() | 199 | $this->getUser()->getId() |
200 | ); | 200 | ); |
201 | 201 | ||
202 | $results[$key]['url'] = $element->url; | 202 | $results[$key]['url'] = $url; |
203 | $results[$key]['action'] = $element->action; | ||
204 | 203 | ||
205 | switch ($element->action) { | 204 | if (false !== $entry) { |
206 | case 'delete': | 205 | $em = $this->getDoctrine()->getManager(); |
207 | if (false !== $entry) { | 206 | $em->remove($entry); |
208 | $em = $this->getDoctrine()->getManager(); | 207 | $em->flush(); |
209 | $em->remove($entry); | ||
210 | $em->flush(); | ||
211 | 208 | ||
212 | // entry deleted, dispatch event about it! | 209 | // entry deleted, dispatch event about it! |
213 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | 210 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); |
214 | } | 211 | } |
215 | 212 | ||
216 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; | 213 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; |
214 | } | ||
215 | } | ||
217 | 216 | ||
218 | break; | 217 | $json = $this->get('serializer')->serialize($results, 'json'); |
219 | case 'add': | 218 | |
220 | if (false === $entry) { | 219 | return (new JsonResponse())->setJson($json); |
221 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | 220 | } |
222 | new Entry($this->getUser()), | 221 | |
223 | $element->url | 222 | /** |
224 | ); | 223 | * Handles an entries list and create URL. |
225 | } | 224 | * |
225 | * @ApiDoc( | ||
226 | * parameters={ | ||
227 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | ||
228 | * } | ||
229 | * ) | ||
230 | * | ||
231 | * @return JsonResponse | ||
232 | */ | ||
233 | public function postEntriesListAction(Request $request) | ||
234 | { | ||
235 | $this->validateAuthentication(); | ||
226 | 236 | ||
227 | $em = $this->getDoctrine()->getManager(); | 237 | $urls = json_decode($request->query->get('urls', [])); |
228 | $em->persist($entry); | 238 | $results = []; |
229 | $em->flush(); | ||
230 | 239 | ||
231 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | 240 | // handle multiple urls |
241 | if (!empty($urls)) { | ||
242 | $results = []; | ||
243 | foreach ($urls as $key => $url) { | ||
244 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
245 | $url, | ||
246 | $this->getUser()->getId() | ||
247 | ); | ||
232 | 248 | ||
233 | // entry saved, dispatch event about it! | 249 | $results[$key]['url'] = $url; |
234 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
235 | 250 | ||
236 | break; | 251 | if (false === $entry) { |
252 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | ||
253 | new Entry($this->getUser()), | ||
254 | $url | ||
255 | ); | ||
237 | } | 256 | } |
257 | |||
258 | $em = $this->getDoctrine()->getManager(); | ||
259 | $em->persist($entry); | ||
260 | $em->flush(); | ||
261 | |||
262 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
263 | |||
264 | // entry saved, dispatch event about it! | ||
265 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
238 | } | 266 | } |
239 | } | 267 | } |
240 | 268 | ||
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index c37d08cb..64c24a2d 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -769,47 +769,44 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
769 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); | 769 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); |
770 | } | 770 | } |
771 | 771 | ||
772 | public function testPostMassEntriesAction() | 772 | |
773 | public function testPostEntriesListAction() | ||
773 | { | 774 | { |
774 | $list = [ | 775 | $list = [ |
775 | [ | 776 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', |
776 | 'url' => 'http://0.0.0.0/entry2', | 777 | 'http://0.0.0.0/entry6', |
777 | 'action' => 'delete', | ||
778 | ], | ||
779 | [ | ||
780 | 'url' => 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | ||
781 | 'action' => 'add', | ||
782 | ], | ||
783 | [ | ||
784 | 'url' => 'http://0.0.0.0/entry3', | ||
785 | 'action' => 'delete', | ||
786 | ], | ||
787 | [ | ||
788 | 'url' => 'http://0.0.0.0/entry6', | ||
789 | 'action' => 'add', | ||
790 | ], | ||
791 | ]; | 778 | ]; |
792 | 779 | ||
793 | $this->client->request('POST', '/api/entries/lists?list='.json_encode($list)); | 780 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); |
794 | 781 | ||
795 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 782 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
796 | 783 | ||
797 | $content = json_decode($this->client->getResponse()->getContent(), true); | 784 | $content = json_decode($this->client->getResponse()->getContent(), true); |
798 | 785 | ||
799 | $this->assertTrue($content[0]['entry']); | 786 | $this->assertInternalType('int', $content[0]['entry']); |
800 | $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); | 787 | $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); |
801 | $this->assertEquals('delete', $content[0]['action']); | ||
802 | 788 | ||
803 | $this->assertInternalType('int', $content[1]['entry']); | 789 | $this->assertInternalType('int', $content[1]['entry']); |
804 | $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[1]['url']); | 790 | $this->assertEquals('http://0.0.0.0/entry6', $content[1]['url']); |
805 | $this->assertEquals('add', $content[1]['action']); | 791 | } |
792 | |||
793 | public function testDeleteEntriesListAction() | ||
794 | { | ||
795 | $list = [ | ||
796 | 'http://0.0.0.0/entry2', | ||
797 | 'http://0.0.0.0/entry3', | ||
798 | ]; | ||
799 | |||
800 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); | ||
806 | 801 | ||
807 | $this->assertFalse($content[2]['entry']); | 802 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
808 | $this->assertEquals('http://0.0.0.0/entry3', $content[2]['url']); | 803 | |
809 | $this->assertEquals('delete', $content[2]['action']); | 804 | $content = json_decode($this->client->getResponse()->getContent(), true); |
805 | |||
806 | $this->assertTrue($content[0]['entry']); | ||
807 | $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); | ||
810 | 808 | ||
811 | $this->assertInternalType('int', $content[3]['entry']); | 809 | $this->assertFalse($content[1]['entry']); |
812 | $this->assertEquals('http://0.0.0.0/entry6', $content[3]['url']); | 810 | $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); |
813 | $this->assertEquals('add', $content[3]['action']); | ||
814 | } | 811 | } |
815 | } | 812 | } |