新增更新购物车购买数量接口

This commit is contained in:
laodaming
2023-09-14 15:46:37 +08:00
parent b71141807b
commit 0bb11f975d
7 changed files with 52 additions and 11 deletions

View File

@@ -37,7 +37,7 @@ func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCa
func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in before add to shopping cart")
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
//校验参数
if err := l.AddToCartParamVerify(req); err != nil {

View File

@@ -1,8 +1,11 @@
package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"context"
@@ -31,10 +34,33 @@ func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.Service
// }
func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
return resp.SetStatus(basic.CodeOK)
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
if req.Id <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "cart id is required")
}
if req.PurchaseQuantity <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity can not less than 0 or equal to 0")
}
//查询购物车
_, err := l.svcCtx.AllModels.FsShoppingCart.FineOneUserCart(l.ctx, req.Id, userinfo.UserId, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the shopping cart is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get shopping cart info ")
}
//修改数量
err = l.svcCtx.AllModels.FsShoppingCart.Update(l.ctx, req.Id, userinfo.UserId, &gmodel.FsShoppingCart{
PurchaseQuantity: &req.PurchaseQuantity,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to modify cart purchase quantity")
}
return resp.SetStatus(basic.CodeOK, "success")
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理

View File

@@ -30,8 +30,8 @@ type DeleteCartReq struct {
}
type ModifyCartPurchaseQuantityReq struct {
Id int64 `json:"id"` //购物车id
Quantity int64 `json:"quantity"` //数量
Id int64 `json:"id"` //购物车id
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
}
type GetCartsReq struct {