Repository navigation
Expand file tree
/
Copy patharticle_controller.go
More file actions
162 lines (138 loc) · 4.29 KB
/
Copy patharticle_controller.go
File metadata and controls
162 lines (138 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package admin
import (
"bbs-go/model/constants"
"bbs-go/pkg/sitemap"
"github.com/mlogclub/simple/date"
"strconv"
"bbs-go/model"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"
"bbs-go/cache"
"bbs-go/controllers/render"
"bbs-go/pkg/common"
"bbs-go/services"
)
type ArticleController struct {
Ctx iris.Context
}
func (c *ArticleController) GetSitemap() *simple.JsonResult {
go func() {
sitemap.Generate()
}()
return simple.JsonSuccess()
}
func (c *ArticleController) GetBy(id int64) *simple.JsonResult {
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return simple.JsonData(t)
}
func (c *ArticleController) AnyList() *simple.JsonResult {
var (
id = simple.FormValueInt64Default(c.Ctx, "id", 0)
userId = simple.FormValueInt64Default(c.Ctx, "userId", 0)
)
params := simple.NewQueryParams(c.Ctx)
if id > 0 {
params.Eq("id", id)
}
if userId > 0 {
params.Eq("user_id", userId)
}
params.EqByReq("status").EqByReq("title").PageByReq().Desc("id")
if id <= 0 && userId <= 0 {
return simple.JsonErrorMsg("请指定查询的【文章编号】或【作者编号】")
}
list, paging := services.ArticleService.FindPageByParams(params)
results := c.buildArticles(list)
return simple.JsonPageData(results, paging)
}
// GetRecent 展示最近一页数据
func (c *ArticleController) GetRecent() *simple.JsonResult {
params := simple.NewQueryParams(c.Ctx).EqByReq("id").EqByReq("user_id").EqByReq("status").Desc("id").Limit(20)
list := services.ArticleService.Find(¶ms.SqlCnd)
results := c.buildArticles(list)
return simple.JsonData(results)
}
// 构建文章列表返回数据
func (c *ArticleController) buildArticles(articles []model.Article) []map[string]interface{} {
var results []map[string]interface{}
for _, article := range articles {
builder := simple.NewRspBuilderExcludes(article, "content")
// 用户
builder = builder.Put("user", render.BuildUserInfoDefaultIfNull(article.UserId))
// 简介
builder.Put("summary", common.GetSummary(article.ContentType, article.Content))
// 标签
tagIds := cache.ArticleTagCache.Get(article.Id)
tags := cache.TagCache.GetList(tagIds)
builder.Put("tags", render.BuildTags(tags))
results = append(results, builder.Build())
}
return results
}
func (c *ArticleController) PostUpdate() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
t := services.ArticleService.Get(id)
if t == nil {
return simple.JsonErrorMsg("entity not found")
}
if err := simple.ReadForm(c.Ctx, t); err != nil {
return simple.JsonErrorMsg(err.Error())
}
// 数据校验
if len(t.Title) == 0 {
return simple.JsonErrorMsg("标题不能为空")
}
if len(t.Content) == 0 {
return simple.JsonErrorMsg("内容不能为空")
}
if len(t.ContentType) == 0 {
return simple.JsonErrorMsg("请选择内容格式")
}
t.UpdateTime = date.NowTimestamp()
err := services.ArticleService.Update(t)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonData(t)
}
func (c *ArticleController) GetTags() *simple.JsonResult {
articleId := simple.FormValueInt64Default(c.Ctx, "articleId", 0)
tags := services.ArticleService.GetArticleTags(articleId)
return simple.JsonData(render.BuildTags(tags))
}
func (c *ArticleController) PutTags() *simple.JsonResult {
var (
articleId = simple.FormValueInt64Default(c.Ctx, "articleId", 0)
tags = simple.FormValueStringArray(c.Ctx, "tags")
)
services.ArticleService.PutTags(articleId, tags)
return simple.JsonData(render.BuildTags(services.ArticleService.GetArticleTags(articleId)))
}
func (c *ArticleController) PostDelete() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
err := services.ArticleService.Delete(id)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
func (c *ArticleController) PostPending() *simple.JsonResult {
id := c.Ctx.PostValueInt64Default("id", 0)
if id <= 0 {
return simple.JsonErrorMsg("id is required")
}
err := services.ArticleService.UpdateColumn(id, "status", constants.StatusOk)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}