-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhandler.go
275 lines (236 loc) · 7.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"context"
"google.golang.org/protobuf/proto"
"toktik/constant/biz"
"toktik/constant/config"
"toktik/kitex_gen/douyin/favorite"
"toktik/kitex_gen/douyin/feed"
feedService "toktik/kitex_gen/douyin/feed/feedservice"
"toktik/logging"
gen "toktik/repo"
"toktik/repo/model"
"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
consul "github.com/kitex-contrib/registry-consul"
"github.com/sirupsen/logrus"
)
var FeedClient feedService.Client
func init() {
r, err := consul.NewConsulResolver(config.EnvConfig.CONSUL_ADDR)
if err != nil {
panic(err)
}
FeedClient, err = feedService.NewClient(
config.FeedServiceName,
client.WithResolver(r),
client.WithSuite(tracing.NewClientSuite()),
)
if err != nil {
panic(err)
}
}
// FavoriteServiceImpl implements the last service interface defined in the IDL.
type FavoriteServiceImpl struct{}
var logger = logging.Logger
func like(ctx context.Context, actorId uint32, videoId uint32) (resp *favorite.FavoriteResponse, err error) {
if err = gen.Q.Favorite.WithContext(ctx).Create(&model.Favorite{
UserId: actorId,
VideoId: videoId,
}); err != nil {
resp = &favorite.FavoriteResponse{
StatusCode: biz.UnableToLike,
StatusMsg: proto.String("点赞失败"),
}
logger.WithFields(logrus.Fields{
"method": "like",
}).Errorf("Failed to like: %v", err)
return
}
resp = &favorite.FavoriteResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
}
return
}
func cancelLike(ctx context.Context, actorId uint32, videoId uint32) (resp *favorite.FavoriteResponse, err error) {
if _, err = gen.Q.Favorite.WithContext(ctx).
Unscoped().
Where(gen.Favorite.UserId.Eq(actorId), gen.Favorite.VideoId.Eq(videoId)).
Delete(); err != nil {
resp = &favorite.FavoriteResponse{
StatusCode: biz.UnableToCancelLike,
StatusMsg: proto.String("取消点赞失败"),
}
logger.WithFields(logrus.Fields{
"method": "cancelLike",
}).Errorf("Failed to cancel like: %v", err)
return
}
resp = &favorite.FavoriteResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
}
return
}
// FavoriteAction implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteAction(ctx context.Context, req *favorite.FavoriteRequest) (resp *favorite.FavoriteResponse, err error) {
field := logrus.Fields{
"method": "FavoriteAction",
}
logger.WithFields(field).Info("process start")
v := gen.Video
var authorId uint32
err = v.WithContext(ctx).Where(v.ID.Eq(req.VideoId)).Pluck(v.UserId, &authorId)
if err != nil {
resp = &favorite.FavoriteResponse{
StatusCode: biz.UnableToQueryVideo,
StatusMsg: proto.String("视频作者不存在"),
}
logger.WithFields(field).Error("Failed to get video author")
return
}
if req.ActionType == 1 {
resp, err = like(ctx, req.ActorId, req.VideoId)
} else {
resp, err = cancelLike(ctx, req.ActorId, req.VideoId)
}
return
}
// FavoriteList implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteList(ctx context.Context, req *favorite.FavoriteListRequest) (resp *favorite.FavoriteListResponse, err error) {
field := logrus.Fields{
"method": "FavoriteList",
}
logger.WithFields(field).Info("process start")
f := gen.Favorite
var videoIds []uint32
err = f.WithContext(ctx).Where(f.UserId.Eq(req.UserId)).Pluck(f.VideoId, &videoIds)
if err != nil {
resp = &favorite.FavoriteListResponse{
StatusCode: biz.FailedToGetVideoList,
StatusMsg: &biz.InternalServerErrorStatusMsg,
VideoList: nil,
}
logger.WithFields(field).
Errorf("Failed to get the id of the user's favorite videos: %v", err)
return
}
queryVideoResp, err := FeedClient.QueryVideos(ctx, &feed.QueryVideosRequest{
ActorId: req.ActorId,
VideoIds: videoIds,
})
if err != nil {
resp = &favorite.FavoriteListResponse{
StatusCode: biz.FailedToGetVideoList,
StatusMsg: &biz.InternalServerErrorStatusMsg,
VideoList: nil,
}
logger.WithFields(field).
Errorf("Failed to get the user's favorite videos: %v", err)
return
}
resp = &favorite.FavoriteListResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
VideoList: queryVideoResp.VideoList,
}
return
}
// IsFavorite implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) IsFavorite(ctx context.Context, req *favorite.IsFavoriteRequest) (resp *favorite.IsFavoriteResponse, err error) {
field := logrus.Fields{
"method": "IsFavorite",
}
logger.WithFields(field).Info("process start")
f := gen.Favorite
_, err = f.WithContext(ctx).Where(f.UserId.Eq(req.UserId), f.VideoId.Eq(req.VideoId)).First()
if err != nil {
return &favorite.IsFavoriteResponse{Result: false}, nil
}
return &favorite.IsFavoriteResponse{Result: true}, nil
}
// CountFavorite implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) CountFavorite(ctx context.Context, req *favorite.CountFavoriteRequest) (resp *favorite.CountFavoriteResponse, err error) {
field := logrus.Fields{
"method": "CountFavorite",
}
logger.WithFields(field).Info("process start")
count, err := gen.Q.Favorite.WithContext(ctx).
Where(gen.Q.Favorite.VideoId.Eq(req.VideoId)).
Count()
if err != nil {
logger.WithFields(field).
Errorf("Failed to get the number of favorites: %v", err)
return &favorite.CountFavoriteResponse{
StatusCode: biz.SQLQueryErrorStatusCode,
StatusMsg: &biz.InternalServerErrorStatusMsg,
}, nil
}
return &favorite.CountFavoriteResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
Count: uint32(count),
}, nil
}
// CountUserFavorite implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) CountUserFavorite(ctx context.Context, req *favorite.CountUserFavoriteRequest) (resp *favorite.CountUserFavoriteResponse, err error) {
field := logrus.Fields{
"method": "CountUserFavorite",
}
logger.WithFields(field).Info("process start")
count, err := gen.Q.Favorite.WithContext(ctx).
Where(gen.Q.Favorite.UserId.Eq(req.UserId)).
Count()
if err != nil {
logger.WithFields(field).
Errorf("Failed to get the number of favorites: %v", err)
return &favorite.CountUserFavoriteResponse{
StatusCode: biz.SQLQueryErrorStatusCode,
StatusMsg: &biz.InternalServerErrorStatusMsg,
}, nil
}
return &favorite.CountUserFavoriteResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
Count: uint32(count),
}, nil
}
// CountUserTotalFavorited implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) CountUserTotalFavorited(ctx context.Context, req *favorite.CountUserTotalFavoritedRequest) (resp *favorite.CountUserTotalFavoritedResponse, err error) {
field := logrus.Fields{
"method": "CountUserTotalFavorited",
}
logger.WithFields(field).Info("process start")
videos, err := gen.Q.Video.WithContext(ctx).
Where(gen.Q.Video.UserId.Eq(req.UserId)).
Find()
if err != nil {
logger.WithFields(field).
Errorf("Failed to get the number of favorites: %v", err)
return &favorite.CountUserTotalFavoritedResponse{
StatusCode: biz.SQLQueryErrorStatusCode,
StatusMsg: &biz.InternalServerErrorStatusMsg,
}, nil
}
videoIds := make([]uint32, 0, len(videos))
for _, video := range videos {
videoIds = append(videoIds, video.ID)
}
count, err := gen.Q.Favorite.WithContext(ctx).
Where(gen.Q.Favorite.VideoId.In(videoIds...)).
Count()
if err != nil {
logger.WithFields(field).
Errorf("Failed to get the number of favorites: %v", err)
return &favorite.CountUserTotalFavoritedResponse{
StatusCode: biz.SQLQueryErrorStatusCode,
StatusMsg: &biz.InternalServerErrorStatusMsg,
}, nil
}
return &favorite.CountUserTotalFavoritedResponse{
StatusCode: biz.OkStatusCode,
StatusMsg: &biz.OkStatusMsg,
Count: uint32(count),
}, nil
}