/
relationservice_grpc.go
250 lines (214 loc) · 6.88 KB
/
relationservice_grpc.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
package service
import (
"context"
"errors"
"time"
"github.com/go-eagle/eagle/pkg/errcode"
"github.com/google/wire"
pb "github.com/go-microservice/relation-service/api/relation/v1"
"github.com/go-microservice/relation-service/internal/ecode"
"github.com/go-microservice/relation-service/internal/model"
repo "github.com/go-microservice/relation-service/internal/repository"
)
const (
// FollowStatusNormal 关注状态-正常
FollowStatusNormal int = 1 // 正常
// FollowStatusDelete 关注状态-删除
FollowStatusDelete = 0 // 删除
)
var (
_ pb.RelationServiceServer = (*RelationServiceServer)(nil)
)
// ProviderSet is service providers.
var ProviderSet = wire.NewSet(NewRelationServiceServer)
type RelationServiceServer struct {
pb.UnimplementedRelationServiceServer
followerRepo repo.UserFollowerRepo
followingRepo repo.UserFollowingRepo
}
func NewRelationServiceServer(followerRepo repo.UserFollowerRepo, followingRepo repo.UserFollowingRepo) *RelationServiceServer {
return &RelationServiceServer{
followerRepo: followerRepo,
followingRepo: followingRepo,
}
}
// Follow user
func (s *RelationServiceServer) Follow(ctx context.Context, req *pb.FollowRequest) (*pb.FollowReply, error) {
// if is follow self
if isSelf(req.GetUserId(), req.GetFollowedUid()) {
return nil, ecode.ErrInvalidArgument.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": errors.New("can not follow yourself"),
})).Status(req).Err()
}
// check if has followed
following, err := s.followingRepo.GetUserFollowing(ctx, req.UserId, req.FollowedUid)
if err != nil {
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
// has follow
if following != nil && following.Status == FollowStatusNormal {
return &pb.FollowReply{}, nil
}
db := model.GetDB()
tx := db.Begin()
if tx.Error != nil {
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": tx.Error.Error(),
})).Status(req).Err()
}
curTime := time.Now()
// 添加到关注表
_, err = s.followingRepo.CreateUserFollowing(ctx, tx, &model.UserFollowingModel{
UserID: req.UserId,
FollowedUID: req.FollowedUid,
Status: FollowStatusNormal,
CreatedAt: curTime,
UpdatedAt: curTime,
})
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
// 添加到粉丝表
_, err = s.followerRepo.CreateUserFollower(ctx, tx, &model.UserFollowerModel{
UserID: req.FollowedUid,
FollowerUID: req.UserId,
Status: FollowStatusNormal,
CreatedAt: curTime,
UpdatedAt: curTime,
})
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
// 增加关注数
// 增加粉丝数
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
return &pb.FollowReply{}, nil
}
// Unfollow
func (s *RelationServiceServer) Unfollow(ctx context.Context, req *pb.UnfollowRequest) (*pb.UnfollowReply, error) {
// cannot unfollow self
if isSelf(req.GetUserId(), req.GetFollowedUid()) {
return nil, ecode.ErrInvalidArgument.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": errors.New("cannot unfollow self"),
})).Status(req).Err()
}
// 已取关
following, err := s.followingRepo.GetUserFollowingWithoutCache(ctx, req.UserId, req.FollowedUid)
if err != nil {
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
if following != nil && following.Status == FollowStatusDelete {
return &pb.UnfollowReply{}, nil
}
// 如果是已关注,执行取关逻辑
db := model.GetDB()
tx := db.Begin()
if tx.Error != nil {
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": tx.Error.Error(),
})).Status(req).Err()
}
// 删除关注
err = s.followingRepo.UpdateUserFollowingStatus(ctx, tx, req.UserId, req.FollowedUid, FollowStatusDelete)
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
// 删除粉丝
err = s.followerRepo.UpdateUserFollowerStatus(ctx, tx, req.FollowedUid, req.UserId, FollowStatusDelete)
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
// 减少关注数
// 减少粉丝数
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
return &pb.UnfollowReply{}, nil
}
func isSelf(UId, otherUId int64) bool {
return UId == otherUId
}
func (s *RelationServiceServer) BatchGetRelation(ctx context.Context, req *pb.BatchGetRelationRequest) (*pb.BatchGetRelationReply, error) {
if req.GetUserId() == 0 || len(req.GetIds()) == 0 {
return nil, ecode.ErrInvalidArgument.WithDetails().Status(req).Err()
}
ret, err := s.followingRepo.BatchGetUserFollowing(ctx, req.GetUserId(), req.GetIds())
if err != nil {
return nil, ecode.ErrInternalError.WithDetails(errcode.NewDetails(map[string]interface{}{
"msg": err.Error(),
})).Status(req).Err()
}
retMap := make(map[int64]int64)
for _, v := range ret {
retMap[v.FollowedUID] = int64(v.Status)
}
return &pb.BatchGetRelationReply{
Result: retMap,
}, nil
}
func (s *RelationServiceServer) GetFollowingList(ctx context.Context, req *pb.FollowingListRequest) (*pb.FollowingListReply, error) {
if req.GetLastId() == 0 {
req.LastId = MaxID
}
userFollowList, err := s.followingRepo.GetFollowingUserList(ctx, req.UserId, req.LastId, int(req.Limit))
if err != nil {
return nil, err
}
var data []*pb.FollowingListReplyUserFollow
for _, v := range userFollowList {
item := pb.FollowingListReplyUserFollow{
Id: v.ID,
FollowedUid: v.FollowedUID,
}
data = append(data, &item)
}
return &pb.FollowingListReply{
Result: data,
}, nil
}
func (s *RelationServiceServer) GetFollowerList(ctx context.Context, req *pb.FollowerListRequest) (*pb.FollowerListReply, error) {
if req.GetLastId() == 0 {
req.LastId = MaxID
}
userFollowList, err := s.followerRepo.GetFollowerUserList(ctx, req.UserId, req.LastId, int(req.Limit))
if err != nil {
return nil, err
}
var data []*pb.FollowerListReplyFollower
for _, v := range userFollowList {
item := pb.FollowerListReplyFollower{
Id: v.ID,
FollowerUid: v.FollowerUID,
}
data = append(data, &item)
}
return &pb.FollowerListReply{
Result: data,
}, nil
}