mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 21:12:36 +03:00
Added pagination to ShortUrls list
This commit is contained in:
parent
35f1a4b672
commit
cc1829f9ed
8 changed files with 109 additions and 9 deletions
|
@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
|
||||||
* @author
|
* @author
|
||||||
* @link
|
* @link
|
||||||
*
|
*
|
||||||
* @ORM\Entity
|
* @ORM\Entity(repositoryClass="Acelaya\UrlShortener\Repository\ShortUrlRepository")
|
||||||
* @ORM\Table(name="short_urls")
|
* @ORM\Table(name="short_urls")
|
||||||
*/
|
*/
|
||||||
class ShortUrl extends AbstractEntity implements \JsonSerializable
|
class ShortUrl extends AbstractEntity implements \JsonSerializable
|
||||||
|
|
|
@ -8,6 +8,7 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
use Zend\Stdlib\ArrayUtils;
|
||||||
use Zend\Stratigility\MiddlewareInterface;
|
use Zend\Stratigility\MiddlewareInterface;
|
||||||
|
|
||||||
class ListShortcodesMiddleware implements MiddlewareInterface
|
class ListShortcodesMiddleware implements MiddlewareInterface
|
||||||
|
@ -60,8 +61,11 @@ class ListShortcodesMiddleware implements MiddlewareInterface
|
||||||
|
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'shortUrls' => [
|
'shortUrls' => [
|
||||||
'data' => $shortUrls,
|
'data' => ArrayUtils::iteratorToArray($shortUrls->getCurrentItems()),
|
||||||
// 'pagination' => [],
|
'pagination' => [
|
||||||
|
'currentPage' => $shortUrls->getCurrentPageNumber(),
|
||||||
|
'pagesCount' => $shortUrls->count(),
|
||||||
|
],
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|
56
src/Paginator/Adapter/PaginableRepositoryAdapter.php
Normal file
56
src/Paginator/Adapter/PaginableRepositoryAdapter.php
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<?php
|
||||||
|
namespace Acelaya\UrlShortener\Paginator\Adapter;
|
||||||
|
|
||||||
|
use Acelaya\UrlShortener\Repository\PaginableRepositoryInterface;
|
||||||
|
use Zend\Paginator\Adapter\AdapterInterface;
|
||||||
|
|
||||||
|
class PaginableRepositoryAdapter implements AdapterInterface
|
||||||
|
{
|
||||||
|
const ITEMS_PER_PAGE = 10;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var PaginableRepositoryInterface
|
||||||
|
*/
|
||||||
|
private $paginableRepository;
|
||||||
|
/**
|
||||||
|
* @var null
|
||||||
|
*/
|
||||||
|
private $searchTerm;
|
||||||
|
/**
|
||||||
|
* @var null
|
||||||
|
*/
|
||||||
|
private $orderBy;
|
||||||
|
|
||||||
|
public function __construct(PaginableRepositoryInterface $paginableRepository, $searchTerm = null, $orderBy = null)
|
||||||
|
{
|
||||||
|
$this->paginableRepository = $paginableRepository;
|
||||||
|
$this->searchTerm = $searchTerm;
|
||||||
|
$this->orderBy = $orderBy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a collection of items for a page.
|
||||||
|
*
|
||||||
|
* @param int $offset Page offset
|
||||||
|
* @param int $itemCountPerPage Number of items per page
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getItems($offset, $itemCountPerPage)
|
||||||
|
{
|
||||||
|
return $this->paginableRepository->findList($itemCountPerPage, $offset, $this->searchTerm, $this->orderBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count elements of an object
|
||||||
|
* @link http://php.net/manual/en/countable.count.php
|
||||||
|
* @return int The custom count as an integer.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* The return value is cast to an integer.
|
||||||
|
* @since 5.1.0
|
||||||
|
*/
|
||||||
|
public function count()
|
||||||
|
{
|
||||||
|
return $this->paginableRepository->countList($this->searchTerm);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Acelaya\UrlShortener\Repository;
|
namespace Acelaya\UrlShortener\Repository;
|
||||||
|
|
||||||
interface PaginableRepository
|
interface PaginableRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* Gets a list of elements using provided filtering data
|
||||||
|
*
|
||||||
* @param int|null $limit
|
* @param int|null $limit
|
||||||
* @param int|null $offset
|
* @param int|null $offset
|
||||||
* @param string|null $searchTerm
|
* @param string|null $searchTerm
|
||||||
|
@ -11,4 +13,12 @@ interface PaginableRepository
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function findList($limit = null, $offset = null, $searchTerm = null, $orderBy = null);
|
public function findList($limit = null, $offset = null, $searchTerm = null, $orderBy = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts the number of elements in a list using provided filtering data
|
||||||
|
*
|
||||||
|
* @param null $searchTerm
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function countList($searchTerm = null);
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ namespace Acelaya\UrlShortener\Repository;
|
||||||
|
|
||||||
use Acelaya\UrlShortener\Entity\ShortUrl;
|
use Acelaya\UrlShortener\Entity\ShortUrl;
|
||||||
use Doctrine\ORM\EntityRepository;
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
|
||||||
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
|
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
|
||||||
{
|
{
|
||||||
|
@ -39,4 +40,23 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
|
||||||
|
|
||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts the number of elements in a list using provided filtering data
|
||||||
|
*
|
||||||
|
* @param null $searchTerm
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function countList($searchTerm = null)
|
||||||
|
{
|
||||||
|
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||||
|
$qb->select('COUNT(s)')
|
||||||
|
->from(ShortUrl::class, 's');
|
||||||
|
|
||||||
|
if (isset($searchTerm)) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) $qb->getQuery()->getSingleScalarResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,6 @@ namespace Acelaya\UrlShortener\Repository;
|
||||||
|
|
||||||
use Doctrine\Common\Persistence\ObjectRepository;
|
use Doctrine\Common\Persistence\ObjectRepository;
|
||||||
|
|
||||||
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepository
|
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
namespace Acelaya\UrlShortener\Service;
|
namespace Acelaya\UrlShortener\Service;
|
||||||
|
|
||||||
use Acelaya\UrlShortener\Entity\ShortUrl;
|
use Acelaya\UrlShortener\Entity\ShortUrl;
|
||||||
|
use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter;
|
||||||
|
use Acelaya\UrlShortener\Repository\ShortUrlRepository;
|
||||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Zend\Paginator\Paginator;
|
use Zend\Paginator\Paginator;
|
||||||
|
@ -25,10 +27,17 @@ class ShortUrlService implements ShortUrlServiceInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param int $page
|
||||||
* @return Paginator|ShortUrl[]
|
* @return Paginator|ShortUrl[]
|
||||||
*/
|
*/
|
||||||
public function listShortUrls()
|
public function listShortUrls($page = 1)
|
||||||
{
|
{
|
||||||
return $this->em->getRepository(ShortUrl::class)->findAll();
|
/** @var ShortUrlRepository $repo */
|
||||||
|
$repo = $this->em->getRepository(ShortUrl::class);
|
||||||
|
$paginator = new Paginator(new PaginableRepositoryAdapter($repo));
|
||||||
|
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
|
||||||
|
->setCurrentPageNumber($page);
|
||||||
|
|
||||||
|
return $paginator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@ use Zend\Paginator\Paginator;
|
||||||
interface ShortUrlServiceInterface
|
interface ShortUrlServiceInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return Paginator|ShortUrl[]
|
* @param int $page
|
||||||
|
* @return ShortUrl[]|Paginator
|
||||||
*/
|
*/
|
||||||
public function listShortUrls();
|
public function listShortUrls($page = 1);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue