Commit f8e57532 by Lech Sawon

include simple sorting form

parent fb19fb6b
...@@ -22,12 +22,12 @@ final class CategoryController extends AbstractController ...@@ -22,12 +22,12 @@ final class CategoryController extends AbstractController
/** /**
* @Route("/", name="category_index", methods={"GET"}) * @Route("/", name="category_index", methods={"GET"})
*/ */
public function index(CategoryRepository $categoryRepository): Response public function index(Request $request, CategoryRepository $categoryRepository): Response
{ {
$this->denyAccessUnlessGranted('ROLE_USER'); $sorting = $request->query->get('sorting');
return $this->render('category/index.html.twig', [ return $this->render('category/index.html.twig', [
'categories' => $categoryRepository->findAll(), 'categories' => $categoryRepository->findSorted($sorting)
]); ]);
} }
......
...@@ -19,4 +19,20 @@ class CategoryRepository extends ServiceEntityRepository ...@@ -19,4 +19,20 @@ class CategoryRepository extends ServiceEntityRepository
parent::__construct($registry, Category::class); parent::__construct($registry, Category::class);
} }
public function findSorted($sorting)
{
$qb = $this
->createQueryBuilder('category');
if ('ASC' === $sorting) {
$qb->addOrderBy('category.name', 'ASC');
} elseif ('DESC' === $sorting) {
$qb->addOrderBy('category.name', 'DESC');
}
$query = $qb->getQuery();
return $query->getResult();
}
} }
<form>
<label>
<select name="sorting">
<option value="">Brak</option>
<option value="ASC">Spadająco</option>
<option value="DESC">Rosnąco</option>
</select>
</label>
<input type="submit"/>
</form>
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
{% block body %} {% block body %}
<h1>Category</h1> <h1>Category</h1>
{{ include('_embed/_simple_sorting.html.twig') }}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
......
...@@ -4,16 +4,7 @@ ...@@ -4,16 +4,7 @@
{% block body %} {% block body %}
<h1>Room index</h1> <h1>Room index</h1>
<form> {{ include('_embed/_simple_sorting.html.twig') }}
<label>
<select name="sorting">
<option value="">Brak</option>
<option value="ASC">Spadająco</option>
<option value="DESC">Rosnąco</option>
</select>
</label>
<input type="submit"/>
</form>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment