store/app/Http/Controllers/HomeController.php

81 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use App\Models\SystemConfiguration;
use App\Services\Manage\CuttingResultPriceResolver;
use App\Services\System\Setting\HomepageSettingService;
use App\Settings\SocialMediaSettings;
use App\Settings\SystemSettings;
use App\Support\Media\MediaPresenter;
use Inertia\Inertia;
use Inertia\Response;
class HomeController extends Controller
{
public function __construct(
private readonly CuttingResultPriceResolver $cuttingResultPriceResolver,
private readonly HomepageSettingService $homepageSettingService,
) {}
public function index(): Response
{
$categories = Category::whereHas('products')->get(['id', 'name', 'slug']);
$products = Product::where('is_active', true)
->with([
'categories',
'variants' => fn ($query) => $query
->with('media')
->orderBy('created_at'),
])
->get();
$products = $products->map(function ($product) {
$product->variants->each(function ($variant) {
$variant->setAttribute('images', MediaPresenter::collection($variant, 'images'));
$variant->setAttribute(
'prices',
collect($this->cuttingResultPriceResolver->latestPricesForVariant($variant->id))
->map(fn ($price) => [
'type' => $price->price_type->value,
'type_label' => $price->price_type->label(),
'price' => (int) $price->price,
'price_formatted' => $price->price_formatted,
])
->values()
->all(),
);
});
return $product;
});
$configuration = SystemConfiguration::instance();
$logo = MediaPresenter::first($configuration, 'logo');
$logoUrl = $logo['url'] ?? null;
$settings = app(SystemSettings::class);
$socialSettings = app(SocialMediaSettings::class);
$appName = $settings->app_name ?? 'DST Collection';
return Inertia::render('Home', [
'categories' => $categories,
'products' => $products,
'appName' => $appName,
'aboutApp' => $settings->about_app ?? '',
'contactEmail' => $settings->email ?? '',
'contactPhone' => $settings->phone ?? '',
'contactAddress' => $settings->address ?? '',
'logoUrl' => $logoUrl,
'instagramUrl' => $socialSettings->instagram_url ?? null,
'facebookUrl' => $socialSettings->facebook_url ?? null,
'tiktokUrl' => $socialSettings->tiktok_url ?? null,
'homepage' => $this->homepageSettingService->homepageData(),
]);
}
}