107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
|
package system
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"miniapp/global"
|
||
|
"miniapp/model/common"
|
||
|
"miniapp/model/common/request"
|
||
|
r "miniapp/model/common/response"
|
||
|
)
|
||
|
|
||
|
type ArticleApi struct{}
|
||
|
|
||
|
// GetArticleList 获取文章列表
|
||
|
func (a ArticleApi) GetArticleList(ctx *gin.Context) {
|
||
|
var p request.PageInfo
|
||
|
if err := ctx.ShouldBind(&p); err != nil {
|
||
|
global.GVA_LOG.Error("参数错误" + err.Error())
|
||
|
r.FailWithMessage("参数错误"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
list, total, err := articleService.GetArticleList(p)
|
||
|
if err != nil {
|
||
|
global.GVA_LOG.Error("获取文章列表失败" + err.Error())
|
||
|
r.FailWithMessage("获取文章列表失败", ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
r.OkWithDetailed(r.PageResult{List: list, Total: total, Page: p.Page, PageSize: p.PageSize}, "获取成功", ctx)
|
||
|
}
|
||
|
|
||
|
// CreateArticle 创建文章
|
||
|
func (a ArticleApi) CreateArticle(ctx *gin.Context) {
|
||
|
var article common.Article
|
||
|
if err := ctx.ShouldBindJSON(&article); err != nil {
|
||
|
global.GVA_LOG.Error("参数错误" + err.Error())
|
||
|
r.FailWithMessage("参数错误"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err := articleService.CreateArticle(&article)
|
||
|
if err != nil {
|
||
|
global.GVA_LOG.Error("创建失败" + err.Error())
|
||
|
r.FailWithMessage("创建失败"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
r.OkWithMessage("创建成功", ctx)
|
||
|
}
|
||
|
|
||
|
// UpdateArticle 更新文章
|
||
|
func (a ArticleApi) UpdateArticle(ctx *gin.Context) {
|
||
|
var article common.Article
|
||
|
if err := ctx.ShouldBindJSON(&article); err != nil {
|
||
|
global.GVA_LOG.Error("参数错误" + err.Error())
|
||
|
r.FailWithMessage("参数错误"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err := articleService.UpdateArticle(&article)
|
||
|
if err != nil {
|
||
|
global.GVA_LOG.Error("更新失败" + err.Error())
|
||
|
r.FailWithMessage("更新失败"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
r.OkWithMessage("更新成功", ctx)
|
||
|
}
|
||
|
|
||
|
// DeleteArticle 删除文章
|
||
|
func (a ArticleApi) DeleteArticle(ctx *gin.Context) {
|
||
|
var article common.Article
|
||
|
if err := ctx.ShouldBind(&article); err != nil {
|
||
|
global.GVA_LOG.Error("参数错误" + err.Error())
|
||
|
r.FailWithMessage("参数错误"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err := articleService.DeleteArticle(&article)
|
||
|
if err != nil {
|
||
|
global.GVA_LOG.Error("删除失败" + err.Error())
|
||
|
r.FailWithMessage("删除失败"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
r.OkWithMessage("删除成功", ctx)
|
||
|
}
|
||
|
|
||
|
// GetArticleById 根据id获取文章
|
||
|
func (a ArticleApi) GetArticleById(ctx *gin.Context) {
|
||
|
Id := ctx.Param("id")
|
||
|
if Id == "" {
|
||
|
global.GVA_LOG.Error("参数错误")
|
||
|
r.FailWithMessage("参数错误", ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
article, err := articleService.GetArticleById(Id)
|
||
|
if err != nil {
|
||
|
global.GVA_LOG.Error("获取失败" + err.Error())
|
||
|
r.FailWithMessage("获取失败"+err.Error(), ctx)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
r.OkWithDetailed(article, "获取成功", ctx)
|
||
|
}
|