You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.9 KiB
Go

11 months ago
package system
import (
"github.com/gin-gonic/gin"
"miniapp/api"
11 months ago
"miniapp/global"
"miniapp/model/app"
11 months ago
"miniapp/model/common"
"miniapp/model/common/request"
r "miniapp/model/common/response"
"strconv"
11 months ago
)
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
}
// 获取用户信息
var user app.User
api.GetUser(ctx, &user, false, true)
article, err := articleService.GetArticleById(Id, strconv.Itoa(int(user.ID)))
11 months ago
if err != nil {
global.GVA_LOG.Error("获取失败" + err.Error())
r.FailWithMessage("获取失败"+err.Error(), ctx)
return
}
r.OkWithDetailed(article, "获取成功", ctx)
}