-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
170 lines (152 loc) · 5.54 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"errors"
"github.com/bytedance/sonic"
"github.com/cqqqq777/go-kitex-mall/cmd/cart/dao"
"github.com/cqqqq777/go-kitex-mall/cmd/cart/model"
"github.com/cqqqq777/go-kitex-mall/cmd/cart/pkg"
"github.com/cqqqq777/go-kitex-mall/shared/errz"
"github.com/cqqqq777/go-kitex-mall/shared/kitex_gen/cart"
"github.com/cqqqq777/go-kitex-mall/shared/kitex_gen/common"
"github.com/cqqqq777/go-kitex-mall/shared/log"
"github.com/cqqqq777/go-kitex-mall/shared/response"
"time"
)
// CartServiceImpl implements the last service interface defined in the IDL.
type CartServiceImpl struct {
Dao *dao.Cart
ProductManager
UserManager
}
type ProductManager interface {
GetProductInfo(ctx context.Context, productId int64) (productInfo *pkg.Product, err error)
}
type UserManager interface {
GetUserInfo(ctx context.Context, userId int64) error
}
// AddProductToCart implements the CartServiceImpl interface.
func (s *CartServiceImpl) AddProductToCart(ctx context.Context, req *cart.MallAddProductToCartRequest) (resp *cart.MallAddProductToCartResponse, err error) {
// TODO: Your code here...
resp = new(cart.MallAddProductToCartResponse)
// get user info
if err != s.UserManager.GetUserInfo(ctx, req.UserId) {
if errors.Is(err, pkg.ErrNoSuchUser) {
resp.CommonResp = response.NewCommonResp(errz.NewErrZ(errz.WithErr(err), errz.WithCode(errz.CodeGetUserInfo)))
return resp, nil
}
resp.CommonResp = response.NewCommonResp(errz.ErrGetUserInfo)
log.Zlogger.Errorf("get user info failed err:%s", err.Error())
return resp, nil
}
// get product info
productInfo, err := s.ProductManager.GetProductInfo(ctx, req.ProductId)
if err != nil {
if errors.Is(err, pkg.ErrNoSuchProduct) {
resp.CommonResp = response.NewCommonResp(errz.ErrNoProduct)
return resp, nil
}
resp.CommonResp = response.NewCommonResp(errz.ErrProductInternal)
log.Zlogger.Errorf("get product info failed err:%s", err.Error())
return resp, nil
}
// check stock
if req.ProductNum > productInfo.Stock {
resp.CommonResp = response.NewCommonResp(errz.ErrShortage)
return resp, nil
}
// add product to cart
body, err := sonic.Marshal(&model.CartProduct{
ProductId: req.ProductId,
ProductNum: req.ProductNum,
MerchantId: productInfo.MerchantId,
Amount: productInfo.Price * req.ProductNum,
AddTime: time.Now().UnixNano(),
})
if err != nil {
resp.CommonResp = response.NewCommonResp(errz.ErrCartInternal)
log.Zlogger.Errorf("marshal product failed err:%s", err.Error())
return resp, nil
}
err = s.Dao.AddProductToCart(ctx, req.UserId, req.ProductId, body)
if err != nil {
resp.CommonResp = response.NewCommonResp(errz.ErrAddProductToCart)
log.Zlogger.Errorf("%s err:%s", errz.ErrAddProductToCart.Msg, err.Error())
return resp, nil
}
resp.CommonResp = response.NewCommonResp(nil)
return resp, nil
}
// GetCart implements the CartServiceImpl interface.
func (s *CartServiceImpl) GetCart(ctx context.Context, req *cart.MallGetCartRequest) (resp *cart.MallGetCartResponse, err error) {
// TODO: Your code here...
resp = new(cart.MallGetCartResponse)
// get user info
if err != s.UserManager.GetUserInfo(ctx, req.UserId) {
if errors.Is(err, pkg.ErrNoSuchUser) {
resp.CommonResp = response.NewCommonResp(errz.NewErrZ(errz.WithErr(err), errz.WithCode(errz.CodeGetUserInfo)))
return resp, nil
}
resp.CommonResp = response.NewCommonResp(errz.ErrGetUserInfo)
log.Zlogger.Errorf("get user info failed err:%s", err.Error())
return resp, nil
}
// get cart
products, err := s.Dao.GetCart(ctx, req.UserId)
if err != nil {
resp.CommonResp = response.NewCommonResp(errz.ErrGetCart)
log.Zlogger.Errorf("get cart failed err:%s", err.Error())
return resp, nil
}
// build response
for _, product := range products {
cartProduct := new(common.CartProduct)
// check whether valid
productInfo, err := s.ProductManager.GetProductInfo(ctx, product.ProductId)
if err != nil {
log.Zlogger.Errorf("get product info failed err:%s", err.Error())
continue
}
if productInfo.Stock < product.ProductNum {
cartProduct.IsInvalid = false
continue
}
cartProduct.ProductId = product.ProductId
cartProduct.ProductNum = product.ProductNum
cartProduct.Amount = product.Amount
cartProduct.AddTime = product.AddTime
cartProduct.MerchantId = product.MerchantId
cartProduct.IsInvalid = true
resp.Cart.CartProducts = append(resp.Cart.CartProducts, cartProduct)
}
resp.CommonResp = response.NewCommonResp(nil)
return resp, nil
}
// DelCartProduct implements the CartServiceImpl interface.
func (s *CartServiceImpl) DelCartProduct(ctx context.Context, req *cart.MallDelCartProductRequest) (resp *cart.MallDelCartProductResponse, err error) {
// TODO: Your code here...
resp = new(cart.MallDelCartProductResponse)
// get user info
if err != s.UserManager.GetUserInfo(ctx, req.UserId) {
if errors.Is(err, pkg.ErrNoSuchUser) {
resp.CommonResp = response.NewCommonResp(errz.NewErrZ(errz.WithErr(err), errz.WithCode(errz.CodeGetUserInfo)))
return resp, nil
}
resp.CommonResp = response.NewCommonResp(errz.ErrGetUserInfo)
log.Zlogger.Errorf("get user info failed err:%s", err.Error())
return resp, nil
}
// del product
err = s.Dao.DelProduct(ctx, req.UserId, req.ProductId)
if err != nil {
if errors.Is(err, pkg.ErrNoSuchProduct) {
resp.CommonResp = response.NewCommonResp(errz.ErrNoProduct)
return resp, nil
}
resp.CommonResp = response.NewCommonResp(errz.ErrCartInternal)
log.Zlogger.Errorf("del product in cart failed err:%s", err.Error())
return resp, nil
}
resp.CommonResp = response.NewCommonResp(nil)
return resp, nil
}