Files
affiliate_dash/backend/internal/handler/skin.go
T

129 lines
3.2 KiB
Go

package handler
import (
"strconv"
"affiliate_dash/internal/middleware"
"affiliate_dash/internal/model"
"affiliate_dash/internal/pkg/response"
"affiliate_dash/internal/service"
"github.com/gin-gonic/gin"
)
type SkinHandler struct {
svc *service.SkinService
}
func NewSkinHandler(svc *service.SkinService) *SkinHandler {
return &SkinHandler{svc: svc}
}
func (h *SkinHandler) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
q := service.SkinListQuery{
MerchantID: middleware.GetMerchantID(c),
Page: page,
Size: size,
Keyword: c.Query("keyword"),
Game: c.Query("game"),
Category: c.Query("category"),
}
if s := c.Query("status"); s != "" {
v, _ := strconv.Atoi(s)
q.Status = &v
}
list, total, err := h.svc.List(q)
if err != nil {
response.ServerError(c, err.Error())
return
}
response.Page(c, list, total, page, size)
}
func (h *SkinHandler) Get(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
skin, err := h.svc.Get(middleware.GetMerchantID(c), uint(id))
if err != nil {
response.NotFound(c, err.Error())
return
}
response.OK(c, skin)
}
type skinCreateReq struct {
Name string `json:"name" binding:"required"`
SKU string `json:"sku" binding:"required"`
Game string `json:"game"`
Category string `json:"category"`
CoverURL string `json:"cover_url"`
Price float64 `json:"price"`
CostPrice float64 `json:"cost_price"`
Commission float64 `json:"commission"`
Stock int `json:"stock"`
Status int `json:"status"`
Description string `json:"description"`
}
func (h *SkinHandler) Create(c *gin.Context) {
var req skinCreateReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:名称与 sku 必填")
return
}
status := req.Status
if status != 1 {
// 未传或非 1 时:创建默认上架;若传 0 仍默认上架(下架请创建后更新)
status = 1
}
if req.Game == "" {
req.Game = "和平精英"
}
skin := &model.Skin{
MerchantID: middleware.GetMerchantID(c),
Name: req.Name,
SKU: req.SKU,
Game: req.Game,
Category: req.Category,
CoverURL: req.CoverURL,
Price: req.Price,
CostPrice: req.CostPrice,
Commission: req.Commission,
Stock: req.Stock,
Status: status,
Description: req.Description,
}
if err := h.svc.Create(skin); err != nil {
response.ServerError(c, err.Error())
return
}
response.OK(c, skin)
}
func (h *SkinHandler) Update(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var updates map[string]interface{}
if err := c.ShouldBindJSON(&updates); err != nil {
response.BadRequest(c, "参数错误")
return
}
delete(updates, "id")
delete(updates, "created_at")
delete(updates, "updated_at")
if err := h.svc.Update(middleware.GetMerchantID(c), uint(id), updates); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}
func (h *SkinHandler) Delete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if err := h.svc.Delete(middleware.GetMerchantID(c), uint(id)); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}