feat: add brand and category relationships to products, update requests and resources, and seed product catalog from images

This commit is contained in:
2026-06-29 13:47:05 -03:00
parent 49771ebb13
commit 24235c5ca9
28 changed files with 454 additions and 14 deletions

View File

@@ -37,7 +37,7 @@ class ProductService
$this->syncProductImages($product, $images);
}
return $product->load(['attributes', 'attachments']);
return $product->load(['attributes', 'attachments', 'brand', 'category']);
});
}
@@ -68,7 +68,7 @@ class ProductService
$this->syncProductImages($product, $images);
}
return $product->load(['attributes', 'attachments']);
return $product->load(['attributes', 'attachments', 'brand', 'category']);
});
}
@@ -79,6 +79,7 @@ class ProductService
{
DB::transaction(function () use ($product) {
foreach ($product->variants as $variant) {
$this->deleteVariantAttachments($variant);
$product->deleteVariant($variant);
}
@@ -108,7 +109,7 @@ class ProductService
$variant = $product->createVariant($data);
if (! empty($images)) {
$this->syncImages($variant, $images);
$this->syncVariantImages($variant, $images);
}
return $variant->load(['product', 'definitions.attribute', 'attachments']);
@@ -132,7 +133,7 @@ class ProductService
$updatedVariant = $product->updateVariant($variant, $data);
if ($hasImages) {
$this->syncImages($updatedVariant, $images);
$this->syncVariantImages($updatedVariant, $images);
}
return $updatedVariant->load(['product', 'definitions.attribute', 'attachments']);
@@ -147,6 +148,7 @@ class ProductService
DB::transaction(function () use ($variant) {
/** @var Product $product */
$product = $variant->product;
$this->deleteVariantAttachments($variant);
$product->deleteVariant($variant);
});
}
@@ -183,14 +185,9 @@ class ProductService
*
* @param array<int, UploadedFile|string> $images
*/
protected function syncImages(ProductVariant $variant, array $images): void
protected function syncVariantImages(ProductVariant $variant, array $images): void
{
// Detach pivot record and delete attachment from S3 and database
$existing = $variant->attachments()->get();
$variant->attachments()->detach();
foreach ($existing as $attachment) {
$this->attachmentService->delete($attachment);
}
$this->deleteVariantAttachments($variant);
$attachmentIds = [];
@@ -205,7 +202,7 @@ class ProductService
/**
* Upload a list of image files/base64 strings and sync them to a product.
*
* Same logic as syncImages but for products without variants.
* Same logic as syncVariantImages but for products without variants.
*
* @param array<int, UploadedFile|string> $images
*/
@@ -228,6 +225,16 @@ class ProductService
$product->attachments()->sync($attachmentIds);
}
protected function deleteVariantAttachments(ProductVariant $variant): void
{
$existing = $variant->attachments()->get();
$variant->attachments()->detach();
foreach ($existing as $attachment) {
$this->attachmentService->delete($attachment);
}
}
/**
* Delete an attribute.
*/