From fda7596bc1a65818da7a0455b6d0aa0b3918b867 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 13 Jun 2023 14:13:56 +0800 Subject: [PATCH] fix --- .../internal/handler/cartdeletehandler.go | 67 +++++++++++++++++++ .../internal/logic/cartdeletelogic.go | 44 ++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go create mode 100644 server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go diff --git a/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go b/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go new file mode 100644 index 00000000..31c0ca7b --- /dev/null +++ b/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go @@ -0,0 +1,67 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/shopping-cart-confirmation/internal/logic" + "fusenapi/server/shopping-cart-confirmation/internal/svc" + "fusenapi/server/shopping-cart-confirmation/internal/types" +) + +func CartDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // 解析jwtToken + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + + // 从Token里获取对应的信息 + userinfo, err := auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + + var req types.CartDeleteReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewCartDeleteLogic(r.Context(), svcCtx) + resp := l.CartDelete(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go b/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go new file mode 100644 index 00000000..5c9edbcf --- /dev/null +++ b/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go @@ -0,0 +1,44 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/shopping-cart-confirmation/internal/svc" + "fusenapi/server/shopping-cart-confirmation/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CartDeleteLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCartDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartDeleteLogic { + return &CartDeleteLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *CartDeleteLogic) CartDelete(req *types.CartDeleteReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if req.Id <= 0 { + return resp.SetStatusWithMessage(basic.CodeApiErr, "invalid param id") + } + cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn) + status := int64(0) + err := cartModel.UpdateByIdUserId(l.ctx, req.Id, userinfo.UserId, gmodel.FsCart{ + Status: &status, + }) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to delete cart") + } + return resp.SetStatusWithMessage(basic.CodeOK, "delete success") +}