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

207 lines
4.0 KiB
Go

/**
* 栏目管理-控制器
* @author
* @since 2021/11/13
* @File : item_cate
*/
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 ItemCate = new(itemCateCtl)
type itemCateCtl struct{}
func (c *itemCateCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "itemcate_index.html").WriteTpl()
}
func (c *itemCateCtl) List(ctx *gin.Context) {
// 参数
var req *dto.ItemCateQueryReq
//if err := ctx.ShouldBind(&req); err != nil {
// ctx.JSON(http.StatusOK, common.JsonResult{
// Code: -1,
// Msg: err.Error(),
// })
// return
//}
// 调用
list := service.ItemCate.GetList(req)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}
func (c *itemCateCtl) Edit(ctx *gin.Context) {
// 站点列表
result := make([]model.Item, 0)
utils.XormDb.Where("mark=1").Find(&result)
var itemList = map[int]string{}
for _, v := range result {
itemList[v.Id] = v.Name
}
// 记录ID
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 编辑
info := &model.ItemCate{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 封面
if info.IsCover == 1 && info.Cover != "" {
info.Cover = utils.GetImageUrl(info.Cover)
}
// 渲染模板
response.BuildTpl(ctx, "itemcate_edit.html").WriteTpl(gin.H{
"info": info,
"itemList": itemList,
})
} else {
// 添加
response.BuildTpl(ctx, "itemcate_edit.html").WriteTpl(gin.H{
"itemList": itemList,
})
}
}
func (c *itemCateCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.ItemCateAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.ItemCate.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 *itemCateCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.ItemCateUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.ItemCate.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 *itemCateCtl) 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.ItemCate.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 *itemCateCtl) GetCateList(ctx *gin.Context) {
list := make([]model.ItemCate, 0)
utils.XormDb.Where("status=1 and mark=1").OrderBy("sort asc").Find(&list)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}
func (c *itemCateCtl) GetCateTreeList(ctx *gin.Context) {
itemId := gconv.Int(ctx.Query("itemId"))
list, err := service.ItemCate.GetCateTreeList(itemId, 0)
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 数据源转换
result := service.ItemCate.MakeList(list)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "操作成功",
Data: result,
})
}