feat: implement product management module with CRUD operations, service layer, and validation logic
This commit is contained in:
@@ -29,16 +29,12 @@ class ProductController extends Controller
|
||||
return ProductResource::make($product)->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
public function show(Tenant $tenant, Product $producto): ProductResource
|
||||
public function show(Tenant $tenant, Product $producto, ProductService $productService): ProductResource
|
||||
{
|
||||
$producto = $this->resolveScopedProduct($tenant, $producto);
|
||||
$producto = $productService->getProductDetail($tenant, $producto);
|
||||
|
||||
return ProductResource::make($producto->load([
|
||||
'attachments',
|
||||
'brand',
|
||||
'category',
|
||||
'attributes.options',
|
||||
]));
|
||||
return ProductResource::make($producto);
|
||||
}
|
||||
|
||||
public function update(UpdateProductRequest $request, Tenant $tenant, Product $producto, ProductService $productService): ProductResource
|
||||
|
||||
@@ -27,7 +27,8 @@ class Product extends Model
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'productos';
|
||||
|
||||
|
||||
protected ?ProductVariant $defaultVariant = null;
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
@@ -274,4 +275,16 @@ class Product extends Model
|
||||
$attribute->options()->delete();
|
||||
$attribute->delete();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function setDefaultVariant(ProductVariant $variant): void
|
||||
{
|
||||
$this->defaultVariant = $variant;
|
||||
}
|
||||
|
||||
public function getDefaultVariant(): ?ProductVariant
|
||||
{
|
||||
return $this->defaultVariant;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,22 @@ class ProductResource extends JsonResource
|
||||
->values()
|
||||
),
|
||||
'attributes' => AttributeResource::collection($this->whenLoaded('attributes')),
|
||||
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
|
||||
'default_variant' => $this->when(
|
||||
$this->getDefaultVariant() !== null,
|
||||
function () {
|
||||
$defaultVariant = $this->getDefaultVariant();
|
||||
return [
|
||||
'variant_id' => $defaultVariant->id,
|
||||
'images' => $defaultVariant->attachments->isNotEmpty()
|
||||
? $defaultVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
|
||||
'attributes' => $defaultVariant->definitions->mapWithKeys(function ($definition) {
|
||||
return [$definition->attribute?->codigo => $definition->value];
|
||||
})->toArray(),
|
||||
];
|
||||
}
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,4 +277,27 @@ class ProductService
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function getProductDetail(Tenant $tenant, Product $product): Product
|
||||
{
|
||||
$product->load([
|
||||
'attachments',
|
||||
'brand',
|
||||
'category',
|
||||
'attributes.options',
|
||||
]);
|
||||
|
||||
$defaultVariant = $product->variants()
|
||||
->with([
|
||||
'attachments',
|
||||
'definitions.attribute.options',
|
||||
])
|
||||
->first();
|
||||
|
||||
if ($defaultVariant) {
|
||||
$product->setDefaultVariant($defaultVariant);
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user