Repository navigation
Expand file tree
/
Copy patharticle_controller.go
More file actions
269 lines (233 loc) · 8.47 KB
/
Copy patharticle_controller.go
File metadata and controls
269 lines (233 loc) · 8.47 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package api
import (
"bbs-go/model/constants"
"bbs-go/pkg/urls"
"bbs-go/spam"
"math/rand"
"strconv"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"
"bbs-go/cache"
"bbs-go/controllers/render"
"bbs-go/model"
"bbs-go/services"
)
type ArticleController struct {
Ctx iris.Context
}
// 文章详情
func (c *ArticleController) GetBy(articleId int64) *simple.JsonResult {
article := services.ArticleService.Get(articleId)
if article == nil || article.Status == constants.StatusDeleted {
return simple.JsonErrorCode(404, "文章不存在")
}
user := services.UserTokenService.GetCurrent(c.Ctx)
if user != nil {
if article.UserId != user.Id && article.Status == constants.StatusPending {
return simple.JsonErrorCode(403, "文章审核中")
}
} else {
if article.Status == constants.StatusPending {
return simple.JsonErrorCode(403, "文章审核中")
}
}
services.ArticleService.IncrViewCount(articleId) // 增加浏览量
return simple.JsonData(render.BuildArticle(article))
}
// PostCreate 发表文章
func (c *ArticleController) PostCreate() *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
return simple.JsonError(err)
}
var (
tags = simple.FormValueStringArray(c.Ctx, "tags")
title = c.Ctx.PostValue("title")
summary = c.Ctx.PostValue("summary")
content = c.Ctx.PostValue("content")
)
form := model.CreateArticleForm{
Title: title,
Summary: summary,
Content: content,
ContentType: constants.ContentTypeMarkdown,
Tags: tags,
}
if err := spam.CheckArticle(user, form); err != nil {
return simple.JsonErrorMsg(err.Error())
}
article, err := services.ArticleService.Publish(user.Id, form)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonData(render.BuildArticle(article))
}
// 编辑时获取详情
func (c *ArticleController) GetEditBy(articleId int64) *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
return simple.JsonError(err)
}
article := services.ArticleService.Get(articleId)
if article == nil || article.Status == constants.StatusDeleted {
return simple.JsonErrorMsg("话题不存在或已被删除")
}
// 非作者、且非管理员
if article.UserId != user.Id && !user.HasAnyRole(constants.RoleAdmin, constants.RoleOwner) {
return simple.JsonErrorMsg("无权限")
}
tags := services.ArticleService.GetArticleTags(articleId)
var tagNames []string
if len(tags) > 0 {
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
}
return simple.NewEmptyRspBuilder().
Put("articleId", article.Id).
Put("title", article.Title).
Put("content", article.Content).
Put("tags", tagNames).
JsonResult()
}
// 编辑文章
func (c *ArticleController) PostEditBy(articleId int64) *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
return simple.JsonError(err)
}
var (
tags = simple.FormValueStringArray(c.Ctx, "tags")
title = c.Ctx.PostValue("title")
content = c.Ctx.PostValue("content")
)
article := services.ArticleService.Get(articleId)
if article == nil || article.Status == constants.StatusDeleted {
return simple.JsonErrorMsg("文章不存在")
}
// 非作者、且非管理员
if article.UserId != user.Id && !user.HasAnyRole(constants.RoleAdmin, constants.RoleOwner) {
return simple.JsonErrorMsg("无权限")
}
if err := services.ArticleService.Edit(articleId, tags, title, content); err != nil {
return simple.JsonError(err)
}
// 操作日志
services.OperateLogService.AddOperateLog(user.Id, constants.OpTypeUpdate, constants.EntityArticle, articleId,
"", c.Ctx.Request())
return simple.NewEmptyRspBuilder().Put("articleId", article.Id).JsonResult()
}
// 删除文章
func (c *ArticleController) PostDeleteBy(articleId int64) *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
return simple.JsonError(err)
}
article := services.ArticleService.Get(articleId)
if article == nil || article.Status == constants.StatusDeleted {
return simple.JsonErrorMsg("文章不存在")
}
// 非作者、且非管理员
if article.UserId != user.Id && !user.HasAnyRole(constants.RoleAdmin, constants.RoleOwner) {
return simple.JsonErrorMsg("无权限")
}
if err := services.ArticleService.Delete(articleId); err != nil {
return simple.JsonErrorMsg(err.Error())
}
// 操作日志
services.OperateLogService.AddOperateLog(user.Id, constants.OpTypeDelete, constants.EntityArticle, articleId,
"", c.Ctx.Request())
return simple.JsonSuccess()
}
// 收藏文章
func (c *ArticleController) PostFavoriteBy(articleId int64) *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if user == nil {
return simple.JsonError(simple.ErrorNotLogin)
}
err := services.FavoriteService.AddArticleFavorite(user.Id, articleId)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.JsonSuccess()
}
// 文章跳转链接
func (c *ArticleController) GetRedirectBy(articleId int64) *simple.JsonResult {
article := services.ArticleService.Get(articleId)
if article == nil || article.Status != constants.StatusOk {
return simple.JsonErrorMsg("文章不存在")
}
return simple.NewEmptyRspBuilder().Put("url", urls.ArticleUrl(articleId)).JsonResult()
}
// 最近文章
func (c *ArticleController) GetRecent() *simple.JsonResult {
articles := services.ArticleService.Find(simple.NewSqlCnd().Where("status = ?", constants.StatusOk).Desc("id").Limit(10))
return simple.JsonData(render.BuildSimpleArticles(articles))
}
// 用户文章列表
func (c *ArticleController) GetUserArticles() *simple.JsonResult {
userId, err := simple.FormValueInt64(c.Ctx, "userId")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
cursor := simple.FormValueInt64Default(c.Ctx, "cursor", 0)
articles, cursor, hasMore := services.ArticleService.GetUserArticles(userId, cursor)
return simple.JsonCursorData(render.BuildSimpleArticles(articles), strconv.FormatInt(cursor, 10), hasMore)
}
// 文章列表
func (c *ArticleController) GetArticles() *simple.JsonResult {
cursor := simple.FormValueInt64Default(c.Ctx, "cursor", 0)
articles, cursor, hasMore := services.ArticleService.GetArticles(cursor)
return simple.JsonCursorData(render.BuildSimpleArticles(articles), strconv.FormatInt(cursor, 10), hasMore)
}
// 标签文章列表
func (c *ArticleController) GetTagArticles() *simple.JsonResult {
cursor := simple.FormValueInt64Default(c.Ctx, "cursor", 0)
tagId := simple.FormValueInt64Default(c.Ctx, "tagId", 0)
articles, cursor, hasMore := services.ArticleService.GetTagArticles(tagId, cursor)
return simple.JsonCursorData(render.BuildSimpleArticles(articles), strconv.FormatInt(cursor, 10), hasMore)
}
// 用户最新的文章
func (c *ArticleController) GetUserNewestBy(userId int64) *simple.JsonResult {
articles := services.ArticleService.GetUserNewestArticles(userId)
return simple.JsonData(render.BuildSimpleArticles(articles))
}
// 近期文章
func (c *ArticleController) GetNearlyBy(articleId int64) *simple.JsonResult {
articles := services.ArticleService.GetNearlyArticles(articleId)
return simple.JsonData(render.BuildSimpleArticles(articles))
}
// 相关文章
func (c *ArticleController) GetRelatedBy(articleId int64) *simple.JsonResult {
relatedArticles := services.ArticleService.GetRelatedArticles(articleId)
return simple.JsonData(render.BuildSimpleArticles(relatedArticles))
}
// 推荐
func (c *ArticleController) GetRecommend() *simple.JsonResult {
articles := cache.ArticleCache.GetRecommendArticles()
if len(articles) == 0 {
return simple.JsonSuccess()
} else {
dest := make([]model.Article, len(articles))
perm := rand.Perm(len(articles))
for i, v := range perm {
dest[v] = articles[i]
}
end := 10
if end > len(articles) {
end = len(articles)
}
ret := dest[0:end]
return simple.JsonData(render.BuildSimpleArticles(ret))
}
}
// 最新文章
func (c *ArticleController) GetNewest() *simple.JsonResult {
articles := services.ArticleService.Find(simple.NewSqlCnd().Eq("status", constants.StatusOk).Desc("id").Limit(5))
return simple.JsonData(render.BuildSimpleArticles(articles))
}
// 热门文章
func (c *ArticleController) GetHot() *simple.JsonResult {
articles := cache.ArticleCache.GetHotArticles()
return simple.JsonData(render.BuildSimpleArticles(articles))
}