Start working on sorting products by different columns.
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace Krzysiej\RyobiCrawler\Controller;
|
namespace Krzysiej\RyobiCrawler\Controller;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||||
|
|
||||||
@@ -17,4 +19,19 @@ class BaseController extends AbstractController
|
|||||||
$capsule->setAsGlobal();
|
$capsule->setAsGlobal();
|
||||||
$capsule->bootEloquent();
|
$capsule->bootEloquent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function handleOrderProducts(Builder $builder, Request $request): Builder
|
||||||
|
{
|
||||||
|
$builder->orderByDesc('starred')->orderByDesc('created_by');
|
||||||
|
if ($request->query->get('order')) {
|
||||||
|
$orderField = $request->query->get('order');
|
||||||
|
$direction = 'desc';
|
||||||
|
if (str_starts_with($orderField, '-')) {
|
||||||
|
$direction = 'asc';
|
||||||
|
}
|
||||||
|
$builder->orderBy($request->query->get('order'), $direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,17 @@
|
|||||||
namespace Krzysiej\RyobiCrawler\Controller;
|
namespace Krzysiej\RyobiCrawler\Controller;
|
||||||
|
|
||||||
use Krzysiej\RyobiCrawler\Models\Product;
|
use Krzysiej\RyobiCrawler\Models\Product;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
|
||||||
final class IndexController extends BaseController
|
final class IndexController extends BaseController
|
||||||
{
|
{
|
||||||
#[Route('/', name: 'app_home')]
|
#[Route('/', name: 'app_home')]
|
||||||
public function __invoke(): Response
|
public function __invoke(Request $request): Response
|
||||||
{
|
{
|
||||||
$products = Product::with(['currentStock', 'price'])
|
$products = Product::with(['currentStock', 'price', 'currentPrice']);
|
||||||
->orderByDesc('starred')
|
$products = $this->handleOrderProducts($products, $request)->get();
|
||||||
->orderByDesc('created_by')
|
|
||||||
->get();
|
|
||||||
return $this->render('productList.html.twig', ['products' => $products]);
|
return $this->render('productList.html.twig', ['products' => $products]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ final class NewController extends BaseController
|
|||||||
$date = (new \DateTime())->modify('-30 days')->format('Y-m-d');
|
$date = (new \DateTime())->modify('-30 days')->format('Y-m-d');
|
||||||
$products = Product::where('created_at', '>', $date)
|
$products = Product::where('created_at', '>', $date)
|
||||||
->orderByDesc('starred')
|
->orderByDesc('starred')
|
||||||
|
->orderByDesc('name')
|
||||||
->orderByDesc('created_by')
|
->orderByDesc('created_by')
|
||||||
->with(['currentPrice'])
|
->with(['currentPrice'])
|
||||||
->get();
|
->get();
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ final class SearchController extends BaseController
|
|||||||
public function __invoke(Request $request): Response
|
public function __invoke(Request $request): Response
|
||||||
{
|
{
|
||||||
$search = $request->query->get('search');
|
$search = $request->query->get('search');
|
||||||
//dd();
|
|
||||||
$products = Product::with('price')
|
$products = Product::with('price')
|
||||||
->orWhere([['name', 'like', "%$search%"]])
|
->orWhere([['name', 'like', "%$search%"]])
|
||||||
->orWhere([['subTitle', 'like', "%$search%"]])->get();
|
->orWhere([['subTitle', 'like', "%$search%"]])->get();
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
{% extends "template.html.twig" %}
|
{% extends "template.html.twig" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
{{ app.request.get('order') }}
|
||||||
<table class='table table-hover'>
|
<table class='table table-hover'>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th></th>
|
<th></th>
|
||||||
<th></th>
|
<th></th>
|
||||||
<th>Name</th>
|
<th><a href="{{ path( app.request.get('_route') , {'order': app.request.get('order') is same as('-name')?'name':'-name'|default('-name')}) }}">Name</a></th>
|
||||||
<th>Categories</th>
|
<th>Categories</th>
|
||||||
<th></th>
|
<th></th>
|
||||||
<th>Price</th>
|
<th><a href="{{ path( app.request.get('_route') , {'order': app.request.get('order') is same as('-price')?'price':'-price'|default('-price')}) }}">Price</a></th>
|
||||||
<th></th>
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|||||||
Reference in New Issue
Block a user