Merge branch 'elv' into cart-temp

This commit is contained in:
elvira
2026-09-04 13:21:58 +03:00
4 changed files with 55 additions and 119 deletions
+4 -19
View File
@@ -2,8 +2,6 @@
namespace App\Catalog;
use Lunar\Models\Product;
/**
* Presentation shaping — how a storefront product listing/grid card is built:
* name, price, image, href. Deliberately not in boboko-core: `href` depends on
@@ -12,26 +10,13 @@
* CategoryController / ProductController / SearchController don't each
* hand-write the same name/price/image/href mapping.
*
* Two sources, because the storefront reads products both ways: a hydrated
* Eloquent model (full-text search via ProductSearchService), or a localized
* index array (ProductService::list()/getById()/random()). Model callers must
* eager-load `variants.prices` and `media`.
* One source: a localized index array — ProductService::list()/getById()/
* random(), and now ProductSearchService::search() too, all return the exact
* same document shape (see ProductListingResult), so this is the only mapping
* every storefront listing page needs.
*/
final class ProductCard
{
/**
* @return array{name: ?string, price: ?string, image: ?string, href: string}
*/
public static function fromModel(Product $product): array
{
return [
'name' => $product->translateAttribute('name'), //@todo check this
'price' => $product->variants->first()?->prices->first()?->price->decimal, //@todo check this
'image' => $product->media->first()?->getUrl(),
'href' => route('product.show', ['id' => $product->id]),
];
}
/**
* @param array<string, mixed> $product one item from ProductService's localized array shape
* @return array{name: ?string, price: ?string, image: ?string, href: string}
+32 -18
View File
@@ -4,41 +4,56 @@
use Closure;
use Modules\Core\Catalog\Enums\ProductSort;
use Modules\Core\Catalog\Services\ProductSearchService;
use Modules\Core\Catalog\Services\ProductService;
/**
* Assembles the data the shared shop listing body (shop/partials/listing.blade.php)
* needs — the product page, price-slider bounds, sort links and the "clear price"
* link. Used by both the category page (scoped to a collection) and the
* all-products page; the `$url` closure turns a query-param array into a URL for
* whichever page is calling, so this class never has to know the route.
* link. Used by the category page (scoped to a collection), the all-products
* page, and the search page (scoped to a query — see $query below); the `$url`
* closure turns a query-param array into a URL for whichever page is calling,
* so this class never has to know the route.
*/
final class ProductListingPage
{
private const PER_PAGE = 12;
public function __construct(private readonly ProductService $products) {}
public function __construct(
private readonly ProductService $products,
private readonly ProductSearchService $search,
) {}
/**
* @param Closure(array<string, string|int>): string $url
* @param ?int $collectionId scope to a collection, or null for every product
* @param ?string $query scope to a text search — when given, calls
* @return array<string, mixed>
*/
public function build(ProductListing $listing, Closure $url, ?int $collectionId = null): array
public function build(ProductListing $listing, Closure $url, ?int $collectionId = null, ?string $query = null): array
{
$filters = $listing->filters($collectionId);
// Listing reads from the Meilisearch index via ProductService, not
// Eloquent. list() returns a ProductListingResult — the product page
// plus the price-slider bounds from one call; the controller no longer
// stitches list() + priceRange() together itself. Sort/filter/page all
// come from $listing (the query string).
$result = $this->products->list(
filters: $filters,
perPage: self::PER_PAGE,
page: $listing->page,
sort: $listing->sort,
);
// Listing reads from the Meilisearch index via ProductService/
// ProductSearchService, not Eloquent. Both return a
// ProductListingResult — the product page plus the price-slider
// bounds (and available tags) from one call; the controller no
// longer stitches list()/search() + priceRange() together itself.
// Sort/filter/page all come from $listing (the query string).
$result = $query !== null
? $this->search->search(
query: $query,
filters: $filters,
sort: $listing->sort,
perPage: self::PER_PAGE,
page: $listing->page,
)
: $this->products->list(
filters: $filters,
perPage: self::PER_PAGE,
page: $listing->page,
sort: $listing->sort,
);
$products = $result->products
->through(ProductCard::fromIndexed(...))
@@ -46,8 +61,7 @@ public function build(ProductListing $listing, Closure $url, ?int $collectionId
// Slider bounds — the price span of everything matching the *other*
// filters, rounded to whole euros, plus whether the current price params
// actually narrow that span. All computed in core now
// (ProductService::priceSliderBounds()).
// actually narrow that span. All computed in core
$priceBounds = $result->priceBounds;
return [
+10 -73
View File
@@ -2,93 +2,30 @@
namespace App\Http\Controllers;
use App\Catalog\ProductCard;
use App\Catalog\ProductSortOptions;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Lunar\Models\Product;
use Modules\Core\Catalog\Enums\ProductSort;
use Modules\Core\Catalog\Services\ProductSearchService;
use App\Catalog\ProductListing;
use App\Catalog\ProductListingPage;
class SearchController extends Controller
{
private const PER_PAGE = 12;
public function __construct(private readonly ProductSearchService $search) {}
public function __construct(private readonly ProductListingPage $listingPage) {}
public function show(string $locale)
{
$query = trim((string) request()->query('q', ''));
// Nothing to search for — send them to the all-products page rather than
// render an empty results page.
if ($query === '') {
return redirect()->route('products');
}
$sort = ProductSort::tryFrom((string) request()->query('sort'));
$page = max(1, (int) request()->query('page', 1));
$listing = ProductListing::fromRequest(request());
$cards = $this->cardsFor($query, $sort);
$products = new LengthAwarePaginator(
items: $cards->forPage($page, self::PER_PAGE)->values(),
total: $cards->count(),
perPage: self::PER_PAGE,
currentPage: $page,
options: ['path' => LengthAwarePaginator::resolveCurrentPath()],
);
$products->appends(array_filter(
['q' => $query, 'sort' => $sort?->value],
fn ($value) => $value !== null,
));
// Sort dropdown links: same query, swapped sort (default sort => no param).
$sortOptions = ProductSortOptions::build(
$sort,
fn (?ProductSort $option) => route('search', array_filter(
['q' => $query, 'sort' => $option?->value],
fn ($value) => $value !== null,
)),
$data = $this->listingPage->build(
$listing,
fn (array $overrides) => route('search', ['q' => $query] + $overrides),
collectionId: null,
query: $query,
);
return view('search.index', [
'query' => $query,
'products' => $products,
'sortOptions' => $sortOptions,
]);
}
/**
* Full-text matches as product-card arrays. ProductSearchService returns
* hydrated models in relevance order with no sort or pagination, so ordering
* is done here in PHP and the controller paginates the mapped collection.
*
* @return Collection<int, array<string, mixed>>
*/
private function cardsFor(string $query, ?ProductSort $sort): Collection
{
$results = $this->search->search($query)->load(['variants.prices', 'media']);
return $this->sortResults($results, $sort)
->map(ProductCard::fromModel(...))
->values();
}
/**
* @param EloquentCollection<int, Product> $results
* @return EloquentCollection<int, Product>
*/
private function sortResults(EloquentCollection $results, ?ProductSort $sort): EloquentCollection
{
$price = fn (Product $product) => $product->variants->first()?->prices->first()?->price->value ?? 0;
return match ($sort) {
ProductSort::PriceAsc => $results->sortBy($price)->values(),
ProductSort::PriceDesc => $results->sortByDesc($price)->values(),
ProductSort::Newest => $results->sortByDesc('created_at')->values(),
default => $results, // Meilisearch relevance order
};
return view('search.index', [...$data, 'query' => $query]);
}
}
Generated
+9 -9
View File
@@ -515,11 +515,11 @@
},
{
"name": "boboko/core",
"version": "0.13.1",
"version": "0.14.0",
"source": {
"type": "git",
"url": "https://code.radical-elements.com/boboko/core.git",
"reference": "e9aa08a3383f9dfa48564fd9b40d2193d5a1d2f3"
"reference": "4ff9bdacc3394bb02537998a2d43adbc2799768a"
},
"require": {
"laravel/framework": "^12.0",
@@ -567,7 +567,7 @@
}
},
"description": "Core module — authentication and shared panel behaviour",
"time": "2026-09-03T15:28:22+00:00"
"time": "2026-09-04T10:06:26+00:00"
},
{
"name": "brick/math",
@@ -2281,16 +2281,16 @@
},
{
"name": "google/protobuf",
"version": "v5.36.0",
"version": "v5.36.1",
"source": {
"type": "git",
"url": "https://github.com/protocolbuffers/protobuf-php.git",
"reference": "9c105104b54709ecd902494ab340ed2122789b2d"
"reference": "d64d16befba8632967f604b9644c0bb8f64cfbc3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/9c105104b54709ecd902494ab340ed2122789b2d",
"reference": "9c105104b54709ecd902494ab340ed2122789b2d",
"url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/d64d16befba8632967f604b9644c0bb8f64cfbc3",
"reference": "d64d16befba8632967f604b9644c0bb8f64cfbc3",
"shasum": ""
},
"require": {
@@ -2319,9 +2319,9 @@
"proto"
],
"support": {
"source": "https://github.com/protocolbuffers/protobuf-php/tree/v5.36.0"
"source": "https://github.com/protocolbuffers/protobuf-php/tree/v5.36.1"
},
"time": "2026-08-20T13:06:50+00:00"
"time": "2026-08-31T22:07:31+00:00"
},
{
"name": "graham-campbell/result-type",