diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-10-02 13:17:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-02 13:17:23 +0200 |
commit | 18b8dc0e9984fce01b231810347fef7d32c06ac7 (patch) | |
tree | 6ab63a4d9bc6e8ef85e274b61f6cc16e676bb5f9 | |
parent | 92395680b6ac76a2776f3635803b5de447014b11 (diff) | |
parent | 3583cadf6fc4e5d2b410a42c874bb9a94e505390 (diff) | |
download | wallabag-18b8dc0e9984fce01b231810347fef7d32c06ac7.tar.gz wallabag-18b8dc0e9984fce01b231810347fef7d32c06ac7.tar.zst wallabag-18b8dc0e9984fce01b231810347fef7d32c06ac7.zip |
Merge pull request #2325 from wallabag/api-entries-exists
Add an exists endpoint in API
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 32 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | 22 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index dd17ef97..104720a9 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -23,6 +23,38 @@ class WallabagRestController extends FOSRestController | |||
23 | } | 23 | } |
24 | 24 | ||
25 | /** | 25 | /** |
26 | * Check if an entry exist by url. | ||
27 | * | ||
28 | * @ApiDoc( | ||
29 | * parameters={ | ||
30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} | ||
31 | * } | ||
32 | * ) | ||
33 | * | ||
34 | * @return JsonResponse | ||
35 | */ | ||
36 | public function getEntriesExistsAction(Request $request) | ||
37 | { | ||
38 | $this->validateAuthentication(); | ||
39 | |||
40 | $url = $request->query->get('url', ''); | ||
41 | |||
42 | if (empty($url)) { | ||
43 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId()); | ||
44 | } | ||
45 | |||
46 | $res = $this->getDoctrine() | ||
47 | ->getRepository('WallabagCoreBundle:Entry') | ||
48 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
49 | |||
50 | $exists = false === $res ? false : true; | ||
51 | |||
52 | $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); | ||
53 | |||
54 | return (new JsonResponse())->setJson($json); | ||
55 | } | ||
56 | |||
57 | /** | ||
26 | * Retrieve all entries. It could be filtered by many options. | 58 | * Retrieve all entries. It could be filtered by many options. |
27 | * | 59 | * |
28 | * @ApiDoc( | 60 | * @ApiDoc( |
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index 101c20eb..9b5760bc 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php | |||
@@ -684,4 +684,26 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
684 | 684 | ||
685 | $this->assertEquals(true, $content['is_starred']); | 685 | $this->assertEquals(true, $content['is_starred']); |
686 | } | 686 | } |
687 | |||
688 | public function testGetEntriesExists() | ||
689 | { | ||
690 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | ||
691 | |||
692 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
693 | |||
694 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
695 | |||
696 | $this->assertEquals(true, $content['exists']); | ||
697 | } | ||
698 | |||
699 | public function testGetEntriesExistsWhichDoesNotExists() | ||
700 | { | ||
701 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | ||
702 | |||
703 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
704 | |||
705 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
706 | |||
707 | $this->assertEquals(false, $content['exists']); | ||
708 | } | ||
687 | } | 709 | } |