Refactor Brand, Category, and Product controllers to use createWithProps and updateWithProps methods for improved property synchronization
This commit is contained in:
@@ -23,8 +23,7 @@ class BrandController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$brand = Brand::create($validated);
|
$brand = Brand::createWithProps($validated, $props);
|
||||||
$brand->syncPropValues($props);
|
|
||||||
|
|
||||||
return BrandResource::make($brand)->response()->setStatusCode(201);
|
return BrandResource::make($brand)->response()->setStatusCode(201);
|
||||||
}
|
}
|
||||||
@@ -40,8 +39,7 @@ class BrandController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$marca->update($validated);
|
$marca = $marca->updateWithProps($validated, $props);
|
||||||
$marca->syncPropValues($props);
|
|
||||||
|
|
||||||
return BrandResource::make($marca);
|
return BrandResource::make($marca);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ class CategoryController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$category = Category::create($validated);
|
$category = Category::createWithProps($validated, $props);
|
||||||
$category->syncPropValues($props);
|
|
||||||
|
|
||||||
return CategoryResource::make($category->load(['parent', 'subCategories', 'tenant']))
|
return CategoryResource::make($category->load(['parent', 'subCategories', 'tenant']))
|
||||||
->response()
|
->response()
|
||||||
@@ -44,8 +43,7 @@ class CategoryController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$categoria->update($validated);
|
$categoria = $categoria->updateWithProps($validated, $props);
|
||||||
$categoria->syncPropValues($props);
|
|
||||||
|
|
||||||
return CategoryResource::make($categoria->load(['parent', 'subCategories', 'tenant']));
|
return CategoryResource::make($categoria->load(['parent', 'subCategories', 'tenant']));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ class ProductController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$product = Product::create($validated);
|
$product = Product::createWithProps($validated, $props);
|
||||||
$product->syncPropValues($props);
|
|
||||||
|
|
||||||
return ProductResource::make($product)->response()->setStatusCode(201);
|
return ProductResource::make($product)->response()->setStatusCode(201);
|
||||||
}
|
}
|
||||||
@@ -40,8 +39,7 @@ class ProductController extends Controller
|
|||||||
$props = $validated['props'] ?? [];
|
$props = $validated['props'] ?? [];
|
||||||
unset($validated['props']);
|
unset($validated['props']);
|
||||||
|
|
||||||
$producto->update($validated);
|
$producto = $producto->updateWithProps($validated, $props);
|
||||||
$producto->syncPropValues($props);
|
|
||||||
|
|
||||||
return ProductResource::make($producto);
|
return ProductResource::make($producto);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,30 @@ use App\Domains\Prop\Models\Prop;
|
|||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use LogicException;
|
use LogicException;
|
||||||
|
|
||||||
trait Propable
|
trait Propable
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $attributes
|
||||||
|
* @param array<string, mixed> $props
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public static function createWithProps(array $attributes, array $props = []): static
|
||||||
|
{
|
||||||
|
/** @var static $model */
|
||||||
|
$model = DB::transaction(function () use ($attributes, $props): Model {
|
||||||
|
/** @var static $createdModel */
|
||||||
|
$createdModel = static::query()->create($attributes);
|
||||||
|
$createdModel->syncPropValues($props);
|
||||||
|
|
||||||
|
return $createdModel;
|
||||||
|
});
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $attributes
|
* @param array<string, mixed> $attributes
|
||||||
*/
|
*/
|
||||||
@@ -70,6 +90,20 @@ trait Propable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $attributes
|
||||||
|
* @param array<string, mixed> $props
|
||||||
|
*/
|
||||||
|
public function updateWithProps(array $attributes, array $props = []): static
|
||||||
|
{
|
||||||
|
DB::transaction(function () use ($attributes, $props): void {
|
||||||
|
$this->update($attributes);
|
||||||
|
$this->syncPropValues($props);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function deletePropValue(Prop|string $prop): bool
|
public function deletePropValue(Prop|string $prop): bool
|
||||||
{
|
{
|
||||||
$resolvedProp = $this->resolveProp($prop);
|
$resolvedProp = $this->resolveProp($prop);
|
||||||
|
|||||||
Reference in New Issue
Block a user