购物车商品库存校验
parent
74f4283a65
commit
027b869dab
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Endpoint\Api\Http\Controllers;
|
namespace App\Endpoint\Api\Http\Controllers;
|
||||||
|
|
||||||
use App\Endpoint\Api\Http\Resources\ShoppingCartItemResource;
|
use App\Endpoint\Api\Http\Resources\ShoppingCartItemResource;
|
||||||
|
use App\Exceptions\ProductNotEnoughException;
|
||||||
use App\Exceptions\ProductOfflineException;
|
use App\Exceptions\ProductOfflineException;
|
||||||
use App\Models\ProductSku;
|
use App\Models\ProductSku;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
@ -40,7 +41,11 @@ class ShoppingCartItemController extends Controller
|
||||||
throw new ProductOfflineException();
|
throw new ProductOfflineException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$item = $request->user()->shoppingCartItems()->firstOrCreate([
|
if ($input['quantity'] > $sku->saleable_stock) {
|
||||||
|
throw new ProductNotEnoughException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$shoppingCartItem = $request->user()->shoppingCartItems()->firstOrCreate([
|
||||||
'sku_id' => $sku->id,
|
'sku_id' => $sku->id,
|
||||||
], [
|
], [
|
||||||
'name' => $sku->name,
|
'name' => $sku->name,
|
||||||
|
|
@ -51,8 +56,12 @@ class ShoppingCartItemController extends Controller
|
||||||
'quantity' => $input['quantity'],
|
'quantity' => $input['quantity'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (! $item->wasRecentlyCreated) {
|
if (! $shoppingCartItem->wasRecentlyCreated) {
|
||||||
$item->increment('quantity', $input['quantity']);
|
if ($shoppingCartItem->quantity + $input['quantity'] > $sku->saleable_stock) {
|
||||||
|
throw new ProductNotEnoughException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$shoppingCartItem->increment('quantity', $input['quantity']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
|
|
@ -71,13 +80,17 @@ class ShoppingCartItemController extends Controller
|
||||||
'quantity' => ['bail', 'required', 'int', 'min:1'],
|
'quantity' => ['bail', 'required', 'int', 'min:1'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$item = $request->user()->shoppingCartItems()->findOrFail($id);
|
$shoppingCartItem = $request->user()->shoppingCartItems()->findOrFail($id);
|
||||||
|
|
||||||
if (is_null($sku = $item->sku()->online()->first())) {
|
if (is_null($sku = $shoppingCartItem->sku()->online()->first())) {
|
||||||
throw new ProductOfflineException();
|
throw new ProductOfflineException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$item->update(array_merge($input, [
|
if ($input['quantity'] > $sku->saleable_stock) {
|
||||||
|
throw new ProductNotEnoughException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$shoppingCartItem->update(array_merge($input, [
|
||||||
'name' => $sku->name,
|
'name' => $sku->name,
|
||||||
'cover' => $sku->cover,
|
'cover' => $sku->cover,
|
||||||
'sell_price' => $sku->sell_price,
|
'sell_price' => $sku->sell_price,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
class ProductNotEnoughException extends BizException
|
||||||
|
{
|
||||||
|
public function __construct(string $message = '商品库存不足')
|
||||||
|
{
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue