]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Ability to check multiple urls in API 2393/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 7 Oct 2016 18:37:01 +0000 (20:37 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 7 Oct 2016 18:37:01 +0000 (20:37 +0200)
src/Wallabag/ApiBundle/Controller/WallabagRestController.php
tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php

index a0d9d4f3a144336735468ad3fd4f04b67eda8087..6dd03c1b6495564371cb96673b3efd3e311d71e5 100644 (file)
@@ -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)) {
index 65b65290c2e83731c0e535b11f9ff673fa35406b..4f16e70f9ae2334d06be47dc3fd830eac202c9ed 100644 (file)
@@ -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');