Add filter for tags on API

This commit is contained in:
Thomas Citharel 2016-06-25 16:27:38 +02:00
parent a314b920bf
commit 28803f106b
3 changed files with 31 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) {
@ -110,6 +111,11 @@ class EntryRepository extends EntityRepository
if ($since >= 0) { if ($since >= 0) {
$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) {

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]);