From 85146e56763d216946ffc8475fc19de37592f63b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Oct 2016 23:02:12 +0200 Subject: [PATCH] Added support to order short URL lists --- .../src/Repository/ShortUrlRepository.php | 37 +++++++++++++++---- module/Core/src/Service/ShortUrlService.php | 5 ++- .../src/Service/ShortUrlServiceInterface.php | 3 +- .../Rest/src/Action/ListShortcodesAction.php | 1 + 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index a287fe0b..0d7677ea 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -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(); diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index faf778b1..59c76565 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -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); diff --git a/module/Core/src/Service/ShortUrlServiceInterface.php b/module/Core/src/Service/ShortUrlServiceInterface.php index cdf9a45f..bc9b8daf 100644 --- a/module/Core/src/Service/ShortUrlServiceInterface.php +++ b/module/Core/src/Service/ShortUrlServiceInterface.php @@ -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 diff --git a/module/Rest/src/Action/ListShortcodesAction.php b/module/Rest/src/Action/ListShortcodesAction.php index dd08c071..aa987c2f 100644 --- a/module/Rest/src/Action/ListShortcodesAction.php +++ b/module/Rest/src/Action/ListShortcodesAction.php @@ -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, ]; } }