package labelValue 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.LabelValue model.LabelID = req.LabelID if err := models.LabelValueRowInsert(&model); err != nil { log.Error(err) ctx.JSON(http.StatusOK, gin.H{ "code": 1, "message": "数据添加错误", }) return } // build response row, err := models.LabelValueRow(model.ID) 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.LabelValue model.ID = req.ID if err := models.LabelValueRowUpdate(&model); err != nil { log.Error(err) ctx.JSON(http.StatusOK, gin.H{ "code": 1, "message": "数据更新错误", }) return } // build response row, err := models.LabelValueRow(model.ID) 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.LabelValueRowDel(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.LabelValueRows(req.Pager, true, req.Keyword, req.LabelID) 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.LabelValueRow(req.ID) 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, }) } }