diff --git a/app/Http/Controllers/Fo/Pages/HomeController.php b/app/Http/Controllers/Fo/Pages/HomeController.php index 107777e2..857c1b20 100644 --- a/app/Http/Controllers/Fo/Pages/HomeController.php +++ b/app/Http/Controllers/Fo/Pages/HomeController.php @@ -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' )); } diff --git a/app/Queries/Products/GetPaginatedProductsQuery.php b/app/Queries/Products/GetPaginatedProductsQuery.php new file mode 100644 index 00000000..afb09cee --- /dev/null +++ b/app/Queries/Products/GetPaginatedProductsQuery.php @@ -0,0 +1,77 @@ +> + */ +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 $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 $result */ + $result = $paginator->through(fn (Product $product): ProductDto => ProductDto::fromModel($product)); + + return $result; + } +} diff --git a/app/Queries/Products/GetPaginatedProductsQueryInput.php b/app/Queries/Products/GetPaginatedProductsQueryInput.php new file mode 100644 index 00000000..18a0fdb3 --- /dev/null +++ b/app/Queries/Products/GetPaginatedProductsQueryInput.php @@ -0,0 +1,19 @@ +publishedOnly) { + $query->where('status', true); + } + $this->dataTable->sortQuery(Range::class, $query); $paginator = $this->dataTable->paginateQuery($query, $input->pagination); diff --git a/app/Queries/Ranges/GetPaginatedRangesQueryInput.php b/app/Queries/Ranges/GetPaginatedRangesQueryInput.php index 0fa651e1..d8d3db23 100644 --- a/app/Queries/Ranges/GetPaginatedRangesQueryInput.php +++ b/app/Queries/Ranges/GetPaginatedRangesQueryInput.php @@ -11,6 +11,7 @@ final readonly class GetPaginatedRangesQueryInput implements QueryInput { public function __construct( public Pagination $pagination, + public bool $publishedOnly = false, ) { } } diff --git a/app/Queries/Slides/GetPaginatedSlidesQuery.php b/app/Queries/Slides/GetPaginatedSlidesQuery.php index d10bade9..63466f65 100644 --- a/app/Queries/Slides/GetPaginatedSlidesQuery.php +++ b/app/Queries/Slides/GetPaginatedSlidesQuery.php @@ -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); diff --git a/app/Queries/Slides/GetPaginatedSlidesQueryInput.php b/app/Queries/Slides/GetPaginatedSlidesQueryInput.php index 7a9ef1fe..024986f5 100644 --- a/app/Queries/Slides/GetPaginatedSlidesQueryInput.php +++ b/app/Queries/Slides/GetPaginatedSlidesQueryInput.php @@ -11,6 +11,8 @@ final readonly class GetPaginatedSlidesQueryInput implements QueryInput { public function __construct( public Pagination $pagination, + public bool $publishedOnly = false, + public bool $forConnectedCustomers = false, ) { } } diff --git a/resources/views/fo/home-parts/sliders/discounts.blade.php b/resources/views/fo/home-parts/sliders/discounts.blade.php index 524d3795..19a248e8 100644 --- a/resources/views/fo/home-parts/sliders/discounts.blade.php +++ b/resources/views/fo/home-parts/sliders/discounts.blade.php @@ -93,7 +93,7 @@
  • - @if ($productWithPricing->product->inco->nutriscore) + @if ($productWithPricing->product->inco?->nutriscore) product->inco->nutriscore->value()}.png") }}" title="{{ __('Nutriscore') }} {{ Str::upper($productWithPricing->product->inco->nutriscore->value()) }}" diff --git a/resources/views/fo/home-parts/sliders/main.blade.php b/resources/views/fo/home-parts/sliders/main.blade.php index 3d330b3e..46fcf0e7 100644 --- a/resources/views/fo/home-parts/sliders/main.blade.php +++ b/resources/views/fo/home-parts/sliders/main.blade.php @@ -2,7 +2,7 @@ aria-roledescription="carousel" aria-label="{{ __('Main Slider') }}">