Implement snip sorting
This commit is contained in:
parent
59e068fbf7
commit
f52058c250
@ -4,8 +4,12 @@ namespace App\Dto;
|
|||||||
|
|
||||||
readonly class SnipFilterRequest implements CachableDtoInterface
|
readonly class SnipFilterRequest implements CachableDtoInterface
|
||||||
{
|
{
|
||||||
|
public const string SORT_NAME = 'name';
|
||||||
|
public const string SORT_DATE = 'date';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public bool $onlyVisible = true,
|
public bool $onlyVisible = true,
|
||||||
|
public ?string $sort = self::SORT_DATE,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
|
@ -48,13 +48,20 @@ class SnipRepository extends ServiceEntityRepository
|
|||||||
->createQueryBuilder('s')
|
->createQueryBuilder('s')
|
||||||
->where('s.createdBy = :user')
|
->where('s.createdBy = :user')
|
||||||
->setParameter('user', $user)
|
->setParameter('user', $user)
|
||||||
->orderBy('s.name', 'ASC')
|
|
||||||
;
|
;
|
||||||
|
|
||||||
$qb->andWhere('s.visible = :visible')
|
$qb->andWhere('s.visible = :visible')
|
||||||
->setParameter('visible', $request->onlyVisible)
|
->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();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,14 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<a class="btn btn-secondary" href="{{ path('snip_index', {onlyVisible: true}) }}">Hide hidden</a>
|
<a class="btn btn-secondary" href="{{ path('snip_index', {onlyVisible: true}) }}">Hide hidden</a>
|
||||||
{% endif %}
|
{% 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>
|
<br><br>
|
||||||
<div class="list-group">
|
<div class="list-group">
|
||||||
{% for snip in snips %}
|
{% for snip in snips %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user