Skip to content

Commit 9e703c7

Browse files
authored
feature: #11评论模块以及#33整合服务接口之间的调用 (X-Engineer#35)
videoService和likeService两个部分互相调用应该不会产生冲突,下面记录一下DeBug之旅:
1 parent d2aaac4 commit 9e703c7

28 files changed

+752
-182
lines changed

Diff for: controller/comment.go

+90-18
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,117 @@ package controller
33
import (
44
"github.com/gin-gonic/gin"
55
"net/http"
6+
"strconv"
7+
"x-tiktok/dao"
8+
"x-tiktok/service"
69
)
710

811
type CommentListResponse struct {
912
Response
10-
CommentList []Comment `json:"comment_list,omitempty"`
13+
CommentList []service.Comment `json:"comment_list,omitempty"`
1114
}
1215

1316
type CommentActionResponse struct {
1417
Response
15-
Comment Comment `json:"comment,omitempty"`
18+
Comment service.Comment `json:"comment,omitempty"`
1619
}
1720

1821
// CommentAction no practical effect, just check if token is valid
1922
func CommentAction(c *gin.Context) {
20-
token := c.Query("token")
21-
actionType := c.Query("action_type")
23+
// 获取userId
24+
userId := c.GetInt64("userId")
25+
videoId, err := strconv.ParseInt(c.Query("video_id"), 10, 64)
26+
if err != nil {
27+
c.JSON(http.StatusOK, CommentActionResponse{
28+
Response: Response{StatusCode: -1,
29+
StatusMsg: "comment videoId json invalid"},
30+
})
31+
return
32+
}
33+
actionType, err := strconv.ParseInt(c.Query("action_type"), 10, 64)
34+
if err != nil || actionType < 1 || actionType > 2 {
35+
c.JSON(http.StatusOK, CommentActionResponse{
36+
Response: Response{StatusCode: -1,
37+
StatusMsg: "comment actionType json invalid"},
38+
})
39+
return
40+
}
41+
commentService := service.GetCommentServiceInstance()
42+
switch {
43+
// 评论
44+
case actionType == 1:
45+
content := c.Query("comment_text")
46+
var comment dao.Comment
47+
comment.UserId = userId
48+
comment.VideoId = videoId
49+
comment.Content = content
50+
comment.ActionType = 1
51+
commentRes, err := commentService.CommentAction(comment)
52+
if err != nil {
53+
c.JSON(http.StatusOK, CommentActionResponse{
54+
Response: Response{StatusCode: -1,
55+
StatusMsg: "comment failed"},
56+
})
57+
return
58+
}
59+
c.JSON(http.StatusOK, CommentActionResponse{
60+
Response: Response{StatusCode: 0,
61+
StatusMsg: "comment success"},
62+
Comment: commentRes,
63+
})
64+
return
2265

23-
if user, exist := usersLoginInfo[token]; exist {
24-
if actionType == "1" {
25-
text := c.Query("comment_text")
26-
c.JSON(http.StatusOK, CommentActionResponse{Response: Response{StatusCode: 0},
27-
Comment: Comment{
28-
Id: 1,
29-
User: user,
30-
Content: text,
31-
CreateDate: "05-01",
32-
}})
66+
// 取消评论
67+
case actionType == 2:
68+
commentId, err := strconv.ParseInt(c.Query("comment_id"), 10, 64)
69+
if err != nil {
70+
c.JSON(http.StatusOK, CommentActionResponse{
71+
Response: Response{StatusCode: -1,
72+
StatusMsg: "delete commentId invalid"},
73+
})
3374
return
3475
}
35-
c.JSON(http.StatusOK, Response{StatusCode: 0})
36-
} else {
37-
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
76+
err = commentService.DeleteCommentAction(commentId)
77+
if err != nil {
78+
c.JSON(http.StatusOK, CommentActionResponse{
79+
Response: Response{StatusCode: -1,
80+
StatusMsg: err.Error()},
81+
})
82+
return
83+
}
84+
c.JSON(http.StatusOK, CommentActionResponse{
85+
Response: Response{StatusCode: 0,
86+
StatusMsg: "delete commentId success"},
87+
})
88+
return
3889
}
3990
}
4091

4192
// CommentList all videos have same demo comment list
4293
func CommentList(c *gin.Context) {
94+
userId := c.GetInt64("userId")
95+
videoId, err := strconv.ParseInt(c.Query("video_id"), 10, 64)
96+
if err != nil {
97+
c.JSON(http.StatusOK, CommentListResponse{
98+
Response: Response{StatusCode: -1,
99+
StatusMsg: "comment videoId json invalid"},
100+
})
101+
return
102+
}
103+
commentService := service.GetCommentServiceInstance()
104+
commentList, err := commentService.GetCommentList(videoId, userId)
105+
// 获取评论列表失败
106+
if err != nil {
107+
c.JSON(http.StatusOK, CommentListResponse{
108+
Response: Response{StatusCode: -1,
109+
StatusMsg: err.Error()},
110+
})
111+
return
112+
}
113+
// 获取评论列表成功
43114
c.JSON(http.StatusOK, CommentListResponse{
44115
Response: Response{StatusCode: 0},
45-
CommentList: DemoComments,
116+
CommentList: commentList,
46117
})
118+
return
47119
}

Diff for: controller/favorite.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type GetFavouriteListResponse struct {
1717
VideoList []service.Video `json:"video_list"`
1818
}
1919

20-
// FavoriteAction 点赞操作
20+
// FavoriteAction 赞操作
2121
func FavoriteAction(c *gin.Context) {
2222
videoId, _ := strconv.ParseInt(c.Query("video_id"), 10, 64)
2323
actionType, _ := strconv.ParseInt(c.Query("action_type"), 10, 32)
@@ -42,26 +42,14 @@ func FavoriteAction(c *gin.Context) {
4242
}
4343
}
4444

45-
// FavoriteList 获取用户的点赞视频列表
45+
// FavoriteList 获取点赞列表
4646
func FavoriteList(c *gin.Context) {
4747
strUserId := c.Query("user_id")
4848
//likeCnt:=dao.VideoLikedCount()
4949
userId, _ := strconv.ParseInt(strUserId, 10, 64)
5050
Fni := service.NewLikeServImpInstance()
51-
52-
_, err := Fni.GetLikesList(userId)
53-
//video类型数组的假数据
54-
//var video_list []service.Video
55-
videoList := make([]service.Video, 1, 2)
56-
videoList[0].Id = 1
57-
videoList[0].IsFavorite = true
58-
videoList[0].AuthorId = 1
59-
videoList[0].CoverUrl = ""
60-
videoList[0].PlayUrl = ""
61-
videoList[0].Title = ""
62-
videoList[0].CommentCount = 10
63-
videoList[0].FavoriteCount = 100
64-
videoList[0].IsFavorite = true
51+
// 返回视频列表信息
52+
videoList, err := Fni.GetLikesList(userId)
6553
if err == nil {
6654
log.Printf("方法like.GetFavouriteList(userid) 成功")
6755
c.JSON(http.StatusOK, GetFavouriteListResponse{

Diff for: controller/feed.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func Feed(c *gin.Context) {
3232
}
3333
//log.Println("返回视频的最新投稿时间:", convTime)
3434
// 获取登录用户的 id(等待用户模块存入用户id到context)
35-
userId, _ := strconv.ParseInt(c.GetString("userId"), 10, 64)
35+
//userId, _ := strconv.ParseInt(c.GetString("userId"), 10, 64)
36+
userId := c.GetInt64("userId")
3637
videoService := service.GetVideoServiceInstance()
3738
videos, nextTime, err := videoService.Feed(convTime, userId)
3839
if err != nil {

Diff for: controller/publish.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type VideoListResponse struct {
1818
func Publish(c *gin.Context) {
1919
token := c.PostForm("token")
2020
log.Println("token:", token)
21+
userId := c.GetInt64("userId")
2122
data, err := c.FormFile("data")
2223
if err != nil {
2324
c.JSON(http.StatusOK, Response{
@@ -30,7 +31,6 @@ func Publish(c *gin.Context) {
3031
log.Printf("视频 title: %v\n", title)
3132
videoService := service.GetVideoServiceInstance()
3233
// 从 token 中获取 userId
33-
userId := c.GetInt64("userId")
3434
err = videoService.Publish(data, title, userId)
3535
if err != nil {
3636
log.Println("上传文件失败")
@@ -49,8 +49,10 @@ func Publish(c *gin.Context) {
4949

5050
// PublishList 用户的视频发布列表,直接列出用户所有投稿过的视频
5151
func PublishList(c *gin.Context) {
52+
// 获取到的userId,被访问的用户id
5253
reqUserId := c.Query("user_id")
5354
userId, _ := strconv.ParseInt(reqUserId, 10, 64)
55+
//userId := c.GetInt64("userId")
5456
log.Println("获取到用户 Id:", userId)
5557
token := c.Query("token")
5658
log.Println("获取到用户 token:", token)

Diff for: controller/user.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@ import (
1010
"x-tiktok/util"
1111
)
1212

13-
// usersLoginInfo use map to store user info, and key is username+password for demo
14-
// user data will be cleared every time the server starts
15-
// test data: username=zhanglei, password=douyin
16-
var usersLoginInfo = map[string]User{
17-
"zhangleidouyin": {
18-
Id: 1,
19-
Name: "zhanglei",
20-
FollowCount: 10,
21-
FollowerCount: 5,
22-
IsFollow: true,
23-
},
24-
}
25-
26-
var userIdSequence = int64(1)
27-
2813
type UserLoginResponse struct {
2914
Response
3015
UserId int64 `json:"user_id,omitempty"`
@@ -33,13 +18,13 @@ type UserLoginResponse struct {
3318

3419
type UserResponse struct {
3520
Response
36-
User User `json:"user"`
21+
User service.User `json:"user"`
3722
}
3823

3924
func Register(c *gin.Context) {
4025
username := c.Query("username")
4126
password := c.Query("password")
42-
usi := service.UserServiceImpl{}
27+
usi := service.GetUserServiceInstance()
4328
user := usi.GetUserBasicInfoByName(username)
4429
if username == user.Name {
4530
c.JSON(http.StatusOK, UserLoginResponse{
@@ -73,7 +58,7 @@ func Login(c *gin.Context) {
7358
encoderPassword := service.EnCoder(password)
7459
//log.Println("encoderPassword:", encoderPassword)
7560
// 登录逻辑:使用jwt,根据用户信息生成token
76-
usi := service.UserServiceImpl{}
61+
usi := service.GetUserServiceInstance()
7762

7863
user := usi.GetUserBasicInfoByName(username)
7964
userId := user.Id
@@ -95,7 +80,7 @@ func Login(c *gin.Context) {
9580
func UserInfo(c *gin.Context) {
9681
userId := c.Query("user_id")
9782
// 使用中间件,做token权限校验
98-
usi := service.UserServiceImpl{}
83+
usi := service.GetUserServiceInstance()
9984
id, _ := strconv.ParseInt(userId, 10, 64)
10085
if user, err := usi.GetUserLoginInfoById(id); err != nil {
10186
c.JSON(http.StatusOK, UserResponse{
@@ -104,7 +89,7 @@ func UserInfo(c *gin.Context) {
10489
} else {
10590
c.JSON(http.StatusOK, UserResponse{
10691
Response: Response{StatusCode: 0, StatusMsg: "Query Success"},
107-
User: User(user),
92+
User: user,
10893
})
10994
}
11095
}

Diff for: dao/commentDao.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dao
2+
3+
import (
4+
"errors"
5+
"log"
6+
"time"
7+
)
8+
9+
type Comment struct {
10+
Id int64 //评论id
11+
UserId int64 //评论用户id
12+
VideoId int64 //视频id
13+
Content string //评论内容
14+
ActionType int64 //发布评论为1,取消评论为2
15+
CreatedAt time.Time //评论发布的日期mm-dd
16+
UpdatedAt time.Time
17+
}
18+
19+
// TableName 修改表名映射
20+
func (Comment) TableName() string {
21+
return "comment"
22+
}
23+
24+
func InsertComment(comment Comment) (Comment, error) {
25+
if err := Db.Model(Comment{}).Create(&comment).Error; err != nil {
26+
log.Println(err.Error())
27+
return Comment{}, err
28+
}
29+
return comment, nil
30+
}
31+
32+
func DeleteComment(commentId int64) error {
33+
var comment Comment
34+
// 先查询是否有此评论
35+
result := Db.Where("id = ?", commentId).
36+
First(&comment)
37+
if result.Error != nil {
38+
return errors.New("del comment is not exist")
39+
}
40+
// 删除评论,将action_type置为2
41+
result = Db.Model(Comment{}).
42+
Where("id=?", commentId).
43+
Update("action_type", 2)
44+
if result.Error != nil {
45+
return result.Error
46+
}
47+
return nil
48+
}
49+
50+
func GetCommentList(videoId int64) ([]Comment, error) {
51+
var commentList []Comment
52+
result := Db.Model(Comment{}).Where(map[string]interface{}{"video_id": videoId, "action_type": 1}).
53+
Order("created_at desc").
54+
Find(&commentList)
55+
if result.Error != nil {
56+
log.Println(result.Error)
57+
return commentList, errors.New("get comment list failed")
58+
}
59+
return commentList, nil
60+
}
61+
62+
func GetCommentCnt(videoId int64) (int64, error) {
63+
var count int64
64+
result := Db.Model(Comment{}).Where(map[string]interface{}{"video_id": videoId, "action_type": 1}).
65+
Count(&count)
66+
if result.Error != nil {
67+
return 0, errors.New("find comments count failed")
68+
}
69+
return count, nil
70+
}

Diff for: dao/commentDao_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dao
2+
3+
import (
4+
"log"
5+
"testing"
6+
)
7+
8+
func TestInsertComment(t *testing.T) {
9+
var comment Comment = Comment{
10+
UserId: 5,
11+
VideoId: 14,
12+
Content: "这条评论来自单元测试TestInsertComment",
13+
}
14+
commentRes, err := InsertComment(comment)
15+
if err != nil {
16+
log.Println(err)
17+
}
18+
log.Println("返回的评论是", commentRes)
19+
}
20+
21+
func TestDeleteComment(t *testing.T) {
22+
err := DeleteComment(1)
23+
if err == nil {
24+
log.Println("delete comment success")
25+
}
26+
}
27+
28+
func TestGetCommentList(t *testing.T) {
29+
commentList, err := GetCommentList(21)
30+
if err == nil {
31+
log.Println(commentList)
32+
}
33+
}
34+
35+
func TestGetCommentCnt(t *testing.T) {
36+
count, err := GetCommentCnt(21)
37+
if err == nil {
38+
log.Println(count)
39+
}
40+
}

0 commit comments

Comments
 (0)