mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Implemented filtering by tags in ListShortcodesAction
This commit is contained in:
parent
8b9caf02d2
commit
52bb14bd66
4 changed files with 16 additions and 6 deletions
|
@ -17,7 +17,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||
*/
|
||||
public function findList($limit = null, $offset = null, $searchTerm = null, array $tags = [], $orderBy = null)
|
||||
{
|
||||
$qb = $this->createListQueryBuilder($searchTerm);
|
||||
$qb = $this->createListQueryBuilder($searchTerm, $tags);
|
||||
$qb->select('s');
|
||||
|
||||
if (isset($limit)) {
|
||||
|
@ -49,7 +49,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||
*/
|
||||
public function countList($searchTerm = null, array $tags = [])
|
||||
{
|
||||
$qb = $this->createListQueryBuilder($searchTerm);
|
||||
$qb = $this->createListQueryBuilder($searchTerm, $tags);
|
||||
$qb->select('COUNT(s)');
|
||||
|
||||
return (int) $qb->getQuery()->getSingleScalarResult();
|
||||
|
@ -64,6 +64,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->from(ShortUrl::class, 's');
|
||||
$qb->where('1=1');
|
||||
|
||||
// Apply search term to every searchable field if not empty
|
||||
if (! empty($searchTerm)) {
|
||||
|
@ -73,11 +74,17 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
|||
];
|
||||
|
||||
// Unpack and apply search conditions
|
||||
$qb->where($qb->expr()->orX(...$conditions));
|
||||
$qb->andWhere($qb->expr()->orX(...$conditions));
|
||||
$searchTerm = '%' . $searchTerm . '%';
|
||||
$qb->setParameter('searchPattern', $searchTerm);
|
||||
}
|
||||
|
||||
// Filter by tags if provided
|
||||
if (! empty($tags)) {
|
||||
$qb->join('s.tags', 't')
|
||||
->andWhere($qb->expr()->in('t.name', $tags));
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,13 +33,14 @@ class ShortUrlService implements ShortUrlServiceInterface
|
|||
/**
|
||||
* @param int $page
|
||||
* @param string $searchQuery
|
||||
* @param array $tags
|
||||
* @return ShortUrl[]|Paginator
|
||||
*/
|
||||
public function listShortUrls($page = 1, $searchQuery = null)
|
||||
public function listShortUrls($page = 1, $searchQuery = null, array $tags = [])
|
||||
{
|
||||
/** @var ShortUrlRepository $repo */
|
||||
$repo = $this->em->getRepository(ShortUrl::class);
|
||||
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery));
|
||||
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags));
|
||||
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
|
||||
->setCurrentPageNumber($page);
|
||||
|
||||
|
|
|
@ -10,9 +10,10 @@ interface ShortUrlServiceInterface
|
|||
/**
|
||||
* @param int $page
|
||||
* @param string $searchQuery
|
||||
* @param array $tags
|
||||
* @return ShortUrl[]|Paginator
|
||||
*/
|
||||
public function listShortUrls($page = 1, $searchQuery = null);
|
||||
public function listShortUrls($page = 1, $searchQuery = null, array $tags = []);
|
||||
|
||||
/**
|
||||
* @param string $shortCode
|
||||
|
|
|
@ -74,6 +74,7 @@ class ListShortcodesAction extends AbstractRestAction
|
|||
return [
|
||||
isset($query['page']) ? $query['page'] : 1,
|
||||
isset($query['searchTerm']) ? $query['searchTerm'] : null,
|
||||
isset($query['tags']) ? $query['tags'] : [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue