-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
339 lines (311 loc) · 9.95 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"context"
"fmt"
"log"
relation "github.com/ClubWeGo/relationmicro/kitex_gen/relation"
"github.com/ClubWeGo/relationmicro/service"
)
// 响应码
const (
// 服务器异常
ERROR = 0
// 正常响应
SUCCESS = 1
// 参数校验不通过
VERIFY = 2
)
// 关注操作类型
const (
// 关注
FOLLOW = 1
// 取关
UNFOLLOW = 2
)
// CombineServiceImpl implements the last service interface defined in the IDL.
type CombineServiceImpl struct{}
// GetFollowerListMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetFollowerListMethod(ctx context.Context, request *relation.GetFollowerListReq) (resp *relation.GetFollowerListResp, err error) {
myId := request.MyId
targetId := request.TargetId
// 参数校验
if verifyMsg := VerifyFindFollowParam(myId, targetId); verifyMsg != nil {
return &relation.GetFollowerListResp{
StatusCode: VERIFY,
UserList: []*relation.User{},
Msg: verifyMsg,
}, nil
}
// myId 为空 isFollow全为false 无影响
followerList, err := service.FindFollowerList(*myId, targetId)
if err != nil {
return &relation.GetFollowerListResp{
StatusCode: ERROR,
UserList: []*relation.User{},
}, err
}
// 封装响应
respUserList := make([]*relation.User, len(followerList))
for i, followerUser := range followerList {
fmt.Println("&", followerUser.Name)
respUserList[i] = &relation.User{
Id: followerUser.Id,
Name: followerUser.Name,
FollowCount: followerUser.FollowCount,
FollowerCount: followerUser.FollowerCount,
IsFollow: followerUser.IsFollow,
Avatar: followerUser.Avatar,
BackgroundImage: followerUser.BackgroundImage,
Signature: followerUser.Signature,
TotalFavorited: followerUser.TotalFavorited,
WorkCount: followerUser.WorkCount,
FavoriteCount: followerUser.FavoriteCount,
}
}
return &relation.GetFollowerListResp{
StatusCode: SUCCESS,
UserList: respUserList,
}, nil
}
// GetAllMessageMethod implements the MessageServiceImpl interface.
func (s *CombineServiceImpl) GetAllMessageMethod(ctx context.Context, request *relation.GetAllMessageReq) (resp *relation.GetAllMessageResp, err error) {
// TODO: Your code here...
// service层拿数据
msgs, err := service.GetAllP2PMsg(request.UserId, request.ToUserId)
if err != nil {
return &relation.GetAllMessageResp{
Status: false,
Msg: []*relation.Message{}, //返回空消息
}, nil
}
respMsg := make([]*relation.Message, len(msgs))
for index, msg := range msgs {
createTimeString := msg.Create_at.Format("2006-01-02")
respMsg[index] = &relation.Message{
Id: msg.Id,
FromUserId: msg.UserId,
ToUserId: msg.ToUserId,
Content: msg.Content,
CreateTime: &createTimeString,
}
}
return &relation.GetAllMessageResp{
Status: true,
Msg: respMsg,
}, nil
}
// SendMessageMethod implements the MessageServiceImpl interface.
func (s *CombineServiceImpl) SendMessageMethod(ctx context.Context, request *relation.SendMessageReq) (resp *relation.SendMessageResp, err error) {
// TODO: Your code here...
// service层拿数据
_, err = service.SendP2PMsg(request.UserId, request.ToUserId, request.Content)
if err != nil {
return &relation.SendMessageResp{
Status: false,
}, err
}
return &relation.SendMessageResp{
Status: true,
}, nil
}
// FollowMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) FollowMethod(ctx context.Context, request *relation.FollowReq) (resp *relation.FollowResp, err error) {
// 校验请求参数
if verifyMsg := VerifyFollowParam(request.MyUid, request.TargetUid); verifyMsg != nil {
resp = &relation.FollowResp{
StatusCode: VERIFY,
Msg: verifyMsg,
}
}
// 关注类型
actionType := request.ActionType
resp = &relation.FollowResp{
StatusCode: SUCCESS,
}
var errMsg error
if actionType == FOLLOW {
// 关注
errMsg = service.Follow(request.MyUid, request.TargetUid)
} else if actionType == UNFOLLOW {
// 取关
errMsg = service.UnFollow(request.MyUid, request.TargetUid)
} else {
// 其他情况算正常操作 反正对数据无影响,或者上游直接禁掉
return resp, nil
}
if errMsg != nil {
resp.StatusCode = ERROR
log.Printf("FollowMethod err:%s", err)
}
return resp, nil
}
// GetFollowInfoMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetFollowInfoMethod(ctx context.Context, req *relation.GetFollowInfoReq) (resp *relation.GetFollowInfoResp, err error) {
myUid := req.MyUid
targetUid := req.TargetUid
// 校验请求参数
if verifyMsg := VerifyFindFollowParam(myUid, targetUid); verifyMsg != nil {
return &relation.GetFollowInfoResp{
StatusCode: VERIFY,
Msg: verifyMsg,
}, nil
}
// 如果请求端没有携带用户信息 默认返回未关注
isFollow := false
if myUid != nil {
isFollow = service.FindIsFollow(*myUid, targetUid)
}
return &relation.GetFollowInfoResp{
StatusCode: SUCCESS,
FollowInfo: &relation.FollowInfo{
FollowCount: service.FindFollowCount(targetUid),
FollowerCount: service.FindFollowerCount(targetUid),
IsFollow: isFollow,
},
}, nil
}
// 校验关注的非法请求参数
func VerifyFollowParam(myUid int64, targetUid int64) *string {
var errMsg *string = nil
if myUid == targetUid {
*errMsg = "two uid the same, not allow!"
}
return errMsg
}
// 校验查询关注信息的非法请求参数
func VerifyFindFollowParam(myUid *int64, targetUid int64) *string {
return nil
}
// GetFriendListMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetFriendListMethod(ctx context.Context, request *relation.GetFriendListReq) (resp *relation.GetFriendListResp, err error) {
log.Println("GetFriendListMethod start")
myId := request.MyUid
targetId := request.TargetUid
// 参数校验
if verifyMsg := VerifyFindFollowParam(myId, targetId); verifyMsg != nil {
return &relation.GetFriendListResp{
StatusCode: VERIFY,
FriendList: []*relation.User{},
Msg: verifyMsg,
}, nil
}
// myId 为空 isFollow全为false 无影响
followerList, err := service.FindFollowerList(*myId, targetId)
log.Println("GetFriendListMethod service 请求完成")
if err != nil {
return &relation.GetFriendListResp{
StatusCode: ERROR,
FriendList: []*relation.User{},
}, err
}
// 封装响应
respUserList := make([]*relation.User, len(followerList))
for i, followerUser := range followerList {
fmt.Println("&", followerUser.Name)
respUserList[i] = &relation.User{
Id: followerUser.Id,
Name: followerUser.Name,
FollowCount: followerUser.FollowCount,
FollowerCount: followerUser.FollowerCount,
IsFollow: followerUser.IsFollow,
Avatar: followerUser.Avatar,
BackgroundImage: followerUser.BackgroundImage,
Signature: followerUser.Signature,
TotalFavorited: followerUser.TotalFavorited,
WorkCount: followerUser.WorkCount,
FavoriteCount: followerUser.FavoriteCount,
}
}
log.Println("GetFriendListMethod 响应封装end")
return &relation.GetFriendListResp{
StatusCode: SUCCESS,
FriendList: respUserList,
}, nil
return
}
// GetFollowListMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetFollowListMethod(ctx context.Context, request *relation.GetFollowListReq) (resp *relation.GetFollowListResp, err error) {
fmt.Println("GetFollowListMethod start")
myId := request.MyId
targetId := request.TargetId
// 参数校验
if verifyMsg := VerifyFindFollowParam(myId, targetId); verifyMsg != nil {
return &relation.GetFollowListResp{
StatusCode: VERIFY,
UserList: []*relation.User{},
Msg: verifyMsg,
}, nil
}
// myId 为空 isFollow全为false 无影响
followList, err := service.FindFollowList(*myId, targetId)
if err != nil {
return &relation.GetFollowListResp{
StatusCode: ERROR,
UserList: []*relation.User{},
}, nil
}
// 封装响应
respUserList := make([]*relation.User, len(followList))
for i, followUser := range followList {
fmt.Println(followUser)
respUserList[i] = &relation.User{
Id: followUser.Id,
Name: followUser.Name,
FollowCount: followUser.FollowCount,
FollowerCount: followUser.FollowerCount,
IsFollow: followUser.IsFollow,
Avatar: followUser.Avatar,
BackgroundImage: followUser.BackgroundImage,
Signature: followUser.Signature,
TotalFavorited: followUser.TotalFavorited,
WorkCount: followUser.WorkCount,
FavoriteCount: followUser.FavoriteCount,
}
}
fmt.Println("GetFollowListMethod end")
return &relation.GetFollowListResp{
StatusCode: SUCCESS,
UserList: respUserList,
}, err
}
// GetIsFollowsMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetIsFollowsMethod(ctx context.Context, request *relation.GetIsFollowsReq) (resp *relation.GetIsFollowsResp, err error) {
isFollows, err := service.FindIsFollows(request.GetMyUid(), request.GetUserIds())
if err != nil {
return &relation.GetIsFollowsResp{
StatusCode: ERROR,
IsFollowMap: nil,
}, nil
}
isFollowMap := make(map[int64]bool)
for userId, isFollow := range isFollows {
if isFollow == 1 {
isFollowMap[userId] = true
} else {
isFollowMap[userId] = false
}
}
return &relation.GetIsFollowsResp{
StatusCode: SUCCESS,
IsFollowMap: isFollowMap,
}, nil
}
// 根据userIds 获取关注相关信息
// GetFollowInfosMethod implements the RelationServiceImpl interface.
func (s *CombineServiceImpl) GetFollowInfosMethod(ctx context.Context, request *relation.GetFollowInfosReq) (resp *relation.GetFollowInfosResp, err error) {
myUid := request.GetMyUid()
userIds := request.GetUserIds()
// todo 参数校验
followInfos, err := service.FindFollowInfos(myUid, userIds)
// 请求异常
if err != nil {
return &relation.GetFollowInfosResp{
StatusCode: ERROR,
FollowInfoList: nil,
}, err
}
return &relation.GetFollowInfosResp{
StatusCode: SUCCESS,
FollowInfoList: followInfos,
}, nil
}