- Refactor Bo\ProductsController to use ActionExecutor and Queries - Refactor Fo\Pages\ProductsController and Fo\Ajax\ProductsController to CQS - Add Actions: CreateProduct, UpdateProduct, DeleteProduct with Inputs - Add Queries: GetBoPaginatedProductsQuery, GetProductFormDataQuery, GetFoProductIndexPageQuery, GetFoFilteredProductsQuery - Add DTOs: ProductFormDataDto and enrich ProductDto - Encapsulate Action/Query inputs conversion in FormRequests - Remove obsolete ProductDirectory and EloquentProductDirectory - Add tests for Fo\Ajax\ProductsController
90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Queries\Products;
|
|
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use App\Dto\ProductDto;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use App\Models\Tag;
|
|
use App\Services\DataTableService;
|
|
use Domain\Enums\ProductBrand;
|
|
use Illuminate\Contracts\Database\Query\Builder;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Query<\App\Queries\Products\GetBoPaginatedProductsQueryInput, \App\Queries\Products\GetBoPaginatedProductsQueryOutput>
|
|
*/
|
|
final readonly class GetBoPaginatedProductsQuery implements Query
|
|
{
|
|
public function __construct(
|
|
private DataTableService $dataTable,
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function __invoke(QueryInput $input): GetBoPaginatedProductsQueryOutput
|
|
{
|
|
Assert::isAOf($input, GetBoPaginatedProductsQueryInput::class);
|
|
|
|
$query = Product::query()->with(['inco', 'range', 'discount', 'validDiscount']);
|
|
|
|
if (! empty($input->search)) {
|
|
$this->dataTable->searchQuery(
|
|
Product::class,
|
|
$query,
|
|
$input->search,
|
|
fn ($q) => $q->orWhereHas(
|
|
'inco',
|
|
fn ($q) => $q->whereLike('no_art', "%{$input->search}%")
|
|
),
|
|
'name',
|
|
);
|
|
}
|
|
|
|
if (! empty($input->brand)) {
|
|
$query->where('brand', $input->brand);
|
|
}
|
|
|
|
if ($input->starred) {
|
|
$query->where('star', true);
|
|
}
|
|
|
|
if ($input->discount) {
|
|
$query->whereHas('discount');
|
|
}
|
|
|
|
if (! empty($input->tag)) {
|
|
$query->whereHas('tags', fn (Builder $q) => $q->where('id', $input->tag));
|
|
}
|
|
|
|
if (! empty($input->range)) {
|
|
$query->whereHas('range', fn (Builder $q) => $q->where('id', $input->range));
|
|
}
|
|
|
|
$this->dataTable->sortQuery(Product::class, $query, ['inco']);
|
|
|
|
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
|
/** @var \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, ProductDto> $productsDtoPaginator */
|
|
$productsDtoPaginator = $paginator->through(fn (Product $product): ProductDto => ProductDto::fromModel($product));
|
|
|
|
$brands = ProductBrand::cases();
|
|
$ranges = Range::query()->pluck('name', 'id');
|
|
$tags = Tag::query()->pluck('label', 'id');
|
|
$searchFields = str(collect([
|
|
trans('validation.attributes.name'),
|
|
trans('validation.custom.attributes.inco_id'),
|
|
])->implode(', '))->title()->value();
|
|
|
|
return new GetBoPaginatedProductsQueryOutput(
|
|
products: $productsDtoPaginator,
|
|
brands: $brands,
|
|
ranges: $ranges,
|
|
tags: $tags,
|
|
searchFields: $searchFields,
|
|
);
|
|
}
|
|
}
|