From 147af8ae2932df98ef1fa28676e11ab023d3a40e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 13 Jun 2023 14:13:40 +0800 Subject: [PATCH] fix --- model/gmodel/fscartmodel.go | 3 +++ .../internal/handler/cartaddhandler.go | 6 +++--- .../shopping-cart-confirmation/internal/handler/routes.go | 5 +++++ .../internal/logic/cartaddlogic.go | 2 +- server/shopping-cart-confirmation/internal/types/types.go | 4 ++++ server_api/shopping-cart-confirmation.api | 7 +++++++ 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/model/gmodel/fscartmodel.go b/model/gmodel/fscartmodel.go index 3bcedfc9..8167bcc9 100755 --- a/model/gmodel/fscartmodel.go +++ b/model/gmodel/fscartmodel.go @@ -86,3 +86,6 @@ func (c *FsCartModel) Create(ctx context.Context, data FsCart) error { func (c *FsCartModel) Update(ctx context.Context, id int64, data FsCart) error { return c.db.WithContext(ctx).Model(&FsCart{}).Where("`id` = ?", id).Updates(data).Error } +func (c *FsCartModel) UpdateByIdUserId(ctx context.Context, id int64, userId int64, data FsCart) error { + return c.db.WithContext(ctx).Model(&FsCart{}).Where("`id` = ? and `user_id` = ?", id, userId).Updates(data).Error +} diff --git a/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go b/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go index 4b22da6e..6b470929 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go @@ -18,7 +18,7 @@ import ( func CartAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // 解析jwtToken - /*claims, err := svcCtx.ParseJwtToken(r) + claims, err := svcCtx.ParseJwtToken(r) // 如果解析出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ @@ -40,7 +40,7 @@ func CartAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { logx.Info("unauthorized:", err.Error()) return } - */ + var req types.CartAddReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { @@ -53,7 +53,7 @@ func CartAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewCartAddLogic(r.Context(), svcCtx) - resp := l.CartAdd(&req, &auth.UserInfo{83}) + resp := l.CartAdd(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { diff --git a/server/shopping-cart-confirmation/internal/handler/routes.go b/server/shopping-cart-confirmation/internal/handler/routes.go index 38910934..917ab944 100644 --- a/server/shopping-cart-confirmation/internal/handler/routes.go +++ b/server/shopping-cart-confirmation/internal/handler/routes.go @@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/cart/add", Handler: CartAddHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/cart/del", + Handler: CartDeleteHandler(serverCtx), + }, }, ) } diff --git a/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go b/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go index 4bef690b..1807f54a 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go @@ -122,5 +122,5 @@ func (l *CartAddLogic) CartAdd(req *types.CartAddReq, userinfo *auth.UserInfo) ( logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart") } - return resp.SetStatus(basic.CodeOK) + return resp.SetStatusWithMessage(basic.CodeOK, "add to cart success") } diff --git a/server/shopping-cart-confirmation/internal/types/types.go b/server/shopping-cart-confirmation/internal/types/types.go index 4846ccff..cebad1bf 100644 --- a/server/shopping-cart-confirmation/internal/types/types.go +++ b/server/shopping-cart-confirmation/internal/types/types.go @@ -11,6 +11,10 @@ type CartAddReq struct { IsCheck int64 `json:"is_check,optional"` } +type CartDeleteReq struct { + Id int64 `json:"id"` +} + type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/server_api/shopping-cart-confirmation.api b/server_api/shopping-cart-confirmation.api index 5dcc53d9..35514f02 100644 --- a/server_api/shopping-cart-confirmation.api +++ b/server_api/shopping-cart-confirmation.api @@ -11,6 +11,9 @@ service shopping-cart-confirmation { //添加入购物车 @handler CartAddHandler post /cart/add (CartAddReq) returns (response); + //删除购物车 + @handler CartDeleteHandler + post /cart/del (CartDeleteReq) returns (response); } //添加入购物车 @@ -18,4 +21,8 @@ type CartAddReq { DesignId string `json:"design_id"` //设计sn BuyNum int64 `json:"buy_num"` //购买数量 IsCheck int64 `json:"is_check,optional"` +} +//删除购物车 +type CartDeleteReq { + Id int64 `json:"id"` } \ No newline at end of file