This commit is contained in:
laodaming 2023-09-20 11:05:59 +08:00
parent d8707e3a4a
commit f284702448
5 changed files with 1 additions and 122 deletions

View File

@ -1,35 +0,0 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/shopping-cart/internal/logic"
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
)
func ModifyCartPurchaseQuantityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ModifyCartPurchaseQuantityReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewModifyCartPurchaseQuantityLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.ModifyCartPurchaseQuantity(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -22,11 +22,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/shopping-cart/delete",
Handler: DeleteCartHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/shopping-cart/modify",
Handler: ModifyCartPurchaseQuantityHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/shopping-cart/get_carts",

View File

@ -1,69 +0,0 @@
package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"context"
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ModifyCartPurchaseQuantityLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyCartPurchaseQuantityLogic {
return &ModifyCartPurchaseQuantityLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *ModifyCartPurchaseQuantityLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) {
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 必须重新处理
// func (l *ModifyCartPurchaseQuantityLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }

View File

@ -29,11 +29,6 @@ type DeleteCartReq struct {
Id int64 `json:"id"` //购物车id
}
type ModifyCartPurchaseQuantityReq struct {
Id int64 `json:"id"` //购物车id
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
}
type GetCartsReq struct {
CurrentPage int `form:"current_page"` //当前页
}

View File

@ -15,9 +15,6 @@ service shopping-cart {
//删除购物车
@handler DeleteCartHandler
post /api/shopping-cart/delete(DeleteCartReq) returns (response);
//修改购物车购买数量
@handler ModifyCartPurchaseQuantityHandler
post /api/shopping-cart/modify(ModifyCartPurchaseQuantityReq) returns (response);
//获取购物车列表
@handler GetCartsHandler
get /api/shopping-cart/get_carts(GetCartsReq) returns (response);
@ -49,11 +46,7 @@ type DiyInfo {
type DeleteCartReq {
Id int64 `json:"id"` //购物车id
}
//修改购物车购买数量
type ModifyCartPurchaseQuantityReq {
Id int64 `json:"id"` //购物车id
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
}
//获取购物车列表
type GetCartsReq {
CurrentPage int `form:"current_page"` //当前页