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

215 lines
4.0 KiB
Go

/**
* 站点管理-控制器
* @author
* @since 2021/11/13
* @File : item
*/
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 Item = new(itemCtl)
type itemCtl struct{}
func (c *itemCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "item_index.html").WriteTpl(gin.H{
"typeList": common.ITEM_TYPE_LIST,
})
}
func (c *itemCtl) List(ctx *gin.Context) {
// 参数
var req *dto.ItemPageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用分页查询方法
list, count, err := service.Item.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,
Msg: "查询成功",
Data: list,
Count: count,
})
}
func (c *itemCtl) Edit(ctx *gin.Context) {
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 编辑
info := &model.Item{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 站点图片
info.Image = utils.GetImageUrl(info.Image)
// 渲染模板
response.BuildTpl(ctx, "item_edit.html").WriteTpl(gin.H{
"info": info,
"typeList": common.ITEM_TYPE_LIST,
})
} else {
// 添加
// 渲染模板
response.BuildTpl(ctx, "item_edit.html").WriteTpl(gin.H{
"typeList": common.ITEM_TYPE_LIST,
})
}
}
func (c *itemCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.ItemAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Item.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 *itemCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.ItemUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Item.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 *itemCtl) Delete(ctx *gin.Context) {
// 参数
ids := ctx.Param("ids")
if ids == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "记录ID不能为空",
})
return
}
// 调用删除方法
rows, err := service.Item.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 *itemCtl) Status(ctx *gin.Context) {
// 参数
var req *dto.ItemStatusReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置方法
rows, err := service.Item.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: "设置成功",
})
}
func (c *itemCtl) GetItemList(ctx *gin.Context) {
// 查询站点列表
list := make([]model.Item, 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,
})
}