#33: limit orders on first page and paginate #35

Merged
lubiana merged 1 commit from #33_show-only-current-orders-on-first-page-and-paginate into main 2024-07-08 19:24:49 +00:00
4 changed files with 57 additions and 19 deletions

View file

@ -14,11 +14,32 @@ use Symfony\Component\Routing\Attribute\Route;
#[Route('/food/order')] #[Route('/food/order')]
final class FoodOrderController extends AbstractController final class FoodOrderController extends AbstractController
{ {
#[Route('/', name: 'app_food_order_index', methods: ['GET'])] #[Route(
public function index(FoodOrderRepository $foodOrderRepository): Response path: '/list/{page}',
name: 'app_food_order_index',
requirements: [
'page' => '\d+',
],
methods: ['GET']
)]
public function index(FoodOrderRepository $foodOrderRepository, int $page = 1): Response
{ {
$days = 4;
if ($page > 1) {
$days = 0;
}
$nextPage = $page + 1;
$prevPage = $page - 1;
$itemsPerPage = 10;
if($foodOrderRepository->count() < $page * $itemsPerPage) {
$nextPage = $page;
}
return $this->render('food_order/index.html.twig', [ return $this->render('food_order/index.html.twig', [
'food_orders' => $foodOrderRepository->findLatestEntries(), 'food_orders' => $foodOrderRepository->findLatestEntries(page: $page, pagesize: $itemsPerPage, days: $days),
'current_page' => $page,
'next_page' => $nextPage,
'prev_page' => $prevPage,
]); ]);
} }

View file

@ -3,7 +3,10 @@
namespace App\Repository; namespace App\Repository;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
use DateInterval;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
@ -27,15 +30,23 @@ final class FoodOrderRepository extends ServiceEntityRepository
/** /**
* @return FoodOrder[] * @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'); $result = $this->createQueryBuilder('alias')
$qb->setMaxResults($limit); ->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();
} }
} }

View file

@ -4,7 +4,14 @@
{% block body %} {% block body %}
<h1>FoodOrder index</h1> <h1>FoodOrder index</h1>
<div>
<button
hx-get="{{ path('app_food_order_new') }}"
hx-trigger="click"
hx-target="closest div"
>Create new</button>
</div>
<hr>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -25,11 +32,10 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<div> {% if prev_page > 0 %}
<button <a href="{{ path('app_food_order_index', {'page': prev_page}) }}">previous page</a> |
hx-get="{{ path('app_food_order_new') }}" {% endif %}
hx-trigger="click" {% if next_page > current_page %}
hx-target="closest div" <a href="{{ path('app_food_order_index', {'page': next_page}) }}">next page</a>
>Create new</button> {% endif %}
</div>
{% endblock %} {% endblock %}

View file

@ -41,7 +41,7 @@ final class FoodOrderControllerTest extends DbWebTest
$this->manager->persist($this->vendor); $this->manager->persist($this->vendor);
$this->manager->flush(); $this->manager->flush();
$crawler = $this->client->request('GET', $this->path); $crawler = $this->client->request('GET', "{$this->path}list");
self::assertResponseStatusCodeSame(200); self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodOrder index'); self::assertPageTitleContains('FoodOrder index');
$this->assertCount( $this->assertCount(
@ -61,7 +61,7 @@ final class FoodOrderControllerTest extends DbWebTest
'food_order[foodVendor]' => $this->vendor->getId(), 'food_order[foodVendor]' => $this->vendor->getId(),
]); ]);
self::assertResponseRedirects($this->path); self::assertResponseRedirects("{$this->path}list");
self::assertSame(1, $this->repository->count([])); self::assertSame(1, $this->repository->count([]));
} }
} }