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.
58 lines
1.1 KiB
58 lines
1.1 KiB
package favorites
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/jinzhu/gorm"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/tools"
|
|
|
|
"recook/internal/back"
|
|
)
|
|
|
|
type createFavoriteParam struct {
|
|
UserID uint `json:"userID"`
|
|
GoodsID uint `json:"goodsID" validate:"required"`
|
|
}
|
|
|
|
func CreateFavorites(c *gin.Context) {
|
|
var p createFavoriteParam
|
|
err := tools.ParseParams(&p, c)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if p.UserID <= 0 {
|
|
back.Fail(c, "游客无法使用该功能,请先登录")
|
|
return
|
|
}
|
|
|
|
var f goods.Favorites
|
|
|
|
err = dbc.DB.First(&f, "user_id = ? AND goods_id = ?", p.UserID, p.GoodsID).Error
|
|
if err != nil {
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
f = goods.Favorites{
|
|
UserID: p.UserID,
|
|
GoodsID: p.GoodsID,
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
}
|
|
|
|
err = dbc.DB.Create(&f).Error
|
|
if err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
} else {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
} else {
|
|
back.Fail(c, "商品已收藏")
|
|
return
|
|
}
|
|
|
|
back.Suc(c, "操作成功", &f)
|
|
}
|