feat: enhance product detail retrieval to support variant selection and fallback image handling

This commit is contained in:
2026-07-01 01:05:31 +00:00
parent 1aa831f921
commit 46685a7141
5 changed files with 191 additions and 35 deletions

View File

@@ -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);
}

View File

@@ -41,7 +41,11 @@ class ProductVariantController extends Controller
$producto = $this->resolveScopedProduct($tenant, $producto);
$productVariant = $this->resolveScopedVariant($producto, $productVariant);
return ProductVariantResource::make($productVariant->load(['product', 'definitions.productAttribute.attribute.options', 'attachments']));
return ProductVariantResource::make($productVariant->load([
'attachments' => fn ($query) => $query->orderBy('attachments.id'),
'definitions.productAttribute.attribute.options',
'product.attachments' => fn ($query) => $query->orderBy('attachments.id'),
]));
}
public function update(UpdateProductVariantRequest $request, Tenant $tenant, Product $producto, ProductVariant $productVariant, ProductService $productService): ProductVariantResource

View File

@@ -2,11 +2,12 @@
namespace App\Domains\Catalog\Resources;
use App\Domains\Catalog\Models\ProductVariant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin \App\Domains\Catalog\Models\ProductVariant
* @mixin ProductVariant
*/
class ProductVariantResource extends JsonResource
{
@@ -35,13 +36,19 @@ class ProductVariantResource extends JsonResource
->values();
}
// Fallback: use product-level attachments when the variant has none
$this->loadMissing('product.attachments');
if ($this->relationLoaded('fallbackAttachments')) {
return $this->fallbackAttachments
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values();
}
return $this->product?->attachments
?->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
?->values()
?? collect();
if ($this->relationLoaded('product') && $this->product?->relationLoaded('attachments')) {
return $this->product->attachments
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values();
}
return collect();
}),
];
}

View File

@@ -10,6 +10,7 @@ use App\Domains\Tenant\Models\Tenant;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ProductService
{
@@ -281,16 +282,35 @@ 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',
'attachments' => fn ($query) => $query->orderBy('attachments.id'),
'attributes.options',
'brand',
'category',
'variants' => fn ($query) => $query->orderBy('id'),
'variants.definitions.productAttribute.attribute.options',
]);
$selectedVariant = $variantId !== null
? $product->variants->firstWhere('id', $variantId)
: $product->variants->first(fn (ProductVariant $variant) => $variant->stock > 0);
if ($variantId !== null && $selectedVariant === null) {
throw new NotFoundHttpException('Product variant not found for product.');
}
$selectedVariant ??= $product->variants->first();
if ($selectedVariant !== null) {
$selectedVariant->load([
'attachments' => fn ($query) => $query->orderBy('attachments.id'),
]);
$selectedVariant->setRelation('fallbackAttachments', $product->attachments);
$product->setSelectedVariant($selectedVariant);
}
return $product;
}
}