-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
129 lines (120 loc) · 3.61 KB
/
handler.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
package service
import (
"context"
"fmt"
"strings"
favorite "github.com/alph00/tiktok-tiny/kitex_gen/favorite"
"github.com/alph00/tiktok-tiny/model"
)
// FavoriteServiceImpl implements the last service interface defined in the IDL.
type FavoriteServiceImpl struct{}
// FavoriteAction implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteAction(ctx context.Context, req *favorite.FavoriteActionRequest) (resp *favorite.FavoriteActionResponse, err error) {
UserId := req.UserId
VideoID := req.VideoId
actType := req.ActionType
fmt.Println("调用favorite微服务handler下的FavoriteAction函数")
if actType == 1 {
fmt.Println("acttype=1,来创建点赞数据")
err := model.CreateVideoFavorite(ctx, &model.FavoriteVideoRelation{
UserID: uint(UserId),
VideoID: uint(VideoID),
})
if err != nil {
res := &favorite.FavoriteActionResponse{
StatusCode: -1,
StatusMsg: "向数据库创建点赞视频数据failure",
}
return res, nil
}
} else {
fmt.Println("acttype=0,来取消点赞数据")
err := model.DelVideoFavorite(ctx, &model.FavoriteVideoRelation{
UserID: uint(UserId),
VideoID: uint(VideoID),
})
if err != nil {
res := &favorite.FavoriteActionResponse{
StatusCode: -1,
StatusMsg: "向数据库删除点赞视频数据failure",
}
return res, nil
}
// res := &favorite.FavoriteActionResponse{
// StatusCode: -1,
// StatusMsg: "取消点赞failure",
// }
// return res, nil
}
res := &favorite.FavoriteActionResponse{
StatusCode: 0,
StatusMsg: "success",
}
return res, nil
// return
}
// FavoriteList implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteList(ctx context.Context, req *favorite.FavoriteListRequest) (resp *favorite.FavoriteListResponse, err error) {
// TODO: Your code here...
// return
// 这里的设置是req为
// UserId: int64(v.(*model.User).ID),
// Token: token,
// UserId := req.UserId
// // VideoID := req.VideoId
fmt.Println("调用favorite微服务下的FavoriteList函数")
videos, err := model.ShowVideoFavorite(ctx, uint(req.UserId))
if err != nil {
res := &favorite.FavoriteListResponse{
StatusCode: -1,
StatusMsg: "数据库查询点赞视频数据failure",
VideoList: nil,
}
return res, nil
}
// if actType == 1 {
// err := model.CreateVideoFavorite(ctx, &model.FavoriteVideoRelation{
// UserID: uint(UserId),
// VideoID: uint(VideoID),
// })
// if err != nil {
// res := &favorite.FavoriteActionResponse{
// StatusCode: -1,
// StatusMsg: "向数据库创建点赞视频数据failure",
// }
// return res, nil
// }
// } else {
// err := model.DelVideoFavorite(ctx, &model.FavoriteVideoRelation{
// UserID: uint(UserId),
// VideoID: uint(VideoID),
// })
// if err != nil {
// res := &favorite.FavoriteActionResponse{
// StatusCode: -1,
// StatusMsg: "向数据库删除点赞视频数据failure",
// }
// return res, nil
// }
// // res := &favorite.FavoriteActionResponse{
// // StatusCode: -1,
// // StatusMsg: "取消点赞failure",
// // }
// // return res, nil
// }
// 使用一个切片来存储转换后的字符串
videoStrings := make([]string, len(videos))
// 遍历切片中的每个 int 指针,并将其值转换为字符串
for i, video := range videos {
videoStrings[i] = fmt.Sprintf("%d", video)
}
// 使用逗号连接字符串切片中的元素
output := "[" + strings.Join(videoStrings, ", ") + "]"
res := &favorite.FavoriteListResponse{
StatusCode: 0,
StatusMsg: "success" + output,
VideoList: nil,
}
return res, nil
// return
}