init
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"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{
|
||||
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(uint(id))
|
||||
if err != nil {
|
||||
response.NotFound(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, skin)
|
||||
}
|
||||
|
||||
type skinCreateReq struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Game string `json:"game"`
|
||||
Category string `json:"category"`
|
||||
CoverURL string `json:"cover_url"`
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
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, "参数错误")
|
||||
return
|
||||
}
|
||||
status := req.Status
|
||||
if status != 0 && status != 1 {
|
||||
status = 1
|
||||
}
|
||||
// 未传 status 时默认上架;若明确要下架请先创建再更新
|
||||
if status == 0 {
|
||||
status = 1
|
||||
}
|
||||
skin := &model.Skin{
|
||||
Name: req.Name,
|
||||
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(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(uint(id)); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user