Implement snip sorting

This commit is contained in:
Tim 2025-05-04 16:58:11 +02:00
parent 59e068fbf7
commit f52058c250
3 changed files with 20 additions and 1 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -11,6 +11,14 @@
{% else %}
<a class="btn btn-secondary" href="{{ path('snip_index', {onlyVisible: true}) }}">Hide hidden</a>
{% endif %}
{% if request.sort != 'name' %}
<a class="btn btn-secondary" href="{{ path('snip_index', {sort: 'name'}) }}">Sort by name</a>
{% endif %}
{% if request.sort != 'date' %}
<a class="btn btn-secondary" href="{{ path('snip_index', {sort: 'date'}) }}">Sort by date</a>
{% endif %}
<br><br>
<div class="list-group">
{% for snip in snips %}