库存不足

This commit is contained in:
Edward Yang 2022-08-26 15:29:25 +08:00
parent 87e8b16a38
commit 3503560202
4 changed files with 89 additions and 2 deletions

View File

@ -3,6 +3,7 @@
namespace Beike\Shop\Http\Controllers;
use Beike\Models\ProductSku;
use Beike\Shop\Http\Requests\CartRequest;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Beike\Shop\Services\CartService;
@ -84,11 +85,11 @@ class CartController extends Controller
/**
* 创建购物车
*
* @param Request $request
* @param CartRequest $request
* @return mixed
* @throws \Exception
*/
public function store(Request $request)
public function store(CartRequest $request)
{
$skuId = $request->sku_id;
$quantity = $request->quantity ?? 1;

View File

@ -0,0 +1,58 @@
<?php
/**
* CartRequest.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-26 14:21:32
* @modified 2022-08-26 14:21:32
*/
namespace Beike\Shop\Http\Requests;
use Beike\Models\ProductSku;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CartRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$skuId = (int)$this->get('sku_id');
return [
'sku_id' => 'required|int',
'quantity' => ['required', 'int', function ($attribute, $value, $fail) use ($skuId) {
$skuQuantity = ProductSku::query()->where('id', $skuId)->value('quantity');
if ($value > $skuQuantity) {
$fail(trans('cart.stock_out'));
}
}],
'buy_now' => 'bool'
];
}
public function attributes()
{
return [
'sku_id' => trans('cart.sku_id'),
'quantity' => trans('cart.quantity'),
];
}
}

View File

@ -0,0 +1,14 @@
<?php
/**
* cart.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-26 15:25:29
* @modified 2022-08-26 15:25:29
*/
return [
'stock_out' => 'Stock Out',
];

View File

@ -0,0 +1,14 @@
<?php
/**
* cart.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-26 15:25:29
* @modified 2022-08-26 15:25:29
*/
return [
'stock_out' => '库存不足',
];