From f52058c250ad82fdfaa15f22f9b76b5d3322a954 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 4 May 2025 16:58:11 +0200 Subject: [PATCH] Implement snip sorting --- src/Dto/SnipFilterRequest.php | 4 ++++ src/Repository/SnipRepository.php | 9 ++++++++- templates/snip/index.html.twig | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Dto/SnipFilterRequest.php b/src/Dto/SnipFilterRequest.php index 79f334b..c1137e5 100644 --- a/src/Dto/SnipFilterRequest.php +++ b/src/Dto/SnipFilterRequest.php @@ -4,8 +4,12 @@ namespace App\Dto; readonly class SnipFilterRequest implements CachableDtoInterface { + public const string SORT_NAME = 'name'; + public const string SORT_DATE = 'date'; + public function __construct( public bool $onlyVisible = true, + public ?string $sort = self::SORT_DATE, ) {} public function toArray(): array diff --git a/src/Repository/SnipRepository.php b/src/Repository/SnipRepository.php index 0d92a97..0700694 100644 --- a/src/Repository/SnipRepository.php +++ b/src/Repository/SnipRepository.php @@ -48,13 +48,20 @@ class SnipRepository extends ServiceEntityRepository ->createQueryBuilder('s') ->where('s.createdBy = :user') ->setParameter('user', $user) - ->orderBy('s.name', 'ASC') ; $qb->andWhere('s.visible = :visible') ->setParameter('visible', $request->onlyVisible) ; + if ($request->sort === SnipFilterRequest::SORT_NAME) { + $qb->orderBy('s.name', 'ASC'); + } elseif ($request->sort === SnipFilterRequest::SORT_DATE) { + $qb->orderBy('s.createdAt', 'DESC'); + } else { + throw new \InvalidArgumentException('Invalid sort option: ', $request->sort); + } + return $qb->getQuery()->getResult(); } diff --git a/templates/snip/index.html.twig b/templates/snip/index.html.twig index 864e2d5..69538c2 100644 --- a/templates/snip/index.html.twig +++ b/templates/snip/index.html.twig @@ -11,6 +11,14 @@ {% else %} Hide hidden {% endif %} + + {% if request.sort != 'name' %} + Sort by name + {% endif %} + {% if request.sort != 'date' %} + Sort by date + {% endif %} +

{% for snip in snips %}