Files
hfb_jysh/backend/internal/modules/listing/handler_collection.go
T

134 lines
3.2 KiB
Go

package listing
import (
"errors"
"strconv"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func parseListingIDParam(c *gin.Context) (uint64, bool) {
id, err := strconv.ParseUint(c.Param("listingId"), 10, 64)
if err != nil || id == 0 {
response.BadRequest(c, "商品 ID 不正确")
return 0, false
}
return id, true
}
func writeCollectionError(c *gin.Context, err error) {
switch {
case errors.Is(err, ErrCollectionKindInvalid):
response.BadRequest(c, "足迹类型不正确")
case errors.Is(err, gorm.ErrRecordNotFound):
response.NotFound(c, "商品不存在或已删除")
default:
writeListingError(c, err)
}
}
func (h *Handler) ListCollections(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
kind := c.DefaultQuery("kind", CollectionKindHistory)
result, err := h.service.ListCollections(c.Request.Context(), userID, kind)
if err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, result)
}
type collectionBody struct {
ListingID uint64 `json:"listing_id" binding:"required"`
}
func (h *Handler) RecordCollectionView(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
var req collectionBody
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "商品 ID 不正确")
return
}
if err := h.service.RecordCollectionView(c.Request.Context(), userID, req.ListingID); err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, gin.H{"recorded": true})
}
func (h *Handler) AddCollectionFavorite(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
var req collectionBody
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "商品 ID 不正确")
return
}
if err := h.service.AddCollectionFavorite(c.Request.Context(), userID, req.ListingID); err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, gin.H{"favorited": true})
}
func (h *Handler) RemoveCollectionFavorite(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
listingID, ok := parseListingIDParam(c)
if !ok {
return
}
if err := h.service.RemoveCollectionFavorite(c.Request.Context(), userID, listingID); err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, gin.H{"deleted": true})
}
func (h *Handler) RemoveCollectionHistory(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
listingID, ok := parseListingIDParam(c)
if !ok {
return
}
if err := h.service.RemoveCollectionHistory(c.Request.Context(), userID, listingID); err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, gin.H{"deleted": true})
}
func (h *Handler) ClearCollectionHistory(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
if err := h.service.ClearCollectionHistory(c.Request.Context(), userID); err != nil {
writeCollectionError(c, err)
return
}
response.OK(c, gin.H{"cleared": true})
}