mirror of
https://github.com/wallabag/wallabag.git
synced 2024-11-27 11:31:05 +00:00
Merge pull request #2174 from wallabag/api-filter-tags
Add filter for tags on API
This commit is contained in:
commit
9dbd3e9302
3 changed files with 32 additions and 2 deletions
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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)));
|
||||
}
|
||||
|
||||
if ('' !== $tags) {
|
||||
foreach (explode(',', $tags) as $tag) {
|
||||
$qb->andWhere('t.label = :label')->setParameter('label', $tag);
|
||||
}
|
||||
}
|
||||
|
||||
if ('created' === $sort) {
|
||||
$qb->orderBy('e.id', $order);
|
||||
} elseif ('updated' === $sort) {
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Reference in a new issue