Paginate posts (#9)
continuous-integration/drone/push Build is passing Details

Squashed commit of the following:
Author: matis.mazingue <matis.mazingue@etu.uca.fr>
Author: clfreville2 <clement.freville2@etu.uca.fr>
pull/10/head
Clément FRÉVILLE 1 year ago
parent 9640b3c102
commit b4a1ae592f

@ -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', [

@ -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<Post>
@ -16,6 +17,20 @@ class PostRepository extends ServiceEntityRepository
parent::__construct($registry, Post::class);
}
/**
* @param int $page
* @param int $limit
* @return Paginator<Post>
*/
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
// */

@ -0,0 +1,24 @@
{% set route = app.request.attributes.get('_route') %}
<nav>
<ul class="pagination">
<li class="page-item {{ page < 2 ? 'disabled' }}">
<a class="page-link" href="{{ path(route, {'page': page - 1}) }}">Previous</a>
</li>
{% if page > 1 %}
<li class="page-item">
<a class="page-link" href="{{ path(route, {'page': page - 1}) }}">{{ page - 1 }}</a>
</li>
{% endif %}
<li class="page-item active" aria-current="page">
<a class="page-link">{{ page }}</a>
</li>
{% if page + 1 <= maxPage %}
<li class="page-item">
<a class="page-link" href="{{ path(route, {'page': page + 1}) }}">{{ page + 1 }}</a>
</li>
{% endif %}
<li class="page-item {{ page + 1 > maxPage ? 'disabled' }}">
<a class="page-link" href="{{ path(route, {'page': page + 1}) }}">Next</a>
</li>
</ul>
</nav>

@ -0,0 +1,21 @@
{% extends 'base.html.twig' %}
{% block title %}Posts{% endblock %}
{% block body %}
{% for post in posts.iterator %}
<div class="card" style="width: 42rem; margin: 20px 0 50px 100px;">
<div class="card-body">
<h5 class="card-title">{{ post.species ? post.species.vernacularName : 'Post' }}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ post.foundDate | date("d/m/Y \\à H \\h") }}</h6>
<p class="card-subtitle mb-2 text-muted">{{ post.latitude }}, {{ post.longitude }}, {{ post.altitude }}m</p>
<p class="card-text">{{ post.commentary }}</p>
</div>
<div class="card-footer">
28 ❤️
128 💬
</div>
</div>
{% endfor %}
{% include '_pagination.html.twig' %}
{% endblock %}
Loading…
Cancel
Save