Paginated comments
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Entity\Conference;
|
|||||||
use App\Repository\CommentRepository;
|
use App\Repository\CommentRepository;
|
||||||
use App\Repository\ConferenceRepository;
|
use App\Repository\ConferenceRepository;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
@@ -21,11 +22,16 @@ class ConferenceController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/conference/{id}', name: 'conference')]
|
#[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', [
|
return new Response($twig->render('conference/show.html.twig', [
|
||||||
'conference' => $conference,
|
'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)
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,37 @@
|
|||||||
namespace App\Repository;
|
namespace App\Repository;
|
||||||
|
|
||||||
use App\Entity\Comment;
|
use App\Entity\Comment;
|
||||||
|
use App\Entity\Conference;
|
||||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||||
use Doctrine\Persistence\ManagerRegistry;
|
use Doctrine\Persistence\ManagerRegistry;
|
||||||
|
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends ServiceEntityRepository<Comment>
|
* @extends ServiceEntityRepository<Comment>
|
||||||
*/
|
*/
|
||||||
class CommentRepository extends ServiceEntityRepository
|
class CommentRepository extends ServiceEntityRepository
|
||||||
{
|
{
|
||||||
|
public const COMMENTS_PER_PAGE = 2;
|
||||||
|
|
||||||
public function __construct(ManagerRegistry $registry)
|
public function __construct(ManagerRegistry $registry)
|
||||||
{
|
{
|
||||||
parent::__construct($registry, Comment::class);
|
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
|
// * @return Comment[] Returns an array of Comment objects
|
||||||
// */
|
// */
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
<h2>{{ conference }} Conference<h2>
|
<h2>{{ conference }} Conference<h2>
|
||||||
|
|
||||||
{% if comments|length > 0 %}
|
{% if comments|length > 0 %}
|
||||||
|
<div>There are {{ comments|length }} comments.</div>
|
||||||
|
|
||||||
{% for comment in comments %}
|
{% for comment in comments %}
|
||||||
{% if comment.photoFilename %}
|
{% if comment.photoFilename %}
|
||||||
<img src="{{ asset('uploads/photos/' ~ comment.photofilename) }}" style="max-width: 200px"/>
|
<img src="{{ asset('uploads/photos/' ~ comment.photofilename) }}" style="max-width: 200px"/>
|
||||||
@@ -18,6 +20,13 @@
|
|||||||
|
|
||||||
<p>{{ comment.text }}</p>
|
<p>{{ comment.text }}</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{% if previous >= 0 %}
|
||||||
|
<a href="{{ path('conference', { id: conference.id, offset: previous }) }}">Previous</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if next < comments|length %}
|
||||||
|
<a href="{{ path('conference', { id: conference.id, offset: next }) }}">Next</a>
|
||||||
|
{% endif %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<div>No comments have been posted yet for this conference.</div>
|
<div>No comments have been posted yet for this conference.</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
Reference in New Issue
Block a user