Merge pull request #2174 from wallabag/api-filter-tags

Add filter for tags on API
This commit is contained in:
Nicolas Lœuillet 2016-07-01 10:35:45 +02:00 committed by GitHub
commit 9dbd3e9302
3 changed files with 32 additions and 2 deletions

View file

@ -51,10 +51,11 @@ class WallabagRestController extends FOSRestController
$page = (int) $request->query->get('page', 1); $page = (int) $request->query->get('page', 1);
$perPage = (int) $request->query->get('perPage', 30); $perPage = (int) $request->query->get('perPage', 30);
$since = $request->query->get('since', 0); $since = $request->query->get('since', 0);
$tags = $request->query->get('tags', '');
$pager = $this->getDoctrine() $pager = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Entry') ->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->setCurrentPage($page);
$pager->setMaxPerPage($perPage); $pager->setMaxPerPage($perPage);

View file

@ -95,9 +95,10 @@ class EntryRepository extends EntityRepository
* *
* @return array * @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') $qb = $this->createQueryBuilder('e')
->leftJoin('e.tags', 't')
->where('e.user =:userId')->setParameter('userId', $userId); ->where('e.user =:userId')->setParameter('userId', $userId);
if (null !== $isArchived) { if (null !== $isArchived) {
@ -112,6 +113,12 @@ class EntryRepository extends EntityRepository
$qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); $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) { if ('created' === $sort) {
$qb->orderBy('e.id', $order); $qb->orderBy('e.id', $order);
} elseif ('updated' === $sort) { } elseif ('updated' === $sort) {

View 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() public function testGetDatedEntries()
{ {
$this->client->request('GET', '/api/entries', ['since' => 1]); $this->client->request('GET', '/api/entries', ['since' => 1]);