From: Thomas Citharel Date: Sat, 25 Jun 2016 14:27:38 +0000 (+0200) Subject: Add filter for tags on API X-Git-Tag: 2.1.0~65^2~1 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=28803f106bd091b5a7540a134cba2545d12d25ea;p=github%2Fwallabag%2Fwallabag.git Add filter for tags on API --- diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 4fae4b0a..03eb9b08 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -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); diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 4b607c81..8d2ec9ce 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -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) { diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php index f256a7f9..528366af 100644 --- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php @@ -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]);