Added support to order short URL lists

This commit is contained in:
Alejandro Celaya 2016-10-22 23:02:12 +02:00
parent 18ae541c93
commit 85146e5676
4 changed files with 35 additions and 11 deletions

View File

@ -20,21 +20,42 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
$qb = $this->createListQueryBuilder($searchTerm, $tags);
$qb->select('s');
// Set limit and offset
if (isset($limit)) {
$qb->setMaxResults($limit);
}
if (isset($offset)) {
$qb->setFirstResult($offset);
}
// In case the ordering has been specified, the query could be more complex. Process it
if (isset($orderBy)) {
if (is_string($orderBy)) {
$qb->orderBy($orderBy);
} elseif (is_array($orderBy)) {
$key = key($orderBy);
$qb->orderBy($key, $orderBy[$key]);
}
} else {
$qb->orderBy('s.dateCreated');
return $this->processOrderByForList($qb, $orderBy);
}
// With no order by, order by date and just return the list of ShortUrls
$qb->orderBy('s.dateCreated');
return $qb->getQuery()->getResult();
}
protected function processOrderByForList(QueryBuilder $qb, $orderBy)
{
$fieldName = is_array($orderBy) ? key($orderBy) : $orderBy;
$order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC';
if ($fieldName === 'visits') {
$qb->addSelect('COUNT(v) AS totalVisits')
->leftJoin('s.visits', 'v')
->groupBy('s')
->orderBy('totalVisits', $order);
return array_column($qb->getQuery()->getResult(), 0);
} elseif (in_array($fieldName, [
'originalUrl',
'shortCode',
'dateCreated',
])) {
$qb->orderBy('s.' . $fieldName, $order);
}
return $qb->getQuery()->getResult();

View File

@ -34,13 +34,14 @@ class ShortUrlService implements ShortUrlServiceInterface
* @param int $page
* @param string $searchQuery
* @param array $tags
* @param null $orderBy
* @return ShortUrl[]|Paginator
*/
public function listShortUrls($page = 1, $searchQuery = null, array $tags = [])
public function listShortUrls($page = 1, $searchQuery = null, array $tags = [], $orderBy = null)
{
/** @var ShortUrlRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags));
$paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags, $orderBy));
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
->setCurrentPageNumber($page);

View File

@ -11,9 +11,10 @@ interface ShortUrlServiceInterface
* @param int $page
* @param string $searchQuery
* @param array $tags
* @param null $orderBy
* @return ShortUrl[]|Paginator
*/
public function listShortUrls($page = 1, $searchQuery = null, array $tags = []);
public function listShortUrls($page = 1, $searchQuery = null, array $tags = [], $orderBy = null);
/**
* @param string $shortCode

View File

@ -75,6 +75,7 @@ class ListShortcodesAction extends AbstractRestAction
isset($query['page']) ? $query['page'] : 1,
isset($query['searchTerm']) ? $query['searchTerm'] : null,
isset($query['tags']) ? $query['tags'] : [],
isset($query['orderBy']) ? $query['orderBy'] : null,
];
}
}