#33: limit orders on first page and paginate
This commit is contained in:
parent
7e53705b4b
commit
5d41b6fef5
4 changed files with 57 additions and 19 deletions
|
@ -3,7 +3,10 @@
|
|||
namespace App\Repository;
|
||||
|
||||
use App\Entity\FoodOrder;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
|
@ -27,15 +30,23 @@ final class FoodOrderRepository extends ServiceEntityRepository
|
|||
/**
|
||||
* @return FoodOrder[]
|
||||
*/
|
||||
public function findLatestEntries(int $limit = 5): array
|
||||
public function findLatestEntries(int $page = 1, int $pagesize = 10, int $days = 4): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('alias');
|
||||
|
||||
$qb->orderBy('alias.id', 'DESC');
|
||||
$qb->setMaxResults($limit);
|
||||
$result = $this->createQueryBuilder('alias')
|
||||
->orderBy('alias.id', 'DESC')
|
||||
->setFirstResult(($page - 1) * $pagesize)
|
||||
->setMaxResults($pagesize)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$query = $qb->getQuery();
|
||||
if ($days < 1) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $query->getResult();
|
||||
$date = (new DateTimeImmutable)->sub(new DateInterval('P' . $days . 'D'));
|
||||
return (new ArrayCollection($result))
|
||||
->filter(static fn(FoodOrder $order): bool => $order->getCreatedAt() >= $date)
|
||||
->getValues();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue