diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 8bbe909..d0f604d 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -14,9 +14,23 @@ use Symfony\Component\Security\Http\Attribute\IsGranted; class PostController extends AbstractController { + private const POSTS_PER_PAGE = 10; + #[Route('/', name: 'app_posts')] + public function index(PostRepository $repository, Request $request): Response + { + $page = $request->query->getInt('page', 1); + $posts = $repository->findPaginatedPosts($page, self::POSTS_PER_PAGE); + $maxPage = ceil($posts->count() / self::POSTS_PER_PAGE); + return $this->render('post/index.html.twig', [ + 'posts' => $posts, + 'maxPage' => $maxPage, + 'page' => $page, + ]); + } + #[Route('/post', name: 'app_post_index', methods: ['GET'])] - public function index(PostRepository $repository): Response + public function table(PostRepository $repository): Response { $posts = $repository->findAll(); return $this->render('post/table.html.twig', [ diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index 93adbe9..2082982 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\Post; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Doctrine\ORM\Tools\Pagination\Paginator; /** * @extends ServiceEntityRepository @@ -16,6 +17,20 @@ class PostRepository extends ServiceEntityRepository parent::__construct($registry, Post::class); } + /** + * @param int $page + * @param int $limit + * @return Paginator + */ + public function findPaginatedPosts(int $page, int $limit): Paginator + { + $query = $this->createQueryBuilder('p') + ->setFirstResult(($page - 1) * $limit) + ->setMaxResults($limit); + + return new Paginator($query, fetchJoinCollection: false); + } + // /** // * @return Post[] Returns an array of Post objects // */ diff --git a/templates/_pagination.html.twig b/templates/_pagination.html.twig new file mode 100644 index 0000000..1c4cc35 --- /dev/null +++ b/templates/_pagination.html.twig @@ -0,0 +1,24 @@ +{% set route = app.request.attributes.get('_route') %} + diff --git a/templates/post/index.html.twig b/templates/post/index.html.twig new file mode 100644 index 0000000..caab461 --- /dev/null +++ b/templates/post/index.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block title %}Posts{% endblock %} + +{% block body %} +{% for post in posts.iterator %} +
+
+
{{ post.species ? post.species.vernacularName : 'Post' }}
+
{{ post.foundDate | date("d/m/Y \\à H \\h") }}
+

{{ post.latitude }}, {{ post.longitude }}, {{ post.altitude }}m

+

{{ post.commentary }}

+
+ +
+{% endfor %} +{% include '_pagination.html.twig' %} +{% endblock %}