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'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$brand = Brand::create($validated);
|
||||
$brand->syncPropValues($props);
|
||||
$brand = Brand::createWithProps($validated, $props);
|
||||
|
||||
return BrandResource::make($brand)->response()->setStatusCode(201);
|
||||
}
|
||||
@@ -40,8 +39,7 @@ class BrandController extends Controller
|
||||
$props = $validated['props'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$marca->update($validated);
|
||||
$marca->syncPropValues($props);
|
||||
$marca = $marca->updateWithProps($validated, $props);
|
||||
|
||||
return BrandResource::make($marca);
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ class CategoryController extends Controller
|
||||
$props = $validated['props'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$category = Category::create($validated);
|
||||
$category->syncPropValues($props);
|
||||
$category = Category::createWithProps($validated, $props);
|
||||
|
||||
return CategoryResource::make($category->load(['parent', 'subCategories', 'tenant']))
|
||||
->response()
|
||||
@@ -44,8 +43,7 @@ class CategoryController extends Controller
|
||||
$props = $validated['props'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$categoria->update($validated);
|
||||
$categoria->syncPropValues($props);
|
||||
$categoria = $categoria->updateWithProps($validated, $props);
|
||||
|
||||
return CategoryResource::make($categoria->load(['parent', 'subCategories', 'tenant']));
|
||||
}
|
||||
|
||||
@@ -23,8 +23,7 @@ class ProductController extends Controller
|
||||
$props = $validated['props'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$product = Product::create($validated);
|
||||
$product->syncPropValues($props);
|
||||
$product = Product::createWithProps($validated, $props);
|
||||
|
||||
return ProductResource::make($product)->response()->setStatusCode(201);
|
||||
}
|
||||
@@ -40,8 +39,7 @@ class ProductController extends Controller
|
||||
$props = $validated['props'] ?? [];
|
||||
unset($validated['props']);
|
||||
|
||||
$producto->update($validated);
|
||||
$producto->syncPropValues($props);
|
||||
$producto = $producto->updateWithProps($validated, $props);
|
||||
|
||||
return ProductResource::make($producto);
|
||||
}
|
||||
|
||||
@@ -7,10 +7,30 @@ use App\Domains\Prop\Models\Prop;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use LogicException;
|
||||
|
||||
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
|
||||
*/
|
||||
@@ -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
|
||||
{
|
||||
$resolvedProp = $this->resolveProp($prop);
|
||||
|
||||
Reference in New Issue
Block a user