/
article.go
358 lines (334 loc) · 8.8 KB
/
article.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package web
import (
"fmt"
"github.com/ecodeclub/ekit/slice"
"github.com/gevinzone/basic-go/week7/webook/internal/domain"
"github.com/gevinzone/basic-go/week7/webook/internal/service"
ijwt "github.com/gevinzone/basic-go/week7/webook/internal/web/jwt"
"github.com/gevinzone/basic-go/week7/webook/pkg/ginx"
"github.com/gevinzone/basic-go/week7/webook/pkg/logger"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
"net/http"
"strconv"
"time"
)
var _ handler = (*ArticleHandler)(nil)
type ArticleHandler struct {
svc service.ArticleService
l logger.LoggerV1
intrSvc service.InteractiveService
biz string
}
func NewArticleHandler(svc service.ArticleService,
l logger.LoggerV1) *ArticleHandler {
return &ArticleHandler{
svc: svc,
l: l,
biz: "article",
}
}
func (h *ArticleHandler) RegisterRoutes(server *gin.Engine) {
g := server.Group("/articles")
// 修改
//g.PUT("/")
// 新增
//g.POST("/")
// g.DELETE("/a_id")
g.POST("/edit", h.Edit)
g.POST("/withdraw", h.Withdraw)
g.POST("/publish", h.Publish)
// 创作者的查询接口
// 这个是获取数据的接口,理论上来说(遵循 RESTful 规范),应该是用 GET 方法
// GET localhost/articles => List 接口
g.POST("/list",
ginx.WrapBodyAndToken[ListReq, ijwt.UserClaims](h.List))
g.GET("/detail/:id", ginx.WrapToken[ijwt.UserClaims](h.Detail))
pub := g.Group("/pub")
pub.GET("/:id", h.PubDetail, func(ctx *gin.Context) {
// 增加阅读计数。
//go func() {
// // 开一个 goroutine,异步去执行
// er := a.intrSvc.IncrReadCnt(ctx, a.biz, art.Id)
// if er != nil {
// a.l.Error("增加阅读计数失败",
// logger.Int64("aid", art.Id),
// logger.Error(err))
// }
//}()
})
// 点赞是这个接口,取消点赞也是这个接口
// RESTful 风格
//pub.POST("/like/:id", ginx.WrapBodyAndToken[LikeReq,
// ijwt.UserClaims](h.Like))
pub.POST("/like", ginx.WrapBodyAndToken[LikeReq,
ijwt.UserClaims](h.Like))
//pub.POST("/cancel_like", ginx.WrapBodyAndToken[LikeReq,
// ijwt.UserClaims](h.Like))
}
func (a *ArticleHandler) Like(ctx *gin.Context, req LikeReq, uc ijwt.UserClaims) (ginx.Result, error) {
var err error
if req.Like {
err = a.intrSvc.Like(ctx, a.biz, req.Id, uc.Id)
} else {
err = a.intrSvc.CancelLike(ctx, a.biz, req.Id, uc.Id)
}
if err != nil {
return ginx.Result{
Code: 5,
Msg: "系统错误",
}, err
}
return ginx.Result{Msg: "OK"}, nil
}
func (a *ArticleHandler) PubDetail(ctx *gin.Context) {
idstr := ctx.Param("id")
id, err := strconv.ParseInt(idstr, 10, 64)
if err != nil {
ctx.JSON(http.StatusOK, Result{
Code: 4,
Msg: "参数错误",
})
a.l.Error("前端输入的 ID 不对", logger.Error(err))
return
}
uc := ctx.MustGet("users").(ijwt.UserClaims)
var eg errgroup.Group
var art domain.Article
eg.Go(func() error {
art, err = a.svc.GetPublishedById(ctx, id, uc.Id)
return err
})
var intr domain.Interactive
eg.Go(func() error {
// 这个地方可以容忍错误
intr, err = a.intrSvc.Get(ctx, a.biz, id, uc.Id)
// 这种是容错的写法
//if err != nil {
// // 记录日志
//}
//return nil
return err
})
// 在这儿等,要保证前面两个
err = eg.Wait()
if err != nil {
// 代表查询出错了
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
return
}
// 增加阅读计数。
go func() {
// 你都异步了,怎么还说有巨大的压力呢?
// 开一个 goroutine,异步去执行
er := a.intrSvc.IncrReadCnt(ctx, a.biz, art.Id)
if er != nil {
a.l.Error("增加阅读计数失败",
logger.Int64("aid", art.Id),
logger.Error(err))
}
}()
// ctx.Set("art", art)
// 这个功能是不是可以让前端,主动发一个 HTTP 请求,来增加一个计数?
ctx.JSON(http.StatusOK, Result{
Data: ArticleVO{
Id: art.Id,
Title: art.Title,
Status: art.Status.ToUint8(),
Content: art.Content,
// 要把作者信息带出去
Author: art.Author.Name,
Ctime: art.Ctime.Format(time.DateTime),
Utime: art.Utime.Format(time.DateTime),
Liked: intr.Liked,
Collected: intr.Collected,
LikeCnt: intr.LikeCnt,
ReadCnt: intr.ReadCnt,
CollectCnt: intr.CollectCnt,
},
})
}
func (a *ArticleHandler) Detail(ctx *gin.Context, usr ijwt.UserClaims) (ginx.Result, error) {
idstr := ctx.Param("id")
id, err := strconv.ParseInt(idstr, 10, 64)
if err != nil {
//ctx.JSON(http.StatusOK, )
//a.l.Error("前端输入的 ID 不对", logger.Error(err))
return ginx.Result{
Code: 4,
Msg: "参数错误",
}, err
}
art, err := a.svc.GetById(ctx, id)
if err != nil {
//ctx.JSON(http.StatusOK, )
//a.l.Error("获得文章信息失败", logger.Error(err))
return ginx.Result{
Code: 5,
Msg: "系统错误",
}, err
}
// 这是不借助数据库查询来判定的方法
if art.Author.Id != usr.Id {
//ctx.JSON(http.StatusOK)
// 如果公司有风控系统,这个时候就要上报这种非法访问的用户了。
//a.l.Error("非法访问文章,创作者 ID 不匹配",
// logger.Int64("uid", usr.Id))
return ginx.Result{
Code: 4,
// 也不需要告诉前端究竟发生了什么
Msg: "输入有误",
}, fmt.Errorf("非法访问文章,创作者 ID 不匹配 %d", usr.Id)
}
return ginx.Result{
Data: ArticleVO{
Id: art.Id,
Title: art.Title,
// 不需要这个摘要信息
//Abstract: art.Abstract(),
Status: art.Status.ToUint8(),
Content: art.Content,
// 这个是创作者看自己的文章列表,也不需要这个字段
//Author: art.Author
Ctime: art.Ctime.Format(time.DateTime),
Utime: art.Utime.Format(time.DateTime),
},
}, nil
}
func (h *ArticleHandler) Publish(ctx *gin.Context) {
var req ArticleReq
if err := ctx.Bind(&req); err != nil {
return
}
c := ctx.MustGet("claims")
claims, ok := c.(*ijwt.UserClaims)
if !ok {
// 你可以考虑监控住这里
//ctx.AbortWithStatus(http.StatusUnauthorized)
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
h.l.Error("未发现用户的 session 信息")
return
}
id, err := h.svc.Publish(ctx, req.toDomain(claims.Id))
if err != nil {
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
// 打日志?
h.l.Error("发表帖子失败", logger.Error(err))
return
}
ctx.JSON(http.StatusOK, Result{
Data: id,
})
}
func (h *ArticleHandler) Withdraw(ctx *gin.Context) {
type Req struct {
Id int64
}
var req Req
if err := ctx.Bind(&req); err != nil {
return
}
c := ctx.MustGet("claims")
claims, ok := c.(*ijwt.UserClaims)
if !ok {
// 你可以考虑监控住这里
//ctx.AbortWithStatus(http.StatusUnauthorized)
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
h.l.Error("未发现用户的 session 信息")
return
}
// 检测输入,跳过这一步
// 调用 svc 的代码
err := h.svc.Withdraw(ctx, domain.Article{
Id: req.Id,
Author: domain.Author{
Id: claims.Id,
},
})
if err != nil {
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
// 打日志?
h.l.Error("保存帖子失败", logger.Error(err))
return
}
ctx.JSON(http.StatusOK, Result{
Msg: "OK",
})
}
func (h *ArticleHandler) Edit(ctx *gin.Context) {
var req ArticleReq
if err := ctx.Bind(&req); err != nil {
return
}
c := ctx.MustGet("claims")
claims, ok := c.(*ijwt.UserClaims)
if !ok {
// 你可以考虑监控住这里
//ctx.AbortWithStatus(http.StatusUnauthorized)
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
h.l.Error("未发现用户的 session 信息")
return
}
// 检测输入,跳过这一步
// 调用 svc 的代码
id, err := h.svc.Save(ctx, req.toDomain(claims.Id))
if err != nil {
ctx.JSON(http.StatusOK, Result{
Code: 5,
Msg: "系统错误",
})
// 打日志?
h.l.Error("保存帖子失败", logger.Error(err))
return
}
ctx.JSON(http.StatusOK, Result{
Data: id,
})
}
func (h *ArticleHandler) List(ctx *gin.Context, req ListReq, uc ijwt.UserClaims) (ginx.Result, error) {
res, err := h.svc.List(ctx, uc.Id, req.Offset, req.Limit)
if err != nil {
return ginx.Result{
Code: 5,
Msg: "系统错误",
}, nil
}
// 在列表页,不显示全文,只显示一个"摘要"
// 比如说,简单的摘要就是前几句话
// 强大的摘要是 AI 帮你生成的
return ginx.Result{
Data: slice.Map[domain.Article, ArticleVO](res,
func(idx int, src domain.Article) ArticleVO {
return ArticleVO{
Id: src.Id,
Title: src.Title,
Abstract: src.Abstract(),
Status: src.Status.ToUint8(),
// 这个列表请求,不需要返回内容
//Content: src.Content,
// 这个是创作者看自己的文章列表,也不需要这个字段
//Author: src.Author
Ctime: src.Ctime.Format(time.DateTime),
Utime: src.Utime.Format(time.DateTime),
}
}),
}, nil
}