generated from boboko/starter
Feat: Updating Home, Product and Category Controllers and veiws to utilize the product service
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Lunar\Models\Collection;
|
||||
use Modules\Core\Catalog\ProductFilters;
|
||||
use Modules\Core\Catalog\ProductService;
|
||||
@@ -19,29 +18,19 @@ public function show(string $locale, Collection $collection)
|
||||
$page = (int) request('page', 1);
|
||||
|
||||
// Listing/filtering reads from the Meilisearch index via ProductService,
|
||||
// not Eloquent — see Modules\Core\Catalog\ProductService. It returns plain
|
||||
// arrays (already localized/flattened), not Product models.
|
||||
$result = $this->products->list(
|
||||
// not Eloquent — see Modules\Core\Catalog\ProductService. list() returns a
|
||||
// real LengthAwarePaginator of plain arrays (already localized/flattened),
|
||||
// not Product models.
|
||||
$products = $this->products->list(
|
||||
filters: new ProductFilters(collectionId: $collection->id),
|
||||
perPage: $perPage,
|
||||
page: $page,
|
||||
);
|
||||
|
||||
$products = new LengthAwarePaginator(
|
||||
items: collect($result['data'])->map(fn (array $product) => [
|
||||
'name' => $product['name'],
|
||||
'price' => $product['price'],
|
||||
'image' => $product['media'][0]['url'] ?? null,
|
||||
'href' => route('product.show', ['product' => $product['id']]),
|
||||
]),
|
||||
total: $result['meta']['total'],
|
||||
perPage: $result['meta']['per_page'],
|
||||
currentPage: $result['meta']['current_page'],
|
||||
options: [
|
||||
'path' => request()->url(),
|
||||
'query' => request()->query(),
|
||||
],
|
||||
);
|
||||
)->through(fn (array $product) => [
|
||||
'name' => $product['name'],
|
||||
'price' => $product['price'],
|
||||
'image' => $product['media'][0]['url'] ?? null,
|
||||
'href' => route('product.show', ['id' => $product['id']]),
|
||||
]);
|
||||
|
||||
return view('category.show', [
|
||||
'collection' => $collection,
|
||||
|
||||
@@ -23,7 +23,7 @@ public function index(string $locale)
|
||||
'name' => $product->translateAttribute('name'),
|
||||
'price' => $product->variants->first()?->prices->first()?->price->decimal,
|
||||
'image' => $product->media->first()?->getUrl(),
|
||||
'href' => route('product.show', ['product' => $product]),
|
||||
'href' => route('product.show', ['id' => $product->id]),
|
||||
]);
|
||||
|
||||
return view('home', [
|
||||
|
||||
@@ -2,44 +2,44 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Lunar\Models\Product;
|
||||
use Illuminate\Http\Response;
|
||||
use Lunar\Models\Collection;
|
||||
use Modules\Core\Catalog\ProductService;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function show(string $locale, Product $product)
|
||||
public function __construct(private readonly ProductService $products) {}
|
||||
|
||||
public function show(string $locale, int $id)
|
||||
{
|
||||
$product->load([
|
||||
"variants.prices.currency",
|
||||
"variants.values.option",
|
||||
"media",
|
||||
"collections",
|
||||
]);
|
||||
$product = $this->products->getById($id);
|
||||
|
||||
$option = $product->variants->first()?->values->first()?->option;
|
||||
abort_if($product === null, Response::HTTP_NOT_FOUND);
|
||||
|
||||
$variantsData = $product->variants
|
||||
->map(
|
||||
fn($v) => [
|
||||
"id" => $v->id,
|
||||
"price" => $v->prices->first()?->price->decimal,
|
||||
"image" => null, // variant-level media not differentiated yet
|
||||
],
|
||||
)
|
||||
$collection = $product['collections'][0] ?? null;
|
||||
$collectionModel = $collection !== null ? Collection::find($collection) : null;
|
||||
|
||||
$variantsData = collect($product['variants'])
|
||||
->map(fn (array $variant) => [
|
||||
'id' => $variant['id'],
|
||||
'price' => $variant['prices'][0]['price'] ?? null,
|
||||
'image' => $variant['media'][0]['url'] ?? null,
|
||||
])
|
||||
->values()
|
||||
->toArray();
|
||||
->all();
|
||||
|
||||
$firstImage = $product->media->first()?->getUrl();
|
||||
$firstVariant = $product['variants'][0] ?? null;
|
||||
$option = $firstVariant['options'][0]['option'] ?? null;
|
||||
|
||||
// temp categories here
|
||||
$categories = \Lunar\Models\Collection::orderBy("_lft")->get();
|
||||
$categories = Collection::orderBy('_lft')->get();
|
||||
|
||||
// dd($product);
|
||||
|
||||
return view("product.show", [
|
||||
"categories" => $categories,
|
||||
"product" => $product,
|
||||
"option" => $option,
|
||||
"variantsData" => $variantsData,
|
||||
return view('product.show', [
|
||||
'categories' => $categories,
|
||||
'collection' => $collectionModel,
|
||||
'product' => $product,
|
||||
'option' => $option,
|
||||
'variantsData' => $variantsData,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user