From e538b82bf58ae1a4be9af9c03eaa3145d2732194 Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Date: Sun, 28 Jul 2024 00:33:23 -0400 Subject: [PATCH] Paginated comments --- src/Controller/ConferenceController.php | 10 ++++++++-- src/Repository/CommentRepository.php | 18 ++++++++++++++++++ templates/conference/show.html.twig | 9 +++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Controller/ConferenceController.php b/src/Controller/ConferenceController.php index d18d3ac..a5756f7 100644 --- a/src/Controller/ConferenceController.php +++ b/src/Controller/ConferenceController.php @@ -6,6 +6,7 @@ use App\Entity\Conference; use App\Repository\CommentRepository; use App\Repository\ConferenceRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Twig\Environment; @@ -21,11 +22,16 @@ class ConferenceController extends AbstractController } #[Route('/conference/{id}', name: 'conference')] - public function show(Environment $twig, Conference $conference, CommentRepository $commentRepository): Response + public function show(Request $request, Environment $twig, Conference $conference, CommentRepository $commentRepository): Response { + $offset = max(0, $request->query->getInt('offset', 0)); + $paginator = $commentRepository->getCommentPaginator($conference, $offset); + return new Response($twig->render('conference/show.html.twig', [ 'conference' => $conference, - 'comments' => $commentRepository->findBy(['conference' => $conference], ['createdAt' => 'DESC']), + 'comments' => $paginator, + 'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE, + 'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE) ])); } } diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php index 47ea76e..8d9d2eb 100644 --- a/src/Repository/CommentRepository.php +++ b/src/Repository/CommentRepository.php @@ -3,19 +3,37 @@ namespace App\Repository; use App\Entity\Comment; +use App\Entity\Conference; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Doctrine\ORM\Tools\Pagination\Paginator; /** * @extends ServiceEntityRepository */ class CommentRepository extends ServiceEntityRepository { + public const COMMENTS_PER_PAGE = 2; + public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Comment::class); } + public function getCommentPaginator(Conference $conference, int $offset): Paginator + { + $query = $this->createQueryBuilder('c') + ->andWhere('c.conference = :conference') + ->setParameter('conference', $conference) + ->orderBy('c.createdAt', 'DESC') + ->setMaxResults(self::COMMENTS_PER_PAGE) + ->setFirstResult($offset) + ->getQuery() + ; + + return new Paginator($query); + } + // /** // * @return Comment[] Returns an array of Comment objects // */ diff --git a/templates/conference/show.html.twig b/templates/conference/show.html.twig index 043de7b..4fcec9d 100644 --- a/templates/conference/show.html.twig +++ b/templates/conference/show.html.twig @@ -6,6 +6,8 @@

{{ conference }} Conference

{% if comments|length > 0 %} +
There are {{ comments|length }} comments.
+ {% for comment in comments %} {% if comment.photoFilename %} @@ -18,6 +20,13 @@

{{ comment.text }}

{% endfor %} + + {% if previous >= 0 %} + Previous + {% endif %} + {% if next < comments|length %} + Next + {% endif %} {% else %}
No comments have been posted yet for this conference.
{% endif %}