refactor(fo): migrate HomeController to CQS queries with DTOs
This commit is contained in:
parent
9aab392350
commit
668151fdaf
11 changed files with 226 additions and 22 deletions
|
|
@ -4,10 +4,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Controllers\Fo\Pages;
|
||||
|
||||
use App\Contracts\Directories\ProductDirectory;
|
||||
use App\Contracts\Executor;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Range;
|
||||
use App\Models\Slide;
|
||||
use App\Queries\Products\GetPaginatedProductsQuery;
|
||||
use App\Queries\Products\GetPaginatedProductsQueryInput;
|
||||
use App\Queries\Ranges\GetPaginatedRangesQuery;
|
||||
use App\Queries\Ranges\GetPaginatedRangesQueryInput;
|
||||
use App\Queries\Slides\GetPaginatedSlidesQuery;
|
||||
use App\Queries\Slides\GetPaginatedSlidesQueryInput;
|
||||
use Domain\Models\ValueObjects\Pagination;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
||||
|
||||
|
|
@ -15,29 +20,46 @@ class HomeController extends Controller
|
|||
{
|
||||
public function __construct(
|
||||
private readonly AppCustomerAuthService $authService,
|
||||
private readonly ProductDirectory $productDirectory,
|
||||
private readonly Executor $executor,
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(): View
|
||||
{
|
||||
// * Star products.
|
||||
$starProductList = $this->productDirectory->getStarProductsWithPricing();
|
||||
public function index(
|
||||
GetPaginatedProductsQuery $productsAction,
|
||||
GetPaginatedSlidesQuery $slidesAction,
|
||||
GetPaginatedRangesQuery $rangesAction
|
||||
): View {
|
||||
|
||||
$slides = Slide::all();
|
||||
$starProductList = $this->executor->execute(
|
||||
$productsAction,
|
||||
new GetPaginatedProductsQueryInput(
|
||||
Pagination::firstPage(),
|
||||
starOnly: true,
|
||||
publishedOnly: true,
|
||||
withPricing: true,
|
||||
)
|
||||
)->items();
|
||||
|
||||
// * Display only slides visible to connected customers.
|
||||
if (! $this->authService->check()) {
|
||||
$slides = $slides->where('connected', false);
|
||||
}
|
||||
$slideList = $this->executor->execute(
|
||||
$slidesAction,
|
||||
new GetPaginatedSlidesQueryInput(
|
||||
Pagination::firstPage(),
|
||||
publishedOnly: true,
|
||||
forConnectedCustomers: $this->authService->check()
|
||||
)
|
||||
)->items();
|
||||
|
||||
$rangeList = Range::query()
|
||||
->where('status', true)
|
||||
->get();
|
||||
$rangeList = $this->executor->execute(
|
||||
$rangesAction,
|
||||
new GetPaginatedRangesQueryInput(
|
||||
Pagination::firstPage(),
|
||||
publishedOnly: true
|
||||
)
|
||||
)->items();
|
||||
|
||||
return view('fo.pages.home', compact(
|
||||
'starProductList',
|
||||
'slides',
|
||||
'slideList',
|
||||
'rangeList'
|
||||
));
|
||||
}
|
||||
|
|
|
|||
77
app/Queries/Products/GetPaginatedProductsQuery.php
Normal file
77
app/Queries/Products/GetPaginatedProductsQuery.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Queries\Products;
|
||||
|
||||
use App\Contracts\Query;
|
||||
use App\Contracts\QueryInput;
|
||||
use App\Dto\ProductDto;
|
||||
use App\Dto\ProductWithPricingDto;
|
||||
use App\Models\Product;
|
||||
use App\Services\DataTableService;
|
||||
use Domain\Ports\Repositories\ProductRepository;
|
||||
use Domain\Services\PricingService;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Query<\App\Queries\Products\GetPaginatedProductsQueryInput, \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\ProductDto|\App\Dto\ProductWithPricingDto>>
|
||||
*/
|
||||
final readonly class GetPaginatedProductsQuery implements Query
|
||||
{
|
||||
public function __construct(
|
||||
private DataTableService $dataTable,
|
||||
private ProductRepository $domainProductRepository,
|
||||
private PricingService $pricingService,
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function __invoke(QueryInput $input): LengthAwarePaginator
|
||||
{
|
||||
Assert::isAOf(
|
||||
$input,
|
||||
GetPaginatedProductsQueryInput::class,
|
||||
);
|
||||
|
||||
$query = Product::query();
|
||||
|
||||
if ($input->publishedOnly) {
|
||||
$query->where('status', true);
|
||||
}
|
||||
|
||||
if ($input->starOnly) {
|
||||
$query->where('star', true);
|
||||
}
|
||||
|
||||
if ($input->withPricing) {
|
||||
$query->with(['inco', 'validDiscount']);
|
||||
}
|
||||
|
||||
$this->dataTable->sortQuery(Product::class, $query);
|
||||
|
||||
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
||||
|
||||
if ($input->withPricing) {
|
||||
/** @var \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\ProductDto|\App\Dto\ProductWithPricingDto> $result */
|
||||
$result = $paginator->through(function (Product $product): ProductWithPricingDto {
|
||||
$domainProduct = $this->domainProductRepository->find($product->getId());
|
||||
Assert::notNull($domainProduct, "Domain product not found for ID {$product->id}");
|
||||
$pricing = $this->pricingService->calculateForProduct($domainProduct);
|
||||
|
||||
return new ProductWithPricingDto(
|
||||
product: $product,
|
||||
pricing: $pricing,
|
||||
);
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** @var \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\ProductDto|\App\Dto\ProductWithPricingDto> $result */
|
||||
$result = $paginator->through(fn (Product $product): ProductDto => ProductDto::fromModel($product));
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
19
app/Queries/Products/GetPaginatedProductsQueryInput.php
Normal file
19
app/Queries/Products/GetPaginatedProductsQueryInput.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Queries\Products;
|
||||
|
||||
use App\Contracts\QueryInput;
|
||||
use Domain\Models\ValueObjects\Pagination;
|
||||
|
||||
final readonly class GetPaginatedProductsQueryInput implements QueryInput
|
||||
{
|
||||
public function __construct(
|
||||
public Pagination $pagination,
|
||||
public bool $starOnly = false,
|
||||
public bool $publishedOnly = false,
|
||||
public bool $withPricing = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -33,6 +33,10 @@ final readonly class GetPaginatedRangesQuery implements Query
|
|||
|
||||
$query = Range::query();
|
||||
|
||||
if ($input->publishedOnly) {
|
||||
$query->where('status', true);
|
||||
}
|
||||
|
||||
$this->dataTable->sortQuery(Range::class, $query);
|
||||
|
||||
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ final readonly class GetPaginatedRangesQueryInput implements QueryInput
|
|||
{
|
||||
public function __construct(
|
||||
public Pagination $pagination,
|
||||
public bool $publishedOnly = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,14 @@ final readonly class GetPaginatedSlidesQuery implements Query
|
|||
|
||||
$query = Slide::query();
|
||||
|
||||
if ($input->publishedOnly) {
|
||||
$query->where('status', true);
|
||||
}
|
||||
|
||||
if (! $input->forConnectedCustomers) {
|
||||
$query->where('connected', false);
|
||||
}
|
||||
|
||||
$this->dataTable->sortQuery(Slide::class, $query);
|
||||
|
||||
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ final readonly class GetPaginatedSlidesQueryInput implements QueryInput
|
|||
{
|
||||
public function __construct(
|
||||
public Pagination $pagination,
|
||||
public bool $publishedOnly = false,
|
||||
public bool $forConnectedCustomers = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@
|
|||
|
||||
<li class="position-relative z-3 py-2 text-center">
|
||||
<li class="d-flex justify-content-between flex-row py-2">
|
||||
@if ($productWithPricing->product->inco->nutriscore)
|
||||
@if ($productWithPricing->product->inco?->nutriscore)
|
||||
<img class="object-fit-contain img-fluid mw-5" data-bs-toggle="tooltip"
|
||||
src="{{ Vite::asset("resources/assets/images/nutriscore/nutriscore-{$productWithPricing->product->inco->nutriscore->value()}.png") }}"
|
||||
title="{{ __('Nutriscore') }} {{ Str::upper($productWithPricing->product->inco->nutriscore->value()) }}"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
aria-roledescription="carousel" aria-label="{{ __('Main Slider') }}">
|
||||
|
||||
<div class="carousel-indicators" role="group" aria-label="{{ __('Choose slide') }}">
|
||||
@foreach ($slides as $slide)
|
||||
@foreach ($slideList as $slide)
|
||||
<button class="@if ($loop->first) active @endif" data-bs-target="#hero-carousel"
|
||||
data-bs-slide-to="{{ $loop->index }}" type="button" aria-current="@if ($loop->first) true @endif"
|
||||
aria-label="{{ __('Slide :number', ['number' => $loop->iteration]) }}">
|
||||
|
|
@ -11,9 +11,9 @@
|
|||
</div>
|
||||
|
||||
<div class="carousel-inner h-100">
|
||||
@foreach ($slides as $slide)
|
||||
@foreach ($slideList as $slide)
|
||||
<div class="carousel-item h-100 @if ($loop->first) active @endif" role="group" aria-roledescription="slide"
|
||||
aria-label="{{ $loop->iteration }} {{ __('of') }} {{ count($slides) }}">
|
||||
aria-label="{{ $loop->iteration }} {{ __('of') }} {{ count($slideList) }}">
|
||||
|
||||
<a class="d-flex justify-content-center align-items-center text-decoration-none px-md-5 h-100 w-100 overflow-hidden px-3"
|
||||
href="{{ $slide->link }}">
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
{{-- MAIN SLIDER --}}
|
||||
<div class="row">
|
||||
<div class="col p-0">
|
||||
@include('fo.home-parts.sliders.main', compact('slides'))
|
||||
@include('fo.home-parts.sliders.main', compact('slideList'))
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,22 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Models\Product;
|
||||
use App\Models\Range;
|
||||
use App\Models\Slide;
|
||||
use Database\Seeders\ContentSeeder;
|
||||
use Domain\Enums\ProductBrand;
|
||||
use Domain\Enums\SellUnit;
|
||||
use Domain\Enums\TaxRate;
|
||||
use Domain\Enums\Units\InvoicingUnit;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Money\Money;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
describe('HomeController', function () {
|
||||
it('can access home page', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$this->seed(ContentSeeder::class);
|
||||
View::share('PageList', Page::getCached()
|
||||
|
|
@ -21,4 +32,64 @@ describe('HomeController', function () {
|
|||
// THEN
|
||||
$response->assertOk();
|
||||
});
|
||||
|
||||
it('displays star products, slides and ranges', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$this->seed(ContentSeeder::class);
|
||||
View::share('PageList', Page::getCached()
|
||||
->mapWithKeys(fn ($page) => [
|
||||
$page->slug->value() => $page,
|
||||
]));
|
||||
|
||||
$range = new Range();
|
||||
$range->name = 'Boucherie';
|
||||
$range->slug = 'boucherie';
|
||||
$range->picture = 'ranges/boucherie.jpg';
|
||||
$range->picture_alt = 'Boucherie';
|
||||
$range->picture_title = 'Boucherie';
|
||||
$range->status = true;
|
||||
$range->order = 1;
|
||||
$range->save();
|
||||
|
||||
$slide = new Slide();
|
||||
$slide->title = 'Super Promo';
|
||||
$slide->status = true;
|
||||
$slide->order = 1;
|
||||
$slide->connected = false;
|
||||
$slide->link = 'https://example.com';
|
||||
$slide->date_start = Carbon::yesterday();
|
||||
$slide->date_end = Carbon::tomorrow();
|
||||
$slide->save();
|
||||
|
||||
$product = new Product();
|
||||
$product->id = (string) Uuid::uuid7();
|
||||
$product->name = 'Poulet Fermier';
|
||||
$product->brand = ProductBrand::loue;
|
||||
$product->slug = 'poulet-fermier';
|
||||
$product->description = 'Délicieux poulet fermier';
|
||||
$product->picture_alt = 'Poulet Fermier';
|
||||
$product->picture_title = 'Poulet Fermier';
|
||||
$product->picture_large_alt = 'Poulet Fermier';
|
||||
$product->picture_large_title = 'Poulet Fermier';
|
||||
$product->price = Money::EUR(1500);
|
||||
$product->invoicing_unit = InvoicingUnit::KGM;
|
||||
$product->sell_unit = SellUnit::KGM;
|
||||
$product->tax_rate = TaxRate::TVA_5_5;
|
||||
$product->stock = 10;
|
||||
$product->range_id = $range->id;
|
||||
$product->order = 1;
|
||||
$product->star = true;
|
||||
$product->status = true;
|
||||
$product->save();
|
||||
|
||||
// WHEN
|
||||
$response = $this->get('/');
|
||||
|
||||
// THEN
|
||||
$response->assertOk();
|
||||
$response->assertSee('Poulet Fermier');
|
||||
$response->assertSee('Super Promo');
|
||||
$response->assertSee('Boucherie');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue