Files
easygoadmin/app/controller/dict.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

123 lines
2.2 KiB
Go

/**
* 字典管理-控制器
* @author
* @since 2021/11/13
* @File : dict
*/
package controller
import (
"easygoadmin/app/dto"
"easygoadmin/app/service"
"easygoadmin/utils"
"easygoadmin/utils/common"
"easygoadmin/utils/response"
"github.com/gin-gonic/gin"
"net/http"
)
var Dict = new(dictCtl)
type dictCtl struct{}
func (c *dictCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "dict_index.html").WriteTpl()
}
func (c *dictCtl) List(ctx *gin.Context) {
// 调用查询列表方法
list := service.Dict.GetList()
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}
func (c *dictCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.DictAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Dict.Add(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "添加成功",
})
}
func (c *dictCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.DictUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Dict.Update(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新成功",
})
}
func (c *dictCtl) Delete(ctx *gin.Context) {
// 记录ID
ids := ctx.Param("ids")
if ids == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "记录ID不能为空",
})
}
// 调用删除方法
rows, err := service.Dict.Delete(ids)
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "删除成功",
})
}