diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-07-01 10:35:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-01 10:35:45 +0200 |
commit | 9dbd3e93028630ca808042387e88304020c80c73 (patch) | |
tree | c79b5cd53b9c2b1a2463d2809cc059fe7d74a65c | |
parent | a314b920bf20feedbeee6ce0432be89901091481 (diff) | |
parent | 4f0558a0d43b3d0bbd661c06568257a91836fa91 (diff) | |
download | wallabag-9dbd3e93028630ca808042387e88304020c80c73.tar.gz wallabag-9dbd3e93028630ca808042387e88304020c80c73.tar.zst wallabag-9dbd3e93028630ca808042387e88304020c80c73.zip |
Merge pull request #2174 from wallabag/api-filter-tags
Add filter for tags on API
3 files changed, 32 insertions, 2 deletions
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 | |||
51 | $page = (int) $request->query->get('page', 1); | 51 | $page = (int) $request->query->get('page', 1); |
52 | $perPage = (int) $request->query->get('perPage', 30); | 52 | $perPage = (int) $request->query->get('perPage', 30); |
53 | $since = $request->query->get('since', 0); | 53 | $since = $request->query->get('since', 0); |
54 | $tags = $request->query->get('tags', ''); | ||
54 | 55 | ||
55 | $pager = $this->getDoctrine() | 56 | $pager = $this->getDoctrine() |
56 | ->getRepository('WallabagCoreBundle:Entry') | 57 | ->getRepository('WallabagCoreBundle:Entry') |
57 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since); | 58 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); |
58 | 59 | ||
59 | $pager->setCurrentPage($page); | 60 | $pager->setCurrentPage($page); |
60 | $pager->setMaxPerPage($perPage); | 61 | $pager->setMaxPerPage($perPage); |
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 4b607c81..b543c5ae 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -95,9 +95,10 @@ class EntryRepository extends EntityRepository | |||
95 | * | 95 | * |
96 | * @return array | 96 | * @return array |
97 | */ | 97 | */ |
98 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0) | 98 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') |
99 | { | 99 | { |
100 | $qb = $this->createQueryBuilder('e') | 100 | $qb = $this->createQueryBuilder('e') |
101 | ->leftJoin('e.tags', 't') | ||
101 | ->where('e.user =:userId')->setParameter('userId', $userId); | 102 | ->where('e.user =:userId')->setParameter('userId', $userId); |
102 | 103 | ||
103 | if (null !== $isArchived) { | 104 | if (null !== $isArchived) { |
@@ -112,6 +113,12 @@ class EntryRepository extends EntityRepository | |||
112 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); | 113 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
113 | } | 114 | } |
114 | 115 | ||
116 | if ('' !== $tags) { | ||
117 | foreach (explode(',', $tags) as $tag) { | ||
118 | $qb->andWhere('t.label = :label')->setParameter('label', $tag); | ||
119 | } | ||
120 | } | ||
121 | |||
115 | if ('created' === $sort) { | 122 | if ('created' === $sort) { |
116 | $qb->orderBy('e.id', $order); | 123 | $qb->orderBy('e.id', $order); |
117 | } elseif ('updated' === $sort) { | 124 | } elseif ('updated' === $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 | |||
121 | ); | 121 | ); |
122 | } | 122 | } |
123 | 123 | ||
124 | public function testGetTaggedEntries() | ||
125 | { | ||
126 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | ||
127 | |||
128 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
129 | |||
130 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
131 | |||
132 | $this->assertGreaterThanOrEqual(1, count($content)); | ||
133 | $this->assertNotEmpty($content['_embedded']['items']); | ||
134 | $this->assertGreaterThanOrEqual(1, $content['total']); | ||
135 | $this->assertEquals(1, $content['page']); | ||
136 | $this->assertGreaterThanOrEqual(1, $content['pages']); | ||
137 | |||
138 | $this->assertTrue( | ||
139 | $this->client->getResponse()->headers->contains( | ||
140 | 'Content-Type', | ||
141 | 'application/json' | ||
142 | ) | ||
143 | ); | ||
144 | } | ||
145 | |||
124 | public function testGetDatedEntries() | 146 | public function testGetDatedEntries() |
125 | { | 147 | { |
126 | $this->client->request('GET', '/api/entries', ['since' => 1]); | 148 | $this->client->request('GET', '/api/entries', ['since' => 1]); |