/** * 演示二管理-控制器 * @author * @since 2022-04-19 * @File : example2 */ package controller import ( "easygoadmin/app/dto" "easygoadmin/app/model" "easygoadmin/app/service" "easygoadmin/utils" "easygoadmin/utils/common" "easygoadmin/utils/gconv" "easygoadmin/utils/response" "github.com/gin-gonic/gin" "net/http" ) var Example2 = new(example2Ctl) type example2Ctl struct{} func (c *example2Ctl) Index(ctx *gin.Context) { // 模板渲染 response.BuildTpl(ctx, "example2_index.html").WriteTpl() } func (c *example2Ctl) List(ctx *gin.Context) { // 参数绑定 var req *dto.Example2PageReq if err := ctx.ShouldBind(&req); err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 调用获取列表方法 list, count, err := service.Example2.GetList(req) if err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 返回结果 ctx.JSON(http.StatusOK, common.JsonResult{ Code: 0, Data: list, Msg: "操作成功", Count: count, }) } func (c *example2Ctl) Edit(ctx *gin.Context) { id := gconv.Int(ctx.Query("id")) if id > 0 { // 编辑 info := &model.Example2{Id: id} has, err := info.Get() if !has || err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 渲染模板 response.BuildTpl(ctx, "example2_edit.html").WriteTpl(gin.H{ "info": info, }) } else { // 添加 response.BuildTpl(ctx, "example2_edit.html").WriteTpl() } } func (c *example2Ctl) Add(ctx *gin.Context) { // 参数绑定 var req *dto.Example2AddReq if err := ctx.ShouldBind(&req); err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 调用添加方法 rows, err := service.Example2.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 *example2Ctl) Update(ctx *gin.Context) { // 参数绑定 var req *dto.Example2UpdateReq if err := ctx.ShouldBind(&req); err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 调用更新方法 rows, err := service.Example2.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 *example2Ctl) Delete(ctx *gin.Context) { // 记录ID ids := ctx.Param("ids") if ids == "" { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: "记录ID不能为空", }) return } // 调用删除方法 rows, err := service.Example2.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: "删除成功", }) } func (c *example2Ctl) Status(ctx *gin.Context) { // 参数绑定 var req *dto.Example2StatusReq if err := ctx.ShouldBind(&req); err != nil { ctx.JSON(http.StatusOK, common.JsonResult{ Code: -1, Msg: err.Error(), }) return } // 调用设置状态方法 rows, err := service.Example2.Status(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: "设置成功", }) }