-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
131 lines (121 loc) · 3.24 KB
/
post.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
package logic
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/miaogu-go/bluebell/dao/mysql"
"github.com/miaogu-go/bluebell/dao/redis"
"github.com/miaogu-go/bluebell/models"
"github.com/miaogu-go/bluebell/pkg/snowflake"
)
type PostDetail struct {
AuthorName string `json:"author_name"`
VoteNum int64 `json:"vote_num"`
mysql.Post
Community mysql.Community `json:"community"`
}
// CreatePost 创建帖子
func CreatePost(c *gin.Context, param *models.CreatePostReq) error {
param.PostId = snowflake.GetID()
err := mysql.CreatePost(param)
if err != nil {
return err
}
// 保存发布时间
err = redis.SavePostPublishTime(param.PostId)
if err != nil {
return err
}
//保存社区与帖子集合
err = redis.SaveCommunityPost(fmt.Sprintf("%d", param.CommunityId), param.PostId)
if err != nil {
return err
}
return nil
}
// GetPostDetail 获取帖子详情
func GetPostDetail(c *gin.Context, postId int64) (*PostDetail, error) {
post, err := mysql.GetPostById(postId)
if err != nil {
return nil, err
}
user, err := mysql.GetUserByUserId(post.AuthorId)
if err != nil {
return nil, err
}
community, err := mysql.GetCommunityDetailById(post.CommunityId)
if err != nil {
return nil, err
}
postDetail := &PostDetail{
AuthorName: user.Username,
Post: *post,
Community: *community,
}
return postDetail, nil
}
// GetPosts 帖子列表
func GetPosts(c *gin.Context, param *models.PostsReq) ([]mysql.Post, error) {
posts, err := mysql.GetPosts(param.Page, param.PageSize)
if err != nil {
return nil, err
}
return posts, nil
}
// GetPosts2 帖子列表2
func GetPosts2(c *gin.Context, param *models.PostsReq) ([]PostDetail, error) {
var err error
start := (param.Page - 1) * param.PageSize
pageSize := start + 1
postIds := make([]string, 0)
//根据社区id判断是跟时间还是分数虎丘帖子列表
//分数:按照分数获取
//时间:按照时间取
if param.CommunityId > 0 { //按照社区key查询
//获取社区文章key
communityPosyKey := redis.GetKeyCommunityPost(fmt.Sprintf("%d", param.CommunityId))
postIds, err = redis.GetCommunityPostIds(communityPosyKey, param.OrderType, int64(start), int64(pageSize))
if err != nil {
return nil, err
}
} else {
if param.OrderType == models.PostOrderTypeTime { //按照时间获取
postIds = redis.GetPostIdsByTime(int64(start), int64(pageSize))
if postIds == nil || len(postIds) == 0 {
return nil, nil
}
} else {
postIds = redis.GetPostIdsByScore(int64(start), int64(pageSize))
if postIds == nil || len(postIds) == 0 {
return nil, nil
}
}
}
posts, err := mysql.GetPostsByIds(postIds)
if err != nil {
return nil, err
}
//获取帖子赞成票数量
voteNums, err := redis.GetPostVoteNums(postIds)
if err != nil {
return nil, err
}
postsResp := make([]PostDetail, 0, len(posts))
for i, post := range posts {
user, err := mysql.GetUserByUserId(post.AuthorId)
if err != nil {
return nil, err
}
community, err := mysql.GetCommunityDetailById(post.CommunityId)
if err != nil {
return nil, err
}
postDetail := PostDetail{
AuthorName: user.Username,
Post: post,
Community: *community,
VoteNum: voteNums[i],
}
postsResp = append(postsResp, postDetail)
}
return postsResp, nil
}