diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 10:22:57 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-05-01 09:25:34 +0200 |
commit | 1eca7831a69b9470b92dcc72e1ce51b42b291338 (patch) | |
tree | a8627c4bc1457729f92096ab4b36ece95b1a8857 | |
parent | 3cbb0cc3ef89873a06bda6583747a2660b989fb8 (diff) | |
download | wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.tar.gz wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.tar.zst wallabag-1eca7831a69b9470b92dcc72e1ce51b42b291338.zip |
Added API endpoint to handle a list of URL
By passing an array, you can add / delete URL in mass (bulk request)
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 71 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 46 |
2 files changed, 109 insertions, 8 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7590efbb..3833ce3c 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -173,6 +173,77 @@ class EntryRestController extends WallabagRestController | |||
173 | } | 173 | } |
174 | 174 | ||
175 | /** | 175 | /** |
176 | * Handles an entries list and create or remove URL. | ||
177 | * | ||
178 | * @ApiDoc( | ||
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."} | ||
181 | * } | ||
182 | * ) | ||
183 | * | ||
184 | * @return JsonResponse | ||
185 | */ | ||
186 | public function postEntriesListAction(Request $request) | ||
187 | { | ||
188 | $this->validateAuthentication(); | ||
189 | |||
190 | $list = json_decode($request->query->get('list', [])); | ||
191 | $results = []; | ||
192 | |||
193 | // handle multiple urls | ||
194 | if (!empty($list)) { | ||
195 | $results = []; | ||
196 | foreach ($list as $key => $element) { | ||
197 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
198 | $element->url, | ||
199 | $this->getUser()->getId() | ||
200 | ); | ||
201 | |||
202 | $results[$key]['url'] = $element->url; | ||
203 | $results[$key]['action'] = $element->action; | ||
204 | |||
205 | switch ($element->action) { | ||
206 | case 'delete': | ||
207 | if (false !== $entry) { | ||
208 | $em = $this->getDoctrine()->getManager(); | ||
209 | $em->remove($entry); | ||
210 | $em->flush(); | ||
211 | |||
212 | // entry deleted, dispatch event about it! | ||
213 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | ||
214 | } | ||
215 | |||
216 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; | ||
217 | |||
218 | break; | ||
219 | case 'add': | ||
220 | if (false === $entry) { | ||
221 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | ||
222 | new Entry($this->getUser()), | ||
223 | $element->url | ||
224 | ); | ||
225 | } | ||
226 | |||
227 | $em = $this->getDoctrine()->getManager(); | ||
228 | $em->persist($entry); | ||
229 | $em->flush(); | ||
230 | |||
231 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
232 | |||
233 | // entry saved, dispatch event about it! | ||
234 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
235 | |||
236 | break; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | |||
241 | $json = $this->get('serializer')->serialize($results, 'json'); | ||
242 | |||
243 | return (new JsonResponse())->setJson($json); | ||
244 | } | ||
245 | |||
246 | /** | ||
176 | * Create an entry. | 247 | * Create an entry. |
177 | * | 248 | * |
178 | * @ApiDoc( | 249 | * @ApiDoc( |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 19fb5170..c37d08cb 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -766,20 +766,50 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
766 | ], | 766 | ], |
767 | ]; | 767 | ]; |
768 | 768 | ||
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 | } | ||
771 | |||
772 | public function testPostMassEntriesAction() | ||
773 | { | ||
774 | $list = [ | ||
775 | [ | ||
776 | 'url' => 'http://0.0.0.0/entry2', | ||
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 | ]; | ||
792 | |||
793 | $this->client->request('POST', '/api/entries/lists?list='.json_encode($list)); | ||
770 | 794 | ||
771 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | 795 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
772 | 796 | ||
773 | $content = json_decode($this->client->getResponse()->getContent(), true); | 797 | $content = json_decode($this->client->getResponse()->getContent(), true); |
774 | 798 | ||
775 | $this->assertInternalType('int', $content[0]['entry']); | 799 | $this->assertTrue($content[0]['entry']); |
776 | $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']); | 800 | $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); |
801 | $this->assertEquals('delete', $content[0]['action']); | ||
777 | 802 | ||
778 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | 803 | $this->assertInternalType('int', $content[1]['entry']); |
779 | ->getRepository('WallabagCoreBundle: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']); |
780 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | 805 | $this->assertEquals('add', $content[1]['action']); |
781 | 806 | ||
782 | $tags = $entry->getTags(); | 807 | $this->assertFalse($content[2]['entry']); |
783 | $this->assertCount(2, $tags); | 808 | $this->assertEquals('http://0.0.0.0/entry3', $content[2]['url']); |
809 | $this->assertEquals('delete', $content[2]['action']); | ||
810 | |||
811 | $this->assertInternalType('int', $content[3]['entry']); | ||
812 | $this->assertEquals('http://0.0.0.0/entry6', $content[3]['url']); | ||
813 | $this->assertEquals('add', $content[3]['action']); | ||
784 | } | 814 | } |
785 | } | 815 | } |