feat: refactor cart item handling to use mapped buyable types and improve method signatures
This commit is contained in:
@@ -116,7 +116,7 @@ class Cart extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function updateItem(string $buyableType, int $buyableId, int $quantity): CartItem
|
||||
public function updateItem(int $cartItemId, int $quantity): CartItem
|
||||
{
|
||||
if ($quantity <= 0) {
|
||||
throw ValidationException::withMessages([
|
||||
@@ -124,17 +124,14 @@ class Cart extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($buyableType, $buyableId, $quantity): CartItem {
|
||||
$canonicalType = $this->resolveBuyableClass($buyableType);
|
||||
|
||||
return DB::transaction(function () use ($cartItemId, $quantity): CartItem {
|
||||
/** @var CartItem $item */
|
||||
$item = $this->items()
|
||||
->where('buyable_type', $canonicalType)
|
||||
->where('buyable_id', $buyableId)
|
||||
->where('id', $cartItemId)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
$buyable = $this->resolveScopedBuyable($buyableType, $buyableId, true);
|
||||
$buyable = $this->resolveScopedBuyable($item->buyable_type, $item->buyable_id, true);
|
||||
$delta = $quantity - $item->cantidad;
|
||||
$availableQuantity = $buyable->availableQuantity();
|
||||
|
||||
@@ -160,19 +157,16 @@ class Cart extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function removeItem(string $buyableType, int $buyableId): void
|
||||
public function removeItem(int $cartItemId): void
|
||||
{
|
||||
DB::transaction(function () use ($buyableType, $buyableId): void {
|
||||
$canonicalType = $this->resolveBuyableClass($buyableType);
|
||||
|
||||
DB::transaction(function () use ($cartItemId): void {
|
||||
/** @var CartItem $item */
|
||||
$item = $this->items()
|
||||
->where('buyable_type', $canonicalType)
|
||||
->where('buyable_id', $buyableId)
|
||||
->where('id', $cartItemId)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
$buyable = $this->resolveScopedBuyable($buyableType, $buyableId, true);
|
||||
$buyable = $this->resolveScopedBuyable($item->buyable_type, $item->buyable_id, true);
|
||||
$buyable->decrementReservedStock($item->cantidad);
|
||||
$item->delete();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user