mh_server/controller/store.go

186 lines
4.1 KiB
Go
Raw Normal View History

2021-06-30 02:12:05 +00:00
package controller
import (
"github.com/codinl/go-logger"
"github.com/gin-gonic/gin"
2022-01-15 13:10:00 +00:00
"mh-server/lib/auth"
2021-06-30 02:12:05 +00:00
"mh-server/lib/status"
"mh-server/model"
)
func StoreList(c *gin.Context) {
req := struct {
2022-02-26 06:29:39 +00:00
//GameCardId uint64 `json:"game_card_id"`
2022-05-13 09:35:10 +00:00
UseType uint32 `json:"use_type"`
GameCardList []model.CardInfo `json:"game_card_list"`
2021-06-30 02:12:05 +00:00
}{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
2022-02-26 06:29:39 +00:00
stores, err := model.GetStoreList(req.GameCardList)
2021-06-30 02:12:05 +00:00
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, stores)
2022-06-21 07:34:42 +00:00
return
2022-05-13 09:35:10 +00:00
}
func DisplayStoreList(c *gin.Context) {
req := struct {
DisplayType uint32 `json:"display_type"` // 0-所有 1-共享卡
}{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
stores, err := model.GetDisplayStoreList(req.DisplayType)
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, stores)
2021-06-30 02:12:05 +00:00
}
func StoreInfo(c *gin.Context) {
req := struct {
StoreId uint32 `json:"store_id"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
store := model.Store{}
store.ID = req.StoreId
err := store.Info()
if err != nil {
logger.Error("err:", err)
return
}
RespOK(c, store)
return
}
2022-01-15 13:10:00 +00:00
func ShoppingCartAdd(c *gin.Context) {
req := &struct {
GameCardId uint32 `json:"game_card_id"`
AddType uint32 `json:"add_type"` // 1-加 2-减
}{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
2022-01-16 08:56:20 +00:00
//fmt.Println("GameCardId:",req.GameCardId)
//fmt.Println("AddType:",req.AddType)
2022-01-15 13:10:00 +00:00
if req.GameCardId == 0 {
logger.Error("GameCardId is 0")
RespJson(c, status.BadRequest, nil)
return
}
uc := auth.GetCurrentUser(c)
if uc == nil {
RespJson(c, status.Unauthorized, nil)
return
}
var shoppingCart model.ShoppingCart
err := model.NewShoppingCartQuerySet(model.DB).UidEq(uint64(uc.Uid)).GameCardIdEq(uint64(req.GameCardId)).One(&shoppingCart)
if err != nil && err != model.RecordNotFound {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
if err == model.RecordNotFound {
shoppingCart = model.ShoppingCart{
Uid: uint64(uc.Uid),
GameCardId: uint64(req.GameCardId),
Count: 1,
}
err := model.DB.Create(&shoppingCart).Error
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, shoppingCart)
return
}
if req.AddType == 1 {
shoppingCart.Count += 1
} else if req.AddType == 2 {
shoppingCart.Count -= 1
}
2022-01-16 08:56:20 +00:00
if shoppingCart.Count < 0 {
shoppingCart.Count = 0
}
2022-01-15 13:10:00 +00:00
_, err = model.NewShoppingCartQuerySet(model.DB).IDEq(shoppingCart.ID).GetUpdater().SetCount(shoppingCart.Count).UpdateNum()
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, shoppingCart)
return
}
func ShoppingCartList(c *gin.Context) {
req := &model.ShoppingCartListReq{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
uc := auth.GetCurrentUser(c)
if uc == nil {
RespJson(c, status.Unauthorized, nil)
return
}
req.Uid = uc.Uid
list, err := req.List()
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, list)
return
}
func ShoppingCartDel(c *gin.Context) {
req := &struct {
ShoppingCartIds []uint32 `json:"shopping_cart_ids"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(err)
RespJson(c, status.BadRequest, nil)
return
}
uc := auth.GetCurrentUser(c)
if uc == nil {
RespJson(c, status.Unauthorized, nil)
return
}
err := model.NewShoppingCartQuerySet(model.DB).UidEq(uint64(uc.Uid)).IDIn(req.ShoppingCartIds...).Delete()
if err != nil {
logger.Error("err:", err)
RespJson(c, status.InternalServerError, nil)
return
}
RespOK(c, nil)
return
}