Feat: Updating COntrollers to remove business logic moved to boboko/core

This commit is contained in:
2026-09-03 11:47:38 +03:00
parent b87e22381f
commit b2207a622c
6 changed files with 56 additions and 50 deletions
+6 -6
View File
@@ -19,8 +19,8 @@ final class CategoryListing
{ {
private function __construct( private function __construct(
public readonly ?ProductSort $sort, public readonly ?ProductSort $sort,
public readonly ?int $minPrice, public readonly ?float $minPrice,
public readonly ?int $maxPrice, public readonly ?float $maxPrice,
public readonly bool $inStockOnly, public readonly bool $inStockOnly,
public readonly int $page, public readonly int $page,
) {} ) {}
@@ -29,8 +29,8 @@ public static function fromRequest(Request $request): self
{ {
return new self( return new self(
sort: ProductSort::tryFrom((string) $request->query('sort')), sort: ProductSort::tryFrom((string) $request->query('sort')),
minPrice: self::intOrNull($request->query('price_min')), minPrice: self::floatOrNull($request->query('price_min')),
maxPrice: self::intOrNull($request->query('price_max')), maxPrice: self::floatOrNull($request->query('price_max')),
inStockOnly: $request->boolean('in_stock'), inStockOnly: $request->boolean('in_stock'),
page: max(1, (int) $request->query('page', 1)), page: max(1, (int) $request->query('page', 1)),
); );
@@ -79,8 +79,8 @@ public function isRefined(): bool
|| $this->page > 1; || $this->page > 1;
} }
private static function intOrNull(mixed $value): ?int private static function floatOrNull(mixed $value): ?float
{ {
return is_numeric($value) ? (int) $value : null; return is_numeric($value) ? (float) $value : null;
} }
} }
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Catalog;
/**
* Presentation shaping — how a product listing/grid card is built from
* ProductService's localized array shape (list()/getById()/random() all
* return it). Deliberately not in boboko-core: `href` depends on this
* storefront's own routes, and another app built on the same core package
* could want an entirely different card shape. Kept in one place so
* HomeController/CategoryController don't each hand-write the same
* name/price/image/href mapping.
*/
final class ProductCard
{
/**
* @param array $product One item from ProductService's localized array shape.
* @return array{name: ?string, price: ?float, image: ?string, href: string}
*/
public static function fromIndexed(array $product): array
{
return [
'name' => $product['name'],
'price' => $product['price'],
'image' => $product['media'][0]['url'] ?? null,
'href' => route('product.show', ['id' => $product['id']]),
];
}
}
+13 -25
View File
@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Catalog\CategoryListing; use App\Catalog\CategoryListing;
use App\Catalog\ProductCard;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Modules\Core\Catalog\Services\CollectionService; use Modules\Core\Catalog\Services\CollectionService;
use Modules\Core\Catalog\Services\ProductService; use Modules\Core\Catalog\Services\ProductService;
@@ -23,42 +24,29 @@ public function show(string $locale, int $collection)
$filters = $listing->filters($collectionData['id']); $filters = $listing->filters($collectionData['id']);
$perPage = 12; $perPage = 12;
// Listing/filtering reads from the Meilisearch index via ProductService, // One call for both the product page and the price slider's bounds —
// not Eloquent — see Modules\Core\Catalog\Services\ProductService. list() returns a // see ProductService::list()'s own docblock for why a controller no
// real LengthAwarePaginator of plain arrays (already localized/flattened), // longer orchestrates list() + priceSliderBounds() itself.
// not Product models. Sort/filter/page all come from the query string via $listingResult = $this->products->list(
// CategoryListing, which is the single source of truth for that state.
$products = $this->products->list(
filters: $filters, filters: $filters,
perPage: $perPage, perPage: $perPage,
page: $listing->page, page: $listing->page,
sort: $listing->sort, sort: $listing->sort,
)->through(fn (array $product) => [ );
'name' => $product['name'],
'price' => $product['price'],
'image' => $product['media'][0]['url'] ?? null,
'href' => route('product.show', ['id' => $product['id']]),
])->appends($listing->query(['page' => null]));
// Slider bounds — the price span of everything matching the *other* $products = $listingResult->products
// filters (priceRange() drops the price filter itself, so the handles ->through(fn (array $product) => ProductCard::fromIndexed($product))
// don't collapse to whatever's already selected). Whole euros. ->appends($listing->query(['page' => null]));
$priceRange = $this->products->priceRange($filters);
$priceFloor = $priceRange['min'] !== null ? (int) floor($priceRange['min']) : null;
$priceCeil = $priceRange['max'] !== null ? (int) ceil($priceRange['max']) : null;
// A price param is only a real filter if it's tighter than the bounds — $priceBounds = $listingResult->priceBounds;
// drives whether the "clear" link shows.
$priceFiltered = ($listing->minPrice !== null && $listing->minPrice > ($priceFloor ?? PHP_INT_MIN))
|| ($listing->maxPrice !== null && $listing->maxPrice < ($priceCeil ?? PHP_INT_MAX));
return view('category.show', [ return view('category.show', [
'collection' => $collectionData, 'collection' => $collectionData,
'products' => $products, 'products' => $products,
'listing' => $listing, 'listing' => $listing,
'priceFloor' => $priceFloor, 'priceFloor' => $priceBounds->floor,
'priceCeil' => $priceCeil, 'priceCeil' => $priceBounds->ceil,
'priceFiltered' => $priceFiltered, 'priceFiltered' => $priceBounds->filtered,
]); ]);
} }
} }
+6 -11
View File
@@ -2,11 +2,14 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Catalog\ProductCard;
use App\Models\StoicPage; use App\Models\StoicPage;
use Lunar\Models\Product; use Modules\Core\Catalog\Services\ProductService;
class HomeController extends Controller class HomeController extends Controller
{ {
public function __construct(private readonly ProductService $products) {}
public function index(string $locale) public function index(string $locale)
{ {
$page = StoicPage::firstWhere('slug', 'home'); $page = StoicPage::firstWhere('slug', 'home');
@@ -15,16 +18,8 @@ public function index(string $locale)
abort(404); abort(404);
} }
$products = Product::with(['variants.prices.currency', 'media']) $products = collect($this->products->random(13))
->inRandomOrder() ->map(fn (array $product) => ProductCard::fromIndexed($product));
->limit(13)
->get()
->map(fn (Product $product) => [
'name' => $product->translateAttribute('name'),
'price' => $product->variants->first()?->prices->first()?->price->decimal,
'image' => $product->media->first()?->getUrl(),
'href' => route('product.show', ['id' => $product->id]),
]);
return view('home', [ return view('home', [
'page' => $page, 'page' => $page,
+1 -8
View File
@@ -16,14 +16,7 @@ public function show(string $locale, int $id)
$collection = $product['collections'][0] ?? null; $collection = $product['collections'][0] ?? null;
$variantsData = collect($product['variants']) $variantsData = $this->products->variantSummaries($product);
->map(fn (array $variant) => [
'id' => $variant['id'],
'price' => $variant['prices'][0]['price'] ?? null,
'image' => $variant['media'][0]['url'] ?? null,
])
->values()
->all();
$firstVariant = $product['variants'][0] ?? null; $firstVariant = $product['variants'][0] ?? null;
$option = $firstVariant['options'][0]['option'] ?? null; $option = $firstVariant['options'][0]['option'] ?? null;
+1
View File
@@ -86,6 +86,7 @@ php artisan lunar:install --quiet || true
# just shipped, same reasoning as optimize:clear above. # just shipped, same reasoning as optimize:clear above.
echo "[entrypoint] Syncing search indexes..." echo "[entrypoint] Syncing search indexes..."
php artisan lunar:meilisearch:setup php artisan lunar:meilisearch:setup
php artisan lunar:meilisearch:tune-product-search --quiet || true
php artisan lunar:search:index --quiet || true php artisan lunar:search:index --quiet || true
if [ "$APP_ENV" = "production" ]; then if [ "$APP_ENV" = "production" ]; then