diff --git a/src/Dto/SnipFilterRequest.php b/src/Dto/SnipFilterRequest.php index c1137e5..9326b91 100644 --- a/src/Dto/SnipFilterRequest.php +++ b/src/Dto/SnipFilterRequest.php @@ -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, ]; } } \ No newline at end of file diff --git a/src/Repository/SnipRepository.php b/src/Repository/SnipRepository.php index 0700694..71cf64f 100644 --- a/src/Repository/SnipRepository.php +++ b/src/Repository/SnipRepository.php @@ -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(); } diff --git a/templates/snip/index.html.twig b/templates/snip/index.html.twig index 69538c2..2c275ac 100644 --- a/templates/snip/index.html.twig +++ b/templates/snip/index.html.twig @@ -1,23 +1,12 @@ -{% extends 'base/one.column.html.twig' %} +{% extends 'base/two.column.html.twig' %} {% set title = 'My Snips' %} -{% block body %} +{% block column1 %} +

Snips

Add - {% if request.onlyVisible %} - Show hidden - {% else %} - Hide hidden - {% endif %} - - {% if request.sort != 'name' %} - Sort by name - {% endif %} - {% if request.sort != 'date' %} - Sort by date - {% endif %}

@@ -30,4 +19,29 @@ {% endfor %}
+{% endblock %} + +{% block column2 %} +

Filters

+ +
Ordering
+
+ {% for sortOption in ['name', 'date'] %} + + Sort by {{ sortOption|capitalize }} + + {% endfor %} +
+ +
+
Visibility
+
+ {% for visibilityOption in ['all', 'visible', 'hidden'] %} + + Show {{ visibilityOption|capitalize }} + + {% endfor %} +
{% endblock %} \ No newline at end of file