package role 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.Role model.Privs = models.PrivFilter(model.Privs) if err := models.RoleRowInsert(&model); err != nil { log.Error(err) ctx.JSON(http.StatusOK, gin.H{ "code": 1, "message": "数据添加错误", }) return } // build response row, err := models.RoleRow(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.Role model.ID = req.ID if model.ID == 1 { model.Privs = "" } else { model.Privs = models.PrivFilter(req.Privs) } if err := models.RoleRowUpdate(&model); err != nil { log.Error(err) ctx.JSON(http.StatusOK, gin.H{ "code": 1, "message": "数据更新错误", }) return } // build response row, err := models.RoleRow(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.RoleRowDel(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.RoleRows(req.Pager, true, req.Keyword) 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.RoleRow(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, }) } }