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.

180 lines
3.3 KiB

2 months ago
package label
import (
"github.com/gin-gonic/gin"
"net/http"
"product/backend/handlers/base"
"product/backend/models"
"product/backend/moo/log"
)
// Post 添加
func Post(ctx *gin.Context) {
// bind request data
req := &PostReq{}
if err := ctx.ShouldBind(&req); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": base.Translate(err),
})
return
}
// insert
model := req.Label
model.EntityType = req.EntityType
model.EntityID = req.EntityID
if err := models.LabelKeyRowInsert(&model); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据添加错误",
})
return
}
// build response
row, err := models.LabelKeyRow(model.ID, false)
if err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据添加错误",
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
// Put 更新全部
func Put(ctx *gin.Context) {
// bind request data
req := &PutReq{}
if err := ctx.ShouldBind(&req); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": base.Translate(err),
})
return
}
// update
model := req.Label
model.ID = req.ID
if err := models.LabelKeyRowUpdate(&model); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据更新错误",
})
return
}
// build response
row, err := models.LabelKeyRow(model.ID, true)
if err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据更新错误",
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
// Delete 删除
func Delete(ctx *gin.Context) {
req := &DelReq{}
if err := req.BindAndInit(ctx); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": base.Translate(err),
})
return
}
if err := models.LabelKeyRowDel(req.ID); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据删除错误",
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
})
}
// Get 获取
func Get(ctx *gin.Context) {
// get request param
req := &GetReq{}
if err := req.BindAndInit(ctx); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": base.Translate(err),
})
return
}
if req.ID == 0 { // list
// fetch rows
list, total, err := models.LabelKeyRows(req.Pager, true, req.Keyword, req.EntityType, req.EntityID, req.WithLabelValue)
if err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据未找到",
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"list": list,
"total": total,
"keyword": req.Keyword,
"pageIndex": req.PageIndex,
"pageSize": req.PageSize,
"sortField": req.SortField,
"sortOrder": req.SortOrder,
},
})
} else {
// fetch row
row, err := models.LabelKeyRow(req.ID, req.WithLabelValue)
if err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据未找到",
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
}