From: Jeremy Benoist Date: Fri, 7 Oct 2016 18:37:01 +0000 (+0200) Subject: Ability to check multiple urls in API X-Git-Tag: 2.1.2~33^2 X-Git-Url: https://git.immae.eu/?p=github%2Fwallabag%2Fwallabag.git;a=commitdiff_plain;h=f0abc22d09d2e38d3dd408425821a89da3d21377 Ability to check multiple urls in API --- diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index a0d9d4f3..6dd03c1b 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -27,7 +27,8 @@ class WallabagRestController extends FOSRestController * * @ApiDoc( * parameters={ - * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} + * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, + * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} * } * ) * @@ -37,6 +38,25 @@ class WallabagRestController extends FOSRestController { $this->validateAuthentication(); + $urls = $request->query->get('urls', []); + + // handle multiple urls first + if (!empty($urls)) { + $results = []; + foreach ($urls as $url) { + $res = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->getUser()->getId()); + + $results[$url] = false === $res ? false : true; + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } + + // let's see if it is a simple url? $url = $request->query->get('url', ''); if (empty($url)) { diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index 65b65290..4f16e70f 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php @@ -794,6 +794,22 @@ class WallabagRestControllerTest extends WallabagApiTestCase $this->assertEquals(true, $content['exists']); } + public function testGetEntriesExistsWithManyUrls() + { + $url1 = 'http://0.0.0.0/entry2'; + $url2 = 'http://0.0.0.0/entry10'; + $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); + + $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertArrayHasKey($url1, $content); + $this->assertArrayHasKey($url2, $content); + $this->assertEquals(true, $content[$url1]); + $this->assertEquals(false, $content[$url2]); + } + public function testGetEntriesExistsWhichDoesNotExists() { $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');