You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

package handler
import (
"context"
"fmt"
"shoppingCart/domain/service"
"shoppingCart/proto"
"strconv"
"strings"
)
type CartHandler struct {
CartService service.ICartService
}
// 新增购物车
func (u *CartHandler) AddCart(ctx context.Context, req *proto.AddCartReq, resp *proto.AddCartResp) error {
obj, err := u.CartService.AddCart(req)
if err != nil {
println(" AddCart err :", err)
} else {
resp.CanSetShoppingCartNumber = int64(obj.Number)
resp.ShoppingCartNumber = int64(obj.Number)
resp.IsBeyondMaxLimit = false // 查询sku
resp.ID = obj.ID //增加新增cart的ID
fmt.Println(" AddCart handler >>>>>> ", resp)
}
return err
}
//修改购物车
func (u *CartHandler) UpdateCart(ctx context.Context, req *proto.UpdateCartReq, resp *proto.UpdateCartResp) error {
obj, err := u.CartService.UpdateCart(req)
if err != nil {
println(" UpdateCart err :", err)
} else {
resp.CanSetShoppingCartNumber = int64(obj.Number)
resp.ShoppingCartNumber = int64(obj.Number)
resp.IsBeyondMaxLimit = false // 查询sku
resp.ID = obj.ID //增加新增cart的ID
fmt.Println(" UpdateCart handler >>>>>> ", resp)
}
return err
}
// (int32List []int32) (obj *float32, err error)
func (u *CartHandler) GetOrderTotal(ctx context.Context, req *proto.OrderTotalReq, obj *proto.OrderTotalResp) error {
//int32List := SplitToInt32List(req.GetCartIds(), ",")
obj.TotalPrice, _ = u.CartService.GetOrderTotal(req.GetCartIds())
return nil
}
// 格式化页面传入的cartIds
func SplitToInt32List(str string, sep string) (int32List []int32) {
tempStr := strings.Split(str, sep)
if len(tempStr) > 0 {
for _, item := range tempStr {
if item == "" {
continue
}
val, err := strconv.ParseInt(item, 10, 32)
if err != nil {
continue
}
int32List = append(int32List, int32(val))
}
}
return int32List
}