Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
withchao 2 years ago
commit f3e37650c6

@ -14,7 +14,7 @@ func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.Ge
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
blacks, total, err := s.BlackInterface.FindOwnerBlacks(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) blacks, total, err := s.BlackDatabase.FindOwnerBlacks(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -28,7 +28,7 @@ func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.Ge
func (s *friendServer) IsBlack(ctx context.Context, req *pbFriend.IsBlackReq) (*pbFriend.IsBlackResp, error) { func (s *friendServer) IsBlack(ctx context.Context, req *pbFriend.IsBlackReq) (*pbFriend.IsBlackResp, error) {
resp := &pbFriend.IsBlackResp{} resp := &pbFriend.IsBlackResp{}
in1, in2, err := s.BlackInterface.CheckIn(ctx, req.UserID1, req.UserID2) in1, in2, err := s.BlackDatabase.CheckIn(ctx, req.UserID1, req.UserID2)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -42,7 +42,7 @@ func (s *friendServer) RemoveBlack(ctx context.Context, req *pbFriend.RemoveBlac
if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil { if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil {
return nil, err return nil, err
} }
if err := s.BlackInterface.Delete(ctx, []*relation.BlackModel{{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID}}); err != nil { if err := s.BlackDatabase.Delete(ctx, []*relation.BlackModel{{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID}}); err != nil {
return nil, err return nil, err
} }
s.notification.BlackDeletedNotification(ctx, req) s.notification.BlackDeletedNotification(ctx, req)
@ -55,7 +55,7 @@ func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq)
return nil, err return nil, err
} }
black := relation.BlackModel{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID, OperatorUserID: tracelog.GetOpUserID(ctx)} black := relation.BlackModel{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID, OperatorUserID: tracelog.GetOpUserID(ctx)}
if err := s.BlackInterface.Create(ctx, []*relation.BlackModel{&black}); err != nil { if err := s.BlackDatabase.Create(ctx, []*relation.BlackModel{&black}); err != nil {
return nil, err return nil, err
} }
s.notification.BlackAddedNotification(ctx, req) s.notification.BlackAddedNotification(ctx, req)

@ -20,8 +20,8 @@ import (
) )
type friendServer struct { type friendServer struct {
controller.FriendInterface controller.FriendDatabase
controller.BlackInterface controller.BlackDatabase
notification *notification.Check notification *notification.Check
userCheck *check.UserCheck userCheck *check.UserCheck
RegisterCenter registry.SvcDiscoveryRegistry RegisterCenter registry.SvcDiscoveryRegistry
@ -36,11 +36,11 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
return err return err
} }
pbfriend.RegisterFriendServer(server, &friendServer{ pbfriend.RegisterFriendServer(server, &friendServer{
FriendInterface: controller.NewFriendController(controller.NewFriendDatabase(relation.NewFriendGorm(db), relation.NewFriendRequestGorm(db), tx.NewGorm(db))), FriendDatabase: controller.NewFriendDatabase(relation.NewFriendGorm(db), relation.NewFriendRequestGorm(db), tx.NewGorm(db)),
BlackInterface: controller.NewBlackController(controller.NewBlackDatabase(relation.NewBlackGorm(db))), BlackDatabase: controller.NewBlackDatabase(relation.NewBlackGorm(db)),
notification: notification.NewCheck(client), notification: notification.NewCheck(client),
userCheck: check.NewUserCheck(client), userCheck: check.NewUserCheck(client),
RegisterCenter: client, RegisterCenter: client,
}) })
return nil return nil
} }
@ -60,14 +60,14 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply
if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil { if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil {
return nil, err return nil, err
} }
in1, in2, err := s.FriendInterface.CheckIn(ctx, req.FromUserID, req.ToUserID) in1, in2, err := s.FriendDatabase.CheckIn(ctx, req.FromUserID, req.ToUserID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if in1 && in2 { if in1 && in2 {
return nil, constant.ErrRelationshipAlready.Wrap() return nil, constant.ErrRelationshipAlready.Wrap()
} }
if err = s.FriendInterface.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil { if err = s.FriendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
return nil, err return nil, err
} }
s.notification.FriendApplicationAddNotification(ctx, req) s.notification.FriendApplicationAddNotification(ctx, req)
@ -91,7 +91,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr
return nil, constant.ErrArgs.Wrap("friend userID repeated") return nil, constant.ErrArgs.Wrap("friend userID repeated")
} }
if err := s.FriendInterface.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport, tracelog.GetOpUserID(ctx)); err != nil { if err := s.FriendDatabase.BecomeFriends(ctx, req.OwnerUserID, req.FriendUserIDs, constant.BecomeFriendByImport, tracelog.GetOpUserID(ctx)); err != nil {
return nil, err return nil, err
} }
return resp, nil return resp, nil
@ -133,7 +133,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := s.FriendInterface.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil { if err := s.FriendDatabase.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil {
return nil, err return nil, err
} }
s.notification.FriendDeletedNotification(ctx, req) s.notification.FriendDeletedNotification(ctx, req)
@ -150,7 +150,7 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := s.FriendInterface.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil { if err := s.FriendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
return nil, err return nil, err
} }
s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
@ -165,7 +165,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G
if utils.Duplicate(req.FriendUserIDs) { if utils.Duplicate(req.FriendUserIDs) {
return nil, constant.ErrArgs.Wrap("friend userID repeated") return nil, constant.ErrArgs.Wrap("friend userID repeated")
} }
friends, err := s.FriendInterface.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs) friends, err := s.FriendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,7 +182,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friendRequests, total, err := s.FriendInterface.PageFriendRequestToMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -200,7 +200,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friendRequests, total, err := s.FriendInterface.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -215,7 +215,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
// ok // ok
func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) (resp *pbfriend.IsFriendResp, err error) { func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) (resp *pbfriend.IsFriendResp, err error) {
resp = &pbfriend.IsFriendResp{} resp = &pbfriend.IsFriendResp{}
resp.InUser1Friends, resp.InUser2Friends, err = s.FriendInterface.CheckIn(ctx, req.UserID1, req.UserID2) resp.InUser1Friends, resp.InUser2Friends, err = s.FriendDatabase.CheckIn(ctx, req.UserID1, req.UserID2)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -228,7 +228,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
if err := s.userCheck.Access(ctx, req.UserID); err != nil { if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }
friends, total, err := s.FriendInterface.PageOwnerFriends(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber) friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -21,7 +21,7 @@ import (
) )
type userServer struct { type userServer struct {
controller.UserInterface controller.UserDatabase
notification *notification.Check notification *notification.Check
userCheck *check.UserCheck userCheck *check.UserCheck
ConversationChecker *check.ConversationChecker ConversationChecker *check.ConversationChecker
@ -38,7 +38,7 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
return err return err
} }
u := &userServer{ u := &userServer{
UserInterface: controller.NewUserController(controller.NewUserDatabase(relation.NewUserGorm(gormDB))), UserDatabase: controller.NewUserDatabase(relation.NewUserGorm(gormDB)),
notification: notification.NewCheck(client), notification: notification.NewCheck(client),
userCheck: check.NewUserCheck(client), userCheck: check.NewUserCheck(client),
RegisterCenter: client, RegisterCenter: client,
@ -51,7 +51,7 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
for k, v := range config.Config.Manager.AppManagerUid { for k, v := range config.Config.Manager.AppManagerUid {
users = append(users, &tablerelation.UserModel{UserID: v, Nickname: config.Config.Manager.Nickname[k]}) users = append(users, &tablerelation.UserModel{UserID: v, Nickname: config.Config.Manager.Nickname[k]})
} }
u.UserInterface.InitOnce(context.Background(), users) u.UserDatabase.InitOnce(context.Background(), users)
return nil return nil
} }

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type BlackInterface interface { type BlackDatabase interface {
// Create 增加黑名单 // Create 增加黑名单
Create(ctx context.Context, blacks []*relation.BlackModel) (err error) Create(ctx context.Context, blacks []*relation.BlackModel) (err error)
// Delete 删除黑名单 // Delete 删除黑名单
@ -19,70 +19,31 @@ type BlackInterface interface {
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
} }
type BlackController struct { type blackDatabase struct {
database BlackDatabaseInterface
}
func NewBlackController(database BlackDatabaseInterface) BlackInterface {
return &BlackController{database: database}
}
// Create 增加黑名单
func (b *BlackController) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.database.Create(ctx, blacks)
}
// Delete 删除黑名单
func (b *BlackController) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.database.Delete(ctx, blacks)
}
// FindOwnerBlacks 获取黑名单列表
func (b *BlackController) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blackList []*relation.BlackModel, total int64, err error) {
return b.database.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
}
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
func (b *BlackController) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
return b.database.CheckIn(ctx, userID1, userID2)
}
type BlackDatabaseInterface interface {
// Create 增加黑名单
Create(ctx context.Context, blacks []*relation.BlackModel) (err error)
// Delete 删除黑名单
Delete(ctx context.Context, blacks []*relation.BlackModel) (err error)
// FindOwnerBlacks 获取黑名单列表
FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error)
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error)
}
type BlackDatabase struct {
black relation.BlackModelInterface black relation.BlackModelInterface
} }
func NewBlackDatabase(black relation.BlackModelInterface) *BlackDatabase { func NewBlackDatabase(black relation.BlackModelInterface) BlackDatabase {
return &BlackDatabase{black} return &blackDatabase{black}
} }
// Create 增加黑名单 // Create 增加黑名单
func (b *BlackDatabase) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) { func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.black.Create(ctx, blacks) return b.black.Create(ctx, blacks)
} }
// Delete 删除黑名单 // Delete 删除黑名单
func (b *BlackDatabase) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) { func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
return b.black.Delete(ctx, blacks) return b.black.Delete(ctx, blacks)
} }
// FindOwnerBlacks 获取黑名单列表 // FindOwnerBlacks 获取黑名单列表
func (b *BlackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error) { func (b *blackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (blacks []*relation.BlackModel, total int64, err error) {
return b.black.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber) return b.black.FindOwnerBlacks(ctx, ownerUserID, pageNumber, showNumber)
} }
// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true) // CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true)
func (b *BlackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) { func (b *blackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) {
_, err = b.black.Take(ctx, userID1, userID2) _, err = b.black.Take(ctx, userID1, userID2)
if err != nil { if err != nil {
if errors.Unwrap(err) != gorm.ErrRecordNotFound { if errors.Unwrap(err) != gorm.ErrRecordNotFound {

@ -10,102 +10,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type FriendInterface interface { type FriendDatabase interface {
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
// 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
// (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可
BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error)
// 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝
RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
// 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 不是好友则建立双向好友关系 4修改申请记录 已同意
AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
// 删除好友 外部判断是否好友关系
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
// 更新好友备注 零值也支持
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
// 获取ownerUserID的好友列表 无结果不返回错误
PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// friendUserID在哪些人的好友列表中
PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
// 获取我发出去的好友申请 无结果不返回错误
PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取我收到的的好友申请 无结果不返回错误
PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
// 获取某人指定好友的信息 如果有一个不存在也返回错误
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
}
type FriendController struct {
database FriendDatabaseInterface
}
func NewFriendController(database FriendDatabaseInterface) FriendInterface {
return &FriendController{database: database}
}
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
func (f *FriendController) CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
return f.database.CheckIn(ctx, user1, user2)
}
// AddFriendRequest 增加或者更新好友申请
func (f *FriendController) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
return f.database.AddFriendRequest(ctx, fromUserID, toUserID, reqMsg, ex)
}
// BecomeFriend 先判断是否在好友表,如果在则不插入
func (f *FriendController) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error) {
return f.database.BecomeFriends(ctx, ownerUserID, friendUserIDs, addSource, OperatorUserID)
}
// RefuseFriendRequest 拒绝好友申请
func (f *FriendController) RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return f.database.RefuseFriendRequest(ctx, friendRequest)
}
// AgreeFriendRequest 同意好友申请
func (f *FriendController) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return f.database.AgreeFriendRequest(ctx, friendRequest)
}
// Delete 删除好友
func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
return f.database.Delete(ctx, ownerUserID, friendUserIDs)
}
// UpdateRemark 更新好友备注
func (f *FriendController) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
return f.database.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
}
// FindOwnerFriends 获取ownerUserID的好友列表
func (f *FriendController) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.PageOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
}
// FindInWhoseFriends friendUserID在哪些人的好友列表中
func (f *FriendController) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.database.PageInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
}
// FindFriendRequestFromMe 获取我发出去的好友申请
func (f *FriendController) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.PageFriendRequestFromMe(ctx, userID, pageNumber, showNumber)
}
// FindFriendRequestToMe 获取我收到的的好友申请
func (f *FriendController) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.database.PageFriendRequestToMe(ctx, userID, pageNumber, showNumber)
}
// FindFriends 获取某人指定好友的信息
func (f *FriendController) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
return f.database.FindFriendsWithError(ctx, ownerUserID, friendUserIDs)
}
type FriendDatabaseInterface interface {
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true) // 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error) CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
// 增加或者更新好友申请 // 增加或者更新好友申请
@ -132,18 +37,18 @@ type FriendDatabaseInterface interface {
FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
} }
type FriendDatabase struct { type friendDatabase struct {
friend relation.FriendModelInterface friend relation.FriendModelInterface
friendRequest relation.FriendRequestModelInterface friendRequest relation.FriendRequestModelInterface
tx tx.Tx tx tx.Tx
} }
func NewFriendDatabase(friend relation.FriendModelInterface, friendRequest relation.FriendRequestModelInterface, tx tx.Tx) *FriendDatabase { func NewFriendDatabase(friend relation.FriendModelInterface, friendRequest relation.FriendRequestModelInterface, tx tx.Tx) FriendDatabase {
return &FriendDatabase{friend: friend, friendRequest: friendRequest, tx: tx} return &friendDatabase{friend: friend, friendRequest: friendRequest, tx: tx}
} }
// ok 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true) // ok 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) { func (f *friendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
friends, err := f.friend.FindUserState(ctx, userID1, userID2) friends, err := f.friend.FindUserState(ctx, userID1, userID2)
if err != nil { if err != nil {
return false, false, err return false, false, err
@ -160,7 +65,7 @@ func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (
} }
// 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增 // 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增
func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) { func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
return f.tx.Transaction(func(tx any) error { return f.tx.Transaction(func(tx any) error {
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID) _, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
//有db错误 //有db错误
@ -188,7 +93,7 @@ func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
} }
// (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可 // (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可
func (f *FriendDatabase) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error) { func (f *friendDatabase) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error) {
return f.tx.Transaction(func(tx any) error { return f.tx.Transaction(func(tx any) error {
//先find 找出重复的 去掉重复的 //先find 找出重复的 去掉重复的
fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs) fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs)
@ -226,7 +131,7 @@ func (f *FriendDatabase) BecomeFriends(ctx context.Context, ownerUserID string,
} }
// 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝 // 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝
func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) { func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
_, err = f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID) _, err = f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
if err != nil { if err != nil {
return err return err
@ -240,7 +145,7 @@ func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest
} }
// 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 不是好友则建立双向好友关系 4修改申请记录 已同意 // 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 不是好友则建立双向好友关系 4修改申请记录 已同意
func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) { func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return f.tx.Transaction(func(tx any) error { return f.tx.Transaction(func(tx any) error {
_, err = f.friendRequest.NewTx(tx).Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID) _, err = f.friendRequest.NewTx(tx).Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
if err != nil { if err != nil {
@ -293,37 +198,37 @@ func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *
} }
// 删除好友 外部判断是否好友关系 // 删除好友 外部判断是否好友关系
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) { func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
return f.friend.Delete(ctx, ownerUserID, friendUserIDs) return f.friend.Delete(ctx, ownerUserID, friendUserIDs)
} }
// 更新好友备注 零值也支持 // 更新好友备注 零值也支持
func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
return f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark) return f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
} }
// 获取ownerUserID的好友列表 无结果不返回错误 // 获取ownerUserID的好友列表 无结果不返回错误
func (f *FriendDatabase) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) { func (f *friendDatabase) PageOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber) return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
} }
// friendUserID在哪些人的好友列表中 // friendUserID在哪些人的好友列表中
func (f *FriendDatabase) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) { func (f *friendDatabase) PageInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber) return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
} }
// 获取我发出去的好友申请 无结果不返回错误 // 获取我发出去的好友申请 无结果不返回错误
func (f *FriendDatabase) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) { func (f *friendDatabase) PageFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber) return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber)
} }
// 获取我收到的的好友申请 无结果不返回错误 // 获取我收到的的好友申请 无结果不返回错误
func (f *FriendDatabase) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) { func (f *friendDatabase) PageFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber) return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber)
} }
// 获取某人指定好友的信息 如果有好友不存在,也返回错误 // 获取某人指定好友的信息 如果有好友不存在,也返回错误
func (f *FriendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) { func (f *friendDatabase) FindFriendsWithError(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs) friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
if err != nil { if err != nil {
return return

@ -7,7 +7,7 @@ import (
"context" "context"
) )
type UserInterface interface { type UserDatabase interface {
//获取指定用户的信息 如有userID未找到 也返回错误 //获取指定用户的信息 如有userID未找到 也返回错误
FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error)
//获取指定用户的信息 如有userID未找到 不返回错误 //获取指定用户的信息 如有userID未找到 不返回错误
@ -28,79 +28,15 @@ type UserInterface interface {
InitOnce(ctx context.Context, users []*relation.UserModel) (err error) InitOnce(ctx context.Context, users []*relation.UserModel) (err error)
} }
type UserController struct { type userDatabase struct {
database UserDatabaseInterface
}
// 获取指定用户的信息 如有userID未找到 也返回错误
func (u *UserController) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
return u.database.FindWithError(ctx, userIDs)
}
func (u *UserController) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
return u.database.Find(ctx, userIDs)
}
func (u *UserController) Create(ctx context.Context, users []*relation.UserModel) error {
return u.database.Create(ctx, users)
}
func (u *UserController) Update(ctx context.Context, users []*relation.UserModel) (err error) {
return u.database.Update(ctx, users)
}
func (u *UserController) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
return u.database.UpdateByMap(ctx, userID, args)
}
func (u *UserController) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) {
return u.database.Page(ctx, pageNumber, showNumber)
}
func (u *UserController) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
return u.database.IsExist(ctx, userIDs)
}
func (u *UserController) GetAllUserID(ctx context.Context) ([]string, error) {
return u.database.GetAllUserID(ctx)
}
func (u *UserController) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) {
return u.database.InitOnce(ctx, users)
}
func NewUserController(database UserDatabaseInterface) UserInterface {
return &UserController{database}
}
type UserDatabaseInterface interface {
//获取指定用户的信息 如有userID未找到 也返回错误
FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error)
//获取指定用户的信息 如有userID未找到 不返回错误
Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error)
//插入多条 外部保证userID 不重复 且在db中不存在
Create(ctx context.Context, users []*relation.UserModel) (err error)
//更新(非零值) 外部保证userID存在
Update(ctx context.Context, users []*relation.UserModel) (err error)
//更新(零值) 外部保证userID存在
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
//如果没找到,不返回错误
Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error)
//只要有一个存在就为true
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
//获取所有用户ID
GetAllUserID(ctx context.Context) ([]string, error)
//函数内部先查询db中是否存在存在则什么都不做不存在则插入
InitOnce(ctx context.Context, users []*relation.UserModel) (err error)
}
type UserDatabase struct {
userDB relation.UserModelInterface userDB relation.UserModelInterface
} }
func NewUserDatabase(userDB relation.UserModelInterface) *UserDatabase { func NewUserDatabase(userDB relation.UserModelInterface) UserDatabase {
return &UserDatabase{userDB: userDB} return &userDatabase{userDB: userDB}
} }
func (u *UserDatabase) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) { func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) {
userIDs := utils.Slice(users, func(e *relation.UserModel) string { userIDs := utils.Slice(users, func(e *relation.UserModel) string {
return e.UserID return e.UserID
}) })
@ -108,14 +44,16 @@ func (u *UserDatabase) InitOnce(ctx context.Context, users []*relation.UserModel
if err != nil { if err != nil {
return err return err
} }
miss := utils.SliceAnySub(users, result, func(e *relation.UserModel) string { return e.UserID })
if len(miss) > 0 {
u.userDB.Create(ctx, miss)
}
miss := utils.SliceSubAny(users, result, func(e *relation.UserModel) string { return e.UserID }) return nil
u.userDB.Create(ctx, miss)
} }
// 获取指定用户的信息 如有userID未找到 也返回错误 // 获取指定用户的信息 如有userID未找到 也返回错误
func (u *UserDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
users, err = u.userDB.Find(ctx, userIDs) users, err = u.userDB.Find(ctx, userIDs)
if err != nil { if err != nil {
@ -128,33 +66,33 @@ func (u *UserDatabase) FindWithError(ctx context.Context, userIDs []string) (use
} }
// 获取指定用户的信息 如有userID未找到 不返回错误 // 获取指定用户的信息 如有userID未找到 不返回错误
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
users, err = u.userDB.Find(ctx, userIDs) users, err = u.userDB.Find(ctx, userIDs)
return return
} }
// 插入多条 外部保证userID 不重复 且在db中不存在 // 插入多条 外部保证userID 不重复 且在db中不存在
func (u *UserDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) { func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) {
return u.userDB.Create(ctx, users) return u.userDB.Create(ctx, users)
} }
// 更新(非零值) 外部保证userID存在 // 更新(非零值) 外部保证userID存在
func (u *UserDatabase) Update(ctx context.Context, users []*relation.UserModel) (err error) { func (u *userDatabase) Update(ctx context.Context, users []*relation.UserModel) (err error) {
return u.userDB.Update(ctx, users) return u.userDB.Update(ctx, users)
} }
// 更新(零值) 外部保证userID存在 // 更新(零值) 外部保证userID存在
func (u *UserDatabase) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) { func (u *userDatabase) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
return u.userDB.UpdateByMap(ctx, userID, args) return u.userDB.UpdateByMap(ctx, userID, args)
} }
// 获取,如果没找到,不返回错误 // 获取,如果没找到,不返回错误
func (u *UserDatabase) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) { func (u *userDatabase) Page(ctx context.Context, pageNumber, showNumber int32) (users []*relation.UserModel, count int64, err error) {
return u.userDB.Page(ctx, pageNumber, showNumber) return u.userDB.Page(ctx, pageNumber, showNumber)
} }
// userIDs是否存在 只要有一个存在就为true // userIDs是否存在 只要有一个存在就为true
func (u *UserDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) { func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
users, err := u.userDB.Find(ctx, userIDs) users, err := u.userDB.Find(ctx, userIDs)
if err != nil { if err != nil {
return false, err return false, err
@ -165,7 +103,7 @@ func (u *UserDatabase) IsExist(ctx context.Context, userIDs []string) (exist boo
return false, nil return false, nil
} }
func (u *UserDatabase) GetAllUserID(ctx context.Context) (userIDs []string, err error) { func (u *userDatabase) GetAllUserID(ctx context.Context) (userIDs []string, err error) {
pageNumber := int32(0) pageNumber := int32(0)
for { for {
tmp, total, err := u.userDB.PageUserID(ctx, pageNumber, constant.ShowNumber) tmp, total, err := u.userDB.PageUserID(ctx, pageNumber, constant.ShowNumber)

Loading…
Cancel
Save