/
comment.go
132 lines (113 loc) · 3.44 KB
/
comment.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
package service
import (
"sort"
"github.com/yunyandz/tiktok-demo-backend/internal/errorx"
"github.com/yunyandz/tiktok-demo-backend/internal/logger"
"github.com/yunyandz/tiktok-demo-backend/internal/model"
)
type Comment struct {
Id uint64 `json:"id,omitempty"`
User User `json:"user"`
Content string `json:"content,omitempty"`
CreateDate string `json:"create_date,omitempty"`
}
type CommentListResponse struct {
Response
CommentList []Comment `json:"comment_list,omitempty"`
}
type CommentActionResponse struct {
Response
Comment Comment `json: "comment"`
}
func (s *Service) PublishComment(userId uint64, videoId uint64, content string) (*CommentActionResponse, error) {
videoModel := model.NewVideoModel(s.db, s.rds)
comment, err := videoModel.CreateAComment(videoId, userId, content)
// Video数据库写入评论失败
if err != nil {
logger.Suger().Errorw("PublishComment CreateComment failed", "err", err.Error())
return nil, errorx.ErrInternalBusy
}
// User数据库查找评论用户信息失败
r, err := s.GetUserInfo(userId, userId)
if err != nil {
return nil, errorx.ErrUserDoesNotExists
}
rsp := CommentActionResponse{
Response: r.Response,
Comment: Comment{
Id: uint64(comment.Model.ID),
User: r.User,
Content: content,
CreateDate: comment.Model.CreatedAt.Format("01-02"), //格式化为mm-dd
},
}
return &rsp, nil
}
func (s *Service) DeleteComment(selfId uint64, commentId uint64) (*CommentActionResponse, error) {
videoModel := model.NewVideoModel(s.db, s.rds)
comment, err := videoModel.FindAComment(commentId)
if err != nil {
return nil, errorx.ErrCommentDoesNotExists
}
if comment.UserID != selfId {
return nil, errorx.ErrPermissionDenied
}
err = videoModel.DeleteAComment(commentId)
// Video数据库删除评论失败
if err != nil {
logger.Suger().Errorw("DeleteComment DeleteAComment failed", "err", err.Error())
return nil, errorx.ErrInternalBusy
}
r, err := s.GetUserInfo(selfId, comment.UserID)
// User数据库查找用户失败
if err != nil {
return nil, errorx.ErrUserDoesNotExists
}
rsp := CommentActionResponse{
Response: r.Response,
Comment: Comment{
Id: uint64(comment.Model.ID),
User: r.User,
Content: comment.Content,
CreateDate: comment.Model.CreatedAt.Format("01-02"), //格式化为mm-dd
},
}
return &rsp, nil
}
func (s *Service) GetCommentList(selfId uint64, videoId uint64) (*CommentListResponse, error) {
videoModel := model.NewVideoModel(s.db, s.rds)
comments, err := videoModel.GetVideoComments(videoId)
if err != nil {
return nil, errorx.ErrCommentDoesNotExists
}
s.sortCommentByTime(comments)
var commentList []Comment
for i := 0; i < len(comments); i++ {
commentA := comments[i]
r, err := s.GetUserInfo(selfId, commentA.UserID)
// User数据库查找用户失败
if err != nil {
return nil, errorx.ErrUserDoesNotExists
}
commentB := Comment{
Id: uint64(commentA.Model.ID),
User: r.User,
Content: commentA.Content,
CreateDate: commentA.Model.CreatedAt.Format("01-02"),
}
commentList = append(commentList, commentB)
}
rsp := CommentListResponse{
Response: Response{
StatusCode: 0,
StatusMsg: "ok",
},
CommentList: commentList,
}
return &rsp, nil
}
func (s *Service) sortCommentByTime(comments []*model.Comment) {
sort.Slice(comments, func(i, j int) bool {
return comments[i].Model.CreatedAt.After(comments[j].Model.CreatedAt)
})
}