70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"cockpit/internal/domain"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (h *Handler) StatusList(c *gin.Context) {
|
|
var list []domain.Status
|
|
_ = h.db.Order("sort_order ASC, id ASC").Find(&list).Error
|
|
c.JSON(http.StatusOK, domain.OK(gin.H{"list": list}))
|
|
}
|
|
|
|
type statusReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
SortOrder int `json:"sortOrder"`
|
|
Color string `json:"color"`
|
|
}
|
|
|
|
func (h *Handler) StatusCreate(c *gin.Context) {
|
|
var req statusReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, domain.Fail("参数错误"))
|
|
return
|
|
}
|
|
row := domain.Status{Name: strings.TrimSpace(req.Name), SortOrder: req.SortOrder, Color: strings.TrimSpace(req.Color)}
|
|
if err := h.db.Create(&row).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, domain.Fail("创建失败:"+err.Error()))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, domain.OK(row))
|
|
}
|
|
|
|
func (h *Handler) StatusUpdate(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req statusReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, domain.Fail("参数错误"))
|
|
return
|
|
}
|
|
var row domain.Status
|
|
if err := h.db.Where("id = ?", id).First(&row).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, domain.Fail("不存在"))
|
|
return
|
|
}
|
|
row.Name = strings.TrimSpace(req.Name)
|
|
row.SortOrder = req.SortOrder
|
|
row.Color = strings.TrimSpace(req.Color)
|
|
if err := h.db.Save(&row).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, domain.Fail("更新失败:"+err.Error()))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, domain.OK(row))
|
|
}
|
|
|
|
func (h *Handler) StatusDelete(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.db.Delete(&domain.Status{}, id).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, domain.Fail("删除失败:"+err.Error()))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, domain.OK(gin.H{}))
|
|
}
|
|
|