system_get_article_logic.go
3.4 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
package article
import (
"context"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemGetArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemGetArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemGetArticleLogic {
return &SystemGetArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemGetArticleLogic) SystemGetArticle(req *types.SystemArticleGetRequest) (resp *types.SystemArticleGetResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
images := make([]string, 0)
lo.ForEach(article.Images, func(img domain.Image, n int) {
images = append(images, img.Url)
})
resp = &types.SystemArticleGetResponse{
Id: article.Id,
Title: article.Title,
AuthorId: article.AuthorId,
Author: types.ArticleAuthor{
Id: article.Author.Id,
Name: article.Author.Name,
Avatar: article.Author.Avatar,
Position: article.Author.Position,
Company: article.Author.Company,
},
CreatedAt: article.CreatedAt,
Section: make([]types.ArticleSection, 0),
Images: images,
WhoRead: article.WhoRead,
WhoReadInfo: make([]types.UserShowName, 0),
WhoReview: article.WhoReview,
WhoReviewInfo: make([]types.UserShowName, 0),
Location: types.Location{
Longitude: article.Location.Longitude,
Latitude: article.Location.Latitude,
Descript: article.Location.Descript,
},
CountLove: article.CountLove,
CountComment: article.CountComment,
CountRead: article.CountRead,
Show: int(article.Show),
}
//文章段落
_, articleSections, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("articleId", req.Id))
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
lo.ForEach(articleSections, func(item *domain.ArticleSection, index int) {
resp.Section = append(resp.Section, types.ArticleSection{
Id: item.Id,
Content: item.Content,
SortBy: item.SortBy,
TotalComment: item.TotalComment,
})
})
userIds := lo.Union(resp.WhoRead, resp.WhoReview)
if len(userIds) > 0 {
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("ids", userIds))
if err != nil {
return nil, xerr.NewErrMsgErr("获取帖子异常", err)
}
userSlices := make(map[int64]types.UserShowName)
lo.ForEach(users, func(user *domain.User, index int) {
userSlices[user.Id] = types.UserShowName{
Id: int(user.Id),
Name: user.Name,
}
})
lo.ForEach(resp.WhoRead, func(userId int64, index int) {
if value, ok := userSlices[userId]; ok {
resp.WhoReadInfo = append(resp.WhoReadInfo, value)
}
})
lo.ForEach(resp.WhoReview, func(userId int64, index int) {
if value, ok := userSlices[userId]; ok {
resp.WhoReviewInfo = append(resp.WhoReviewInfo, value)
}
})
}
return resp, nil
}