package admin import ( "encoding/json" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/core/validation" "viviman.top/controllers" "viviman.top/models" "viviman.top/vo" ) // DictDetailController 字典详情api type DictDetailController struct { controllers.BaseController } func (c *DictDetailController) URLMapping() { c.Mapping("Post", c.Post) c.Mapping("GetAll", c.GetAll) c.Mapping("Put", c.Put) c.Mapping("Delete", c.Delete) } // GetAll @Title获取字典详情列表 // @Description 获取字典详情列表 // @Success 200 {object} controllers.Result // @router / [get] func (c *DictDetailController) GetAll() { dictId, _ := c.GetInt64("dictId") dictName := c.GetString("dictName") total, list := models.GetAllDictDetail(c.GetParams(), dictId, dictName) c.Data["json"] = controllers.SuccessData(vo.ResultList{Content: list, TotalElements: total}) _ = c.ServeJSON() } // Post @Title添加字典详情 // @Description 添加字典详情 // @Success 200 {object} controllers.Result // @router / [post] func (c *DictDetailController) Post() { var model models.SysDictDetail valid := validation.Validation{} err := json.Unmarshal(c.Ctx.Input.RequestBody, &model) if err != nil { c.Data["json"] = controllers.ErrMsg("转译时,发生异常!") _ = c.ServeJSON() return } logs.Info(model) b, _ := valid.Valid(&model) if !b { for _, err := range valid.Errors { c.Data["json"] = controllers.ErrMsg(err.Message) } } _, e := models.AddDictDetail(&model) if e != nil { c.Data["json"] = controllers.ErrMsg(e.Error()) } c.Data["json"] = controllers.SuccessData("操作成功") _ = c.ServeJSON() } // Put @Title修改字典详情 // @Description 修改字典详情 // @Success 200 {object} controllers.Result // @router / [put] func (c *DictDetailController) Put() { var model models.SysDictDetail valid := validation.Validation{} err := json.Unmarshal(c.Ctx.Input.RequestBody, &model) if err != nil { c.Data["json"] = controllers.ErrMsg("转译时,发生异常!") _ = c.ServeJSON() return } b, _ := valid.Valid(&model) if !b { for _, err := range valid.Errors { c.Data["json"] = controllers.ErrMsg(err.Message) } } e := models.UpdateByDictDetail(&model) if e != nil { c.Data["json"] = controllers.ErrMsg(e.Error()) } c.Data["json"] = controllers.SuccessData("操作成功") _ = c.ServeJSON() } // Delete @Title删除字典详情 // @Description 删除字典详情 // @Success 200 {object} controllers.Result // @router /:id [delete] func (c *DictDetailController) Delete() { id, _ := c.GetInt64(":id", 1) e := models.DelByDictDetail(id) if e != nil { c.Data["json"] = controllers.ErrMsg(e.Error()) } c.Data["json"] = controllers.SuccessData("操作成功") _ = c.ServeJSON() }