feat: implement product and attribute management services with corresponding controllers and feature tests

This commit is contained in:
2026-06-29 11:37:22 -03:00
parent b5b57a753c
commit 9b7d728117
7 changed files with 365 additions and 137 deletions

View File

@@ -6,6 +6,7 @@ use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Requests\StoreAttributeRequest;
use App\Domains\Catalog\Requests\UpdateAttributeRequest;
use App\Domains\Catalog\Resources\AttributeResource;
use App\Domains\Catalog\Services\ProductService;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Http\Controllers\Controller;
@@ -28,26 +29,7 @@ class AttributeController extends Controller
public function store(StoreAttributeRequest $request, Tenant $tenant): JsonResponse
{
$attribute = DB::transaction(function () use ($request, $tenant): Attribute {
$validated = $request->validated();
$options = $validated['options'] ?? [];
unset($validated['options']);
$type = FieldType::from((string) $validated['type']);
if (! $type->supportsOptions()) {
$validated['metadata_schema'] = null;
$options = [];
}
/** @var Attribute $attribute */
$attribute = Attribute::query()->create([
...$validated,
'tenant_codigo' => $tenant->codigo,
]);
$attribute->options()->createMany($options);
return $attribute->load('options');
});
$attribute = ProductService::createAttribute($tenant, $request->validated());
return AttributeResource::make($attribute)->response()->setStatusCode(201);
}
@@ -62,24 +44,7 @@ class AttributeController extends Controller
public function update(UpdateAttributeRequest $request, Tenant $tenant, Attribute $attribute): AttributeResource
{
$attribute = $this->resolveScopedAttribute($tenant, $attribute);
$attribute = DB::transaction(function () use ($request, $attribute): Attribute {
$validated = $request->validated();
$options = $validated['options'] ?? [];
unset($validated['options']);
$type = FieldType::from((string) $validated['type']);
if (! $type->supportsOptions()) {
$validated['metadata_schema'] = null;
$options = [];
}
$attribute->update($validated);
$attribute->options()->delete();
$attribute->options()->createMany($options);
return $attribute->load('options');
});
$attribute = ProductService::updateAttribute($attribute, $request->validated());
return AttributeResource::make($attribute);
}
@@ -87,7 +52,7 @@ class AttributeController extends Controller
public function destroy(Tenant $tenant, Attribute $attribute): Response
{
$attribute = $this->resolveScopedAttribute($tenant, $attribute);
$attribute->delete();
ProductService::deleteAttribute($attribute);
return response()->noContent();
}