feat: enhance product detail retrieval to support variant selection and update resource fields
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Domains\Catalog\Services\ProductService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@@ -29,10 +30,12 @@ class ProductController extends Controller
|
||||
return ProductResource::make($product)->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
public function show(Tenant $tenant, Product $producto, ProductService $productService): ProductResource
|
||||
public function show(Request $request, Tenant $tenant, Product $producto, ProductService $productService): ProductResource
|
||||
{
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
$producto = $productService->getProductDetail($tenant, $producto);
|
||||
$variantId = $request->query('variant_id');
|
||||
$variantId = is_numeric($variantId) ? (int) $variantId : null;
|
||||
$producto = $productService->getProductDetail($tenant, $producto, $variantId);
|
||||
|
||||
return ProductResource::make($producto);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class Product extends Model
|
||||
|
||||
protected $table = 'productos';
|
||||
|
||||
protected ?ProductVariant $defaultVariant = null;
|
||||
protected ?ProductVariant $selectedVariant = null;
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
@@ -278,13 +278,13 @@ class Product extends Model
|
||||
|
||||
|
||||
|
||||
public function setDefaultVariant(ProductVariant $variant): void
|
||||
public function setSelectedVariant(ProductVariant $variant): void
|
||||
{
|
||||
$this->defaultVariant = $variant;
|
||||
$this->selectedVariant = $variant;
|
||||
}
|
||||
|
||||
public function getDefaultVariant(): ?ProductVariant
|
||||
public function getSelectedVariant(): ?ProductVariant
|
||||
{
|
||||
return $this->defaultVariant;
|
||||
return $this->selectedVariant;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,16 +32,17 @@ class ProductResource extends JsonResource
|
||||
),
|
||||
'attributes' => AttributeResource::collection($this->whenLoaded('attributes')),
|
||||
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
|
||||
'default_variant' => $this->when(
|
||||
$this->getDefaultVariant() !== null,
|
||||
'variant' => $this->when(
|
||||
$this->getSelectedVariant() !== null,
|
||||
function () {
|
||||
$defaultVariant = $this->getDefaultVariant();
|
||||
$selectedVariant = $this->getSelectedVariant();
|
||||
|
||||
return [
|
||||
'variant_id' => $defaultVariant->id,
|
||||
'images' => $defaultVariant->attachments->isNotEmpty()
|
||||
? $defaultVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
'variant_id' => $selectedVariant->id,
|
||||
'images' => $selectedVariant->attachments->isNotEmpty()
|
||||
? $selectedVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
|
||||
'attributes' => $defaultVariant->definitions->mapWithKeys(function ($definition) {
|
||||
'attributes' => $selectedVariant->definitions->mapWithKeys(function ($definition) {
|
||||
return [$definition->attribute?->codigo => $definition->value];
|
||||
})->toArray(),
|
||||
];
|
||||
|
||||
@@ -279,7 +279,7 @@ class ProductService
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function getProductDetail(Tenant $tenant, Product $product): Product
|
||||
public function getProductDetail(Tenant $tenant, Product $product, ?int $variantId = null): Product
|
||||
{
|
||||
$product->load([
|
||||
'attachments',
|
||||
@@ -288,15 +288,27 @@ class ProductService
|
||||
'attributes.options',
|
||||
]);
|
||||
|
||||
$defaultVariant = $product->variants()
|
||||
$selectedVariantQuery = $product->variants()
|
||||
->with([
|
||||
'attachments',
|
||||
'definitions.attribute.options',
|
||||
])
|
||||
->first();
|
||||
]);
|
||||
|
||||
if ($defaultVariant) {
|
||||
$product->setDefaultVariant($defaultVariant);
|
||||
$selectedVariant = $variantId !== null
|
||||
? $selectedVariantQuery->whereKey($variantId)->first()
|
||||
: null;
|
||||
|
||||
if ($selectedVariant === null) {
|
||||
$selectedVariant = $product->variants()
|
||||
->with([
|
||||
'attachments',
|
||||
'definitions.attribute.options',
|
||||
])
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($selectedVariant) {
|
||||
$product->setSelectedVariant($selectedVariant);
|
||||
}
|
||||
|
||||
return $product;
|
||||
|
||||
Reference in New Issue
Block a user