This commit is contained in:
laodaming 2023-06-13 14:13:40 +08:00
parent 311ad581cc
commit 147af8ae29
6 changed files with 23 additions and 4 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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),
},
},
)
}

View File

@ -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")
}

View File

@ -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"`

View File

@ -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"`
}