]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add filter for tags on API
authorThomas Citharel <tcit@tcit.fr>
Sat, 25 Jun 2016 14:27:38 +0000 (16:27 +0200)
committerThomas Citharel <tcit@tcit.fr>
Wed, 29 Jun 2016 07:55:57 +0000 (09:55 +0200)
src/Wallabag/ApiBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php

index 4fae4b0a8a07842eeb2ed53cd87a056ac641e117..03eb9b0848272381bfbc3dd550c476068d55dcd3 100644 (file)
@@ -51,10 +51,11 @@ class WallabagRestController extends FOSRestController
         $page = (int) $request->query->get('page', 1);
         $perPage = (int) $request->query->get('perPage', 30);
         $since = $request->query->get('since', 0);
+        $tags = $request->query->get('tags', '');
 
         $pager = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since);
+            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
 
         $pager->setCurrentPage($page);
         $pager->setMaxPerPage($perPage);
index 4b607c81671cd749a52ff30a4a28bc4ff4b7118d..8d2ec9ce4f471d6f0f098dc7ec0558dbbbbe71fb 100644 (file)
@@ -95,9 +95,10 @@ class EntryRepository extends EntityRepository
      *
      * @return array
      */
-    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0)
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
     {
         $qb = $this->createQueryBuilder('e')
+            ->leftJoin('e.tags', 't')
             ->where('e.user =:userId')->setParameter('userId', $userId);
 
         if (null !== $isArchived) {
@@ -110,6 +111,11 @@ class EntryRepository extends EntityRepository
 
         if ($since >= 0) {
             $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
+            
+        if ('' !== $tags) {
+            foreach (explode(',', $tags) as $tag) {
+                $qb->andWhere('t.label = :label')->setParameter('label', $tag);
+            }
         }
 
         if ('created' === $sort) {
index f256a7f9fb37f290c1c0d01478a7bb40aec15cda..528366afa08517c6a88d0cde45743286778f0882 100644 (file)
@@ -121,6 +121,28 @@ class WallabagRestControllerTest extends WallabagApiTestCase
         );
     }
 
+    public function testGetTaggedEntries()
+    {
+        $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertGreaterThanOrEqual(1, count($content));
+        $this->assertNotEmpty($content['_embedded']['items']);
+        $this->assertGreaterThanOrEqual(1, $content['total']);
+        $this->assertEquals(1, $content['page']);
+        $this->assertGreaterThanOrEqual(1, $content['pages']);
+
+        $this->assertTrue(
+            $this->client->getResponse()->headers->contains(
+                'Content-Type',
+                'application/json'
+            )
+        );
+    }
+
     public function testGetDatedEntries()
     {
         $this->client->request('GET', '/api/entries', ['since' => 1]);