Rewrite filters to better ui
This commit is contained in:
@ -4,18 +4,23 @@ namespace App\Dto;
|
||||
|
||||
readonly class SnipFilterRequest implements CachableDtoInterface
|
||||
{
|
||||
public const string VISIBILITY_ALL = 'all';
|
||||
public const string VISIBILITY_VISIBLE = 'visible';
|
||||
public const string VISIBILITY_HIDDEN = 'hidden';
|
||||
|
||||
public const string SORT_NAME = 'name';
|
||||
public const string SORT_DATE = 'date';
|
||||
|
||||
public function __construct(
|
||||
public bool $onlyVisible = true,
|
||||
public ?string $visibility = self::VISIBILITY_VISIBLE,
|
||||
public ?string $sort = self::SORT_DATE,
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'onlyVisible' => $this->onlyVisible,
|
||||
'visibility' => $this->visibility,
|
||||
'sort' => $this->sort,
|
||||
];
|
||||
}
|
||||
}
|
@ -50,18 +50,29 @@ class SnipRepository extends ServiceEntityRepository
|
||||
->setParameter('user', $user)
|
||||
;
|
||||
|
||||
$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);
|
||||
switch ($request->visibility) {
|
||||
case SnipFilterRequest::VISIBILITY_ALL:
|
||||
break;
|
||||
case SnipFilterRequest::VISIBILITY_VISIBLE:
|
||||
$qb->andWhere('s.visible = true');
|
||||
break;
|
||||
case SnipFilterRequest::VISIBILITY_HIDDEN:
|
||||
$qb->andWhere('s.visible = false');
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException('Invalid visibility option: ', $request->visibility);
|
||||
}
|
||||
|
||||
switch ($request->sort) {
|
||||
case SnipFilterRequest::SORT_NAME:
|
||||
$qb->orderBy('s.name', 'ASC');
|
||||
break;
|
||||
case SnipFilterRequest::SORT_DATE:
|
||||
$qb->orderBy('s.createdAt', 'DESC');
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException('Invalid sort option: ', $request->sort);
|
||||
}
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user