-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_comment.go
102 lines (95 loc) · 3.18 KB
/
controller_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
package controller
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/yhmain/5th-simple-tiktok/dao"
"github.com/yhmain/5th-simple-tiktok/middleware"
"github.com/yhmain/5th-simple-tiktok/model"
"github.com/yhmain/5th-simple-tiktok/util"
)
// 评论操作的返回结构体
type CommentResponse struct {
util.Response
Comment model.Comment
}
// 评论列表返回的结构体
type CommentListResponse struct {
util.Response
CommentList []model.Comment `json:"comment_list"`
}
//评论操作,路由
func CommentAction(c *gin.Context) {
// 经过jwt中间件
usertoken := c.MustGet("usertoken").(middleware.UserToken)
video_id := middleware.GetParamPostOrGet(c, "video_id")
vID, _ := strconv.ParseInt(video_id, 10, 64) // 字符串转化成int64
action_type := c.Query("action_type")
if action_type == "1" {
// 发布评论
comment_text := middleware.GetParamPostOrGet(c, "comment_text")
cID := util.GenID() // 生成评论ID
newComment := model.Comment{
Id: cID,
Content: comment_text,
CreatedTime: time.Now().Unix(),
UserID: usertoken.UserID,
VideoID: vID,
}
// 知道了视频的ID,先去查看redis里面是否存了它的评论数,若无则去查mysql数据库
if _, err := middleware.GetKey("ComCnt:" + video_id); err != nil {
video := dao.GetVideoByID(vID) // 去数据库查询 视频结构体
middleware.SetKey("ComCnt:"+video_id, strconv.FormatInt(video.CommentCount, 10)) // 评论数量加入redis
}
// 记录插入redis
middleware.UpdateRedisComment(action_type, newComment)
// 返回
c.JSON(http.StatusOK, CommentResponse{
Response: util.Success, //成功
Comment: newComment,
})
} else {
//删除评论
comment_id := middleware.GetParamPostOrGet(c, "comment_id")
cid, _ := strconv.ParseInt(comment_id, 10, 64)
newComment := model.Comment{
Id: cid,
UserID: usertoken.UserID,
VideoID: vID,
}
// 知道了视频的ID,先去查看redis里面是否存了它的评论数,若无则去查mysql数据库
if _, err := middleware.GetKey("ComCnt:" + video_id); err != nil {
video := dao.GetVideoByID(vID) // 去数据库查询 视频结构体
middleware.SetKey("ComCnt:"+video_id, strconv.FormatInt(video.CommentCount, 10)) // 评论数量加入redis
}
// 记录更新到redis
middleware.UpdateRedisComment(action_type, newComment)
// 返回
c.JSON(http.StatusOK, CommentResponse{
Response: util.Success, //成功
Comment: model.Comment{},
})
}
}
//评论列表,路由
func CommentList(c *gin.Context) {
// 查询之前,将Redis数据手段更新到Mysql
SaveRedisToMySQL()
// 再查询MySQL数据
video_id := middleware.GetParamPostOrGet(c, "video_id")
vID, _ := strconv.ParseInt(video_id, 10, 64) // 字符串转化成int64
comments, err := dao.GetCommentsByVid(vID) // 查询mysql数据
if err != nil {
c.JSON(http.StatusOK, CommentListResponse{
Response: util.ComListErr,
CommentList: []model.Comment{},
})
return
}
// 返回
c.JSON(http.StatusOK, CommentListResponse{
Response: util.Success,
CommentList: comments,
})
}