Files
3dealer/app/Http/Controllers/CategoryController.php
T

53 lines
1.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Catalog\CategoryListing;
use App\Catalog\ProductCard;
use Illuminate\Http\Response;
use Modules\Core\Catalog\Services\CollectionService;
use Modules\Core\Catalog\Services\ProductService;
class CategoryController extends Controller
{
public function __construct(
private readonly ProductService $products,
private readonly CollectionService $collections,
) {}
public function show(string $locale, int $collection)
{
$collectionData = $this->collections->getById($collection);
abort_if($collectionData === null, Response::HTTP_NOT_FOUND);
$listing = CategoryListing::fromRequest(request());
$filters = $listing->filters($collectionData['id']);
$perPage = 12;
// One call for both the product page and the price slider's bounds —
// see ProductService::list()'s own docblock for why a controller no
// longer orchestrates list() + priceSliderBounds() itself.
$listingResult = $this->products->list(
filters: $filters,
perPage: $perPage,
page: $listing->page,
sort: $listing->sort,
);
$products = $listingResult->products
->through(fn (array $product) => ProductCard::fromIndexed($product))
->appends($listing->query(['page' => null]));
$priceBounds = $listingResult->priceBounds;
return view('category.show', [
'collection' => $collectionData,
'products' => $products,
'listing' => $listing,
'priceFloor' => $priceBounds->floor,
'priceCeil' => $priceBounds->ceil,
'priceFiltered' => $priceBounds->filtered,
]);
}
}