76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Product;
|
|
|
|
use App\Repositories\Product\ProductRepository;
|
|
|
|
class ProductService
|
|
{
|
|
public function __construct(protected ProductRepository $repository) {}
|
|
|
|
|
|
public function getListSceneData($categoryOrSlug, $search = null)
|
|
{
|
|
$allCategories = $this->repository->getActiveCategories();
|
|
$parentCategories = $allCategories->whereNull('parent_id')->values();
|
|
|
|
// 현재 선택된 카테고리 객체 찾기
|
|
$currentCategory = is_numeric($categoryOrSlug)
|
|
? $allCategories->where('id', $categoryOrSlug)->first()
|
|
: $allCategories->where('slug', $categoryOrSlug)->first();
|
|
|
|
$menuItems = $parentCategories->map(function($parent) use ($allCategories, $currentCategory) {
|
|
$children = $allCategories->where('parent_id', $parent->id)->values();
|
|
|
|
return [
|
|
'id' => $parent->id,
|
|
'slug' => $parent->slug, // 슬러그 추가
|
|
'label' => $parent->name,
|
|
'url' => route('web.product.index', ['category' => $parent->slug]), // ID 대신 Slug로 URL 생성
|
|
'children' => $children->map(function($child) {
|
|
return [
|
|
'id' => $child->id,
|
|
'slug' => $child->slug,
|
|
'label' => $child->name,
|
|
'url' => route('web.product.index', ['category' => $child->slug]),
|
|
];
|
|
})
|
|
];
|
|
});
|
|
|
|
$products = $this->repository->getActiveProducts($categoryOrSlug, $search);
|
|
|
|
return [
|
|
'menuItems' => $menuItems,
|
|
'products' => $products,
|
|
'currentCategory' => $currentCategory,
|
|
'currentCategoryName' => $search ? "'{$search}' 검색 결과" : ($currentCategory ? $currentCategory->name : '전체 상품')
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 상품 상세 페이지 데이터 구성
|
|
*/
|
|
public function getProductDetailData(int $id)
|
|
{
|
|
$product = $this->repository->getProductById($id);
|
|
if (!$product) return null;
|
|
|
|
$skus = $this->repository->getSkusByProductId($id);
|
|
|
|
// 허용된 결제수단 조회
|
|
$allowedPayments = json_decode($product->allowed_payments ?? '[]', true);
|
|
$payments = $this->repository->getPaymentMethodsByIds($allowedPayments);
|
|
|
|
// 사이드바용 카테고리 데이터 (기존 메서드 활용)
|
|
$menuData = $this->getListSceneData(null);
|
|
|
|
return [
|
|
'product' => $product,
|
|
'skus' => $skus,
|
|
'payments' => $payments,
|
|
'menuItems' => $menuData['menuItems'],
|
|
];
|
|
}
|
|
}
|