]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add an exists endpoint in API
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 1 Oct 2016 15:15:28 +0000 (17:15 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 1 Oct 2016 15:15:28 +0000 (17:15 +0200)
It should allow third party to check if an url was already saved by a user

src/Wallabag/ApiBundle/Controller/WallabagRestController.php
tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php

index dd17ef976d38a6a3f8f49ae3b639a5ba5e65a94a..b83e42665ce4e544eea0f55824331bfc8286fa3e 100644 (file)
@@ -22,6 +22,38 @@ class WallabagRestController extends FOSRestController
         }
     }
 
+    /**
+     * Check if an entry exist by url
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}
+     *       }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function getEntriesExistsAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $url = $request->query->get('url', '');
+
+        if (empty($url)) {
+            throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId());
+        }
+
+        $res = $this->getDoctrine()
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findByUrlAndUserId($url, $this->getUser()->getId());
+
+        $exists = false === $res ? false : true;
+
+        $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
+
+        return (new JsonResponse())->setJson($json);
+    }
+
     /**
      * Retrieve all entries. It could be filtered by many options.
      *
index 101c20eb69fd82572d85f0549f633e41e4ecd21e..e582e5f9c298924c912c92036e8fc7112257ed74 100644 (file)
@@ -684,4 +684,15 @@ class WallabagRestControllerTest extends WallabagApiTestCase
 
         $this->assertEquals(true, $content['is_starred']);
     }
+
+    public function testGetEntriesExists()
+    {
+        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertEquals(true, $content['exists']);
+    }
 }