新增添加购物车接口

This commit is contained in:
laodaming 2023-09-13 18:37:50 +08:00
parent b8edd41e69
commit f56946fce9
2 changed files with 77 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
// fs_shopping_cart 新版购物车表
type FsShoppingCart struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // id
UserId *int64 `gorm:"default:0;" json:"user_id"` // 用户id
ProductId *int64 `gorm:"default:0;" json:"product_id"` // 产品id
TemplateId *int64 `gorm:"default:0;" json:"template_id"` // 模板id
ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型id

View File

@ -1,8 +1,10 @@
package logic
import (
"errors"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"context"
@ -31,12 +33,83 @@ func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCa
// }
func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in before add to shopping cart")
}
//校验参数
if err := l.AddToCartParamVerify(req); err != nil {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, err.Error())
}
//获取产品信息
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
}
logx.Error("获取产品信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}
//获取模板信息
templateInfo, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template is not exists")
}
logx.Error("获取模板信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
}
if *templateInfo.IsDel == 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template is deleted")
}
if *templateInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s status is unNormal")
}
//获取模型信息
modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, *templateInfo.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template`s model is not exists")
}
logx.Error("获取模型信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template`s model info")
}
//模型里面的size_id是否跟传入的size_id匹配
if req.SizeId != *modelInfo.SizeId {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param size_id is not match model`s size_id")
}
//获取尺寸信息
sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the size is not exists")
}
logx.Error("获取尺寸信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "failed to get size info")
}
//状态
if *sizeInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "size info status is not normal")
}
return resp.SetStatus(basic.CodeOK)
}
// 参数校验
func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error {
if req.ProductId <= 0 {
return errors.New("invalid param:product_id")
}
if req.SizeId <= 0 {
return errors.New("invalid param:size_id")
}
if req.TemplateId <= 0 {
return errors.New("invalid param:template_id")
}
if req.PurchaseQuantity <= 0 {
return errors.New("invalid param:purchase_quantity")
}
return nil
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)