104 lines
3.4 KiB
PHP
104 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Controllers;
|
|
|
|
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\Shared\Enums\FieldType;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class AttributeController extends Controller
|
|
{
|
|
public function index(Tenant $tenant): JsonResponse
|
|
{
|
|
$query = Attribute::query()
|
|
->where('tenant_codigo', $tenant->codigo)
|
|
->with('options')
|
|
->latest();
|
|
|
|
return AttributeResource::collection($query->paginateFromRequest())->response();
|
|
}
|
|
|
|
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');
|
|
});
|
|
|
|
return AttributeResource::make($attribute)->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Tenant $tenant, Attribute $attribute): AttributeResource
|
|
{
|
|
$attribute = $this->resolveScopedAttribute($tenant, $attribute);
|
|
|
|
return AttributeResource::make($attribute->load('options'));
|
|
}
|
|
|
|
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');
|
|
});
|
|
|
|
return AttributeResource::make($attribute);
|
|
}
|
|
|
|
public function destroy(Tenant $tenant, Attribute $attribute): Response
|
|
{
|
|
$attribute = $this->resolveScopedAttribute($tenant, $attribute);
|
|
$attribute->delete();
|
|
|
|
return response()->noContent();
|
|
}
|
|
|
|
protected function resolveScopedAttribute(Tenant $tenant, Attribute $attribute): Attribute
|
|
{
|
|
if ($attribute->tenant_codigo !== $tenant->codigo) {
|
|
throw new NotFoundHttpException('Attribute not found for tenant.');
|
|
}
|
|
|
|
return $attribute;
|
|
}
|
|
}
|