-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
152 lines (140 loc) · 5.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"github.com/ClubWeGo/favoritemicro/cmd/rpc"
"github.com/ClubWeGo/favoritemicro/dal/rdb"
favorite "github.com/ClubWeGo/favoritemicro/kitex_gen/favorite"
"github.com/ClubWeGo/favoritemicro/pack"
"github.com/ClubWeGo/favoritemicro/pkg/errno"
"log"
)
// FavoriteServiceImpl implements the last service interface defined in the IDL.
type FavoriteServiceImpl struct{}
// FavoriteMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteMethod(ctx context.Context, request *favorite.FavoriteReq) (resp *favorite.FavoriteResp, err error) {
resp = &favorite.FavoriteResp{}
if request.ActionType != 1 && request.ActionType != 2 {
resp.BaseResp = pack.BuildBaseResp(errno.ParamErr)
return resp, nil
}
// 获取作者id
var authorId int64
authorId, _ = rdb.GetAuthorId(ctx, request.VideoId)
// 如果缓存中没有作者id,就去rpc获取
if authorId == 0 {
authorId, err = rpc.GetAuthorId(ctx, request.VideoId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(errno.RPCErr.WithMessage(err.Error()))
return resp, nil
}
// 将作者id存入缓存
rdb.SetAuthorId(ctx, request.VideoId, authorId)
}
if request.ActionType == 1 {
err = rdb.AddFavorite(ctx, request.UserId, request.VideoId, authorId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
log.Println("点赞成功")
resp.BaseResp = pack.BuildBaseResp(errno.Success.WithMessage("点赞成功"))
return resp, nil
} else {
err = rdb.DeleteFavorite(ctx, request.UserId, request.VideoId, authorId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success.WithMessage("取消点赞"))
return resp, nil
}
}
// FavoriteListMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteListMethod(ctx context.Context, request *favorite.FavoriteListReq) (resp *favorite.FavoriteListResp, err error) {
resp = &favorite.FavoriteListResp{}
favoriteList, err := rdb.GetFavoriteList(ctx, request.UserId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(errno.DBErr.WithMessage(err.Error()))
return resp, nil
}
//videoIdList := pack.Favorites(favoriteList)
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.VideoIdList = favoriteList
return resp, nil
}
// FavoriteRelationMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteRelationMethod(ctx context.Context, request *favorite.FavoriteRelationReq) (resp *favorite.FavoriteRelationResp, err error) {
resp = &favorite.FavoriteRelationResp{}
res, err := rdb.GetFavoriteRelation(ctx, request.UserId, request.VideoId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
resp.IsFavorite = false
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success.WithMessage("已点赞"))
resp.IsFavorite = res
return resp, nil
}
// UserFavoriteCountMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) UserFavoriteCountMethod(ctx context.Context, request *favorite.UserFavoriteCountReq) (resp *favorite.UserFavoriteCountResp, err error) {
resp = &favorite.UserFavoriteCountResp{}
favoriteCnt, favoritedCnt, err := rdb.CountUserFavorite(ctx, request.UserId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(errno.DBErr.WithMessage(err.Error()))
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.FavoriteCount = favoriteCnt
resp.FavoritedCount = favoritedCnt
return resp, nil
}
// VideoFavoriteCountMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) VideoFavoriteCountMethod(ctx context.Context, request *favorite.VideoFavoriteCountReq) (resp *favorite.VideoFavoriteCountResp, err error) {
resp = &favorite.VideoFavoriteCountResp{}
cnt, err := rdb.CountVideoFavorite(ctx, request.VideoId)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.FavoriteCount = cnt
return resp, nil
}
// VideosFavoriteCountMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) VideosFavoriteCountMethod(ctx context.Context,
request *favorite.VideosFavoriteCountReq) (resp *favorite.VideosFavoriteCountResp, err error) {
resp = &favorite.VideosFavoriteCountResp{}
favoriteCountMap, err := rdb.CountVideosFavorite(ctx, request.VideoIdList)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.FavoriteCountMap = favoriteCountMap
return
}
// UsersFavoriteCountMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) UsersFavoriteCountMethod(ctx context.Context,
request *favorite.UsersFavoriteCountReq) (resp *favorite.UsersFavoriteCountResp, err error) {
resp = &favorite.UsersFavoriteCountResp{}
favoriteMap, err := rdb.CountUsersFavorite(ctx, request.UserIdList)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.FavoriteCountMap = favoriteMap
return
}
// FavoriteRelationsMethod implements the FavoriteServiceImpl interface.
func (s *FavoriteServiceImpl) FavoriteRelationsMethod(ctx context.Context, request *favorite.FavoriteRelationsReq) (resp *favorite.FavoriteRelationsResp, err error) {
resp = &favorite.FavoriteRelationsResp{}
isFavoriteMap, err := rdb.GetFavoriteRelations(ctx, request.UserId, request.VideoIdList)
if err != nil {
resp.BaseResp = pack.BuildBaseResp(err)
return resp, nil
}
resp.BaseResp = pack.BuildBaseResp(errno.Success)
resp.IsFavoriteMap = isFavoriteMap
return resp, nil
}