diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 212a04039..9cfcf923c 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -4,10 +4,14 @@ import ( chat "Open_IM/internal/rpc/msg" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" + "Open_IM/pkg/common/db/controller" + + "Open_IM/pkg/common/db/relation" "Open_IM/pkg/common/log" "Open_IM/pkg/common/middleware" promePkg "Open_IM/pkg/common/prometheus" "Open_IM/pkg/common/token_verify" + "Open_IM/pkg/common/tools" "Open_IM/pkg/common/tracelog" "Open_IM/pkg/getcdv3" pbFriend "Open_IM/pkg/proto/friend" @@ -27,31 +31,42 @@ import ( ) type friendServer struct { - rpcPort int - rpcRegisterName string - etcdSchema string - etcdAddr []string - friendModel *controller.FriendModel - friendRequestModel *controller.FriendRequestModel - blackModel *controller.BlackModel + rpcPort int + rpcRegisterName string + etcdSchema string + etcdAddr []string + controller.FriendInterface + controller.FriendRequestInterface + controller.BlackInterface } func NewFriendServer(port int) *friendServer { log.NewPrivateLog(constant.LogFileName) - return &friendServer{ + f := friendServer{ rpcPort: port, rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName, etcdSchema: config.Config.Etcd.EtcdSchema, etcdAddr: config.Config.Etcd.EtcdAddr, } + //mysql init + var mysql relation.Mysql + var model relation.Friend + err := mysql.InitConn().AutoMigrateModel(&model) + if err != nil { + panic("db init err:" + err.Error()) + } + if mysql.GormConn() != nil { + model.DB = mysql.GormConn() + } else { + panic("db init err:" + "conn is nil") + } + f.FriendInterface = controller.NewFriendController(model.DB) + f.FriendRequestInterface = controller.NewFriendRequestController(model.DB) + f.BlackInterface = controller.NewBlackController(model.DB) + return &f } func (s *friendServer) Run() { - db := relation.ConnectToDB() - //s.friendModel = mysql.NewFriend(db) - //s.friendRequestModel = mysql.NewFriendRequest(db) - //s.blackModel = mysql.NewBlack(db) - log.NewInfo("0", "friendServer run...") listenIP := "" @@ -111,8 +126,8 @@ func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlackl if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - black := relation.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: utils.OpUserID(ctx)} - if err := s.blackModel.Create(ctx, []*relation.Black{&black}); err != nil { + black := relation.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)} + if err := s.BlackInterface.Create(ctx, []*relation.Black{&black}); err != nil { return nil, err } chat.BlackAddedNotification(req) @@ -127,11 +142,11 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq if err := callbackBeforeAddFriendV1(req); err != nil { return nil, err } - friends1, err := s.friendModel.FindOwnerUserID(ctx, req.ToUserID) + friends1, err := s.FriendInterface.FindOwnerUserID(ctx, req.ToUserID) if err != nil { return nil, err } - friends2, err := s.friendModel.FindOwnerUserID(ctx, req.FromUserID) + friends2, err := s.FriendInterface.FindOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } @@ -159,7 +174,7 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq ReqMsg: req.ReqMsg, CreateTime: time.Now(), } - if err := s.friendRequestModel.Create(ctx, []*relation.FriendRequest{&friendRequest}); err != nil { + if err := s.FriendRequestInterface.Create(ctx, []*relation.FriendRequest{&friendRequest}); err != nil { return nil, err } chat.FriendApplicationNotification(req) @@ -181,7 +196,7 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri if _, err := GetUserInfo(ctx, userID); err != nil { return nil, err } - fs, err := s.friendModel.FindUserState(ctx, req.FromUserID, userID) + fs, err := s.FriendInterface.FindUserState(ctx, req.FromUserID, userID) if err != nil { return nil, err } @@ -199,7 +214,7 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri } } if len(friends) > 0 { - if err := s.friendModel.Create(ctx, friends); err != nil { + if err := s.FriendInterface.Create(ctx, friends); err != nil { return nil, err } } @@ -219,7 +234,7 @@ func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddF friendRequest.HandleResult = req.HandleResult friendRequest.HandleTime = time.Now() friendRequest.HandleMsg = req.HandleMsg - friendRequest.HandlerUserID = utils.OpUserID(ctx) + friendRequest.HandlerUserID = tools.OpUserID(ctx) err = relation.UpdateFriendApplication(friendRequest) if err != nil { return nil, err @@ -230,10 +245,10 @@ func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddF //Establish friendship after find friend relationship not exists _, err := s.friendModel.Take(ctx, req.FromUserID, req.ToUserID) if errors.Is(err, gorm.ErrRecordNotFound) { - if err := s.friendModel.Create(ctx, []*relation.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: utils.OpUserID(ctx)}}); err != nil { + if err := s.friendModel.Create(ctx, []*relation.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil { return nil, err } - chat.FriendAddedNotification(utils.OperationID(ctx), utils.OpUserID(ctx), req.FromUserID, req.ToUserID) + chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) } else if err != nil { return nil, err } @@ -254,7 +269,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFri if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - if err := s.friendModel.Delete(ctx, req.FromUserID, req.ToUserID); err != nil { + if err := s.FriendInterface.Delete(ctx, req.FromUserID, req.ToUserID); err != nil { return nil, err } chat.FriendDeletedNotification(req) @@ -266,7 +281,7 @@ func (s *friendServer) GetBlacklist(ctx context.Context, req *pbFriend.GetBlackl if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - blacks, err := s.blackModel.FindByOwnerUserID(ctx, req.FromUserID) + blacks, err := s.BlackInterface.FindByOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } @@ -286,10 +301,10 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFri if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - if err := s.friendModel.UpdateRemark(ctx, req.FromUserID, req.ToUserID, req.Remark); err != nil { + if err := s.FriendInterface.UpdateRemark(ctx, req.FromUserID, req.ToUserID, req.Remark); err != nil { return nil, err } - chat.FriendRemarkSetNotification(utils.OperationID(ctx), utils.OpUserID(ctx), req.FromUserID, req.ToUserID) + chat.FriendRemarkSetNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) return resp, nil } @@ -299,7 +314,7 @@ func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.Remove if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - if err := s.blackModel.Delete(ctx, []*relation.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { + if err := s.BlackInterface.Delete(ctx, []*relation.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { return nil, err } chat.BlackDeletedNotification(req) @@ -311,7 +326,7 @@ func (s *friendServer) IsInBlackList(ctx context.Context, req *pbFriend.IsInBlac if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - exist, err := s.blackModel.IsExist(ctx, req.FromUserID, req.ToUserID) + exist, err := s.BlackInterface.IsExist(ctx, req.FromUserID, req.ToUserID) if err != nil { return nil, err } @@ -324,7 +339,7 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbFriend.IsFriendReq) if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - exist, err := s.friendModel.IsExist(ctx, req.FromUserID, req.ToUserID) + exist, err := s.FriendInterface.IsExist(ctx, req.FromUserID, req.ToUserID) if err != nil { return nil, err } @@ -337,7 +352,7 @@ func (s *friendServer) GetFriendList(ctx context.Context, req *pbFriend.GetFrien if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - friends, err := s.friendModel.FindOwnerUserID(ctx, req.FromUserID) + friends, err := s.FriendInterface.FindOwnerUserID(ctx, req.FromUserID) if err != nil { return nil, err } @@ -403,7 +418,7 @@ func (s *friendServer) GetSelfApplyList(ctx context.Context, req *pbFriend.GetSe return nil, err } // Find the self add other userinfo - friendRequests, err := s.friendRequestModel.FindFromUserID(ctx, req.FromUserID) + friendRequests, err := s.FriendRequestInterface.FindFromUserID(ctx, req.FromUserID) if err != nil { return nil, err } diff --git a/pkg/common/db/controller/black.go b/pkg/common/db/controller/black.go index 50b5ca4ee..aa84a8582 100644 --- a/pkg/common/db/controller/black.go +++ b/pkg/common/db/controller/black.go @@ -1,52 +1,90 @@ package controller import ( - "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/relation" "context" - "errors" "gorm.io/gorm" ) -type BlackModel struct { - db *relation.Black - cache *cache.GroupCache +type BlackInterface interface { + Create(ctx context.Context, blacks []*relation.Black) (err error) + Delete(ctx context.Context, blacks []*relation.Black) (err error) + UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, blacks []*relation.Black) (err error) + Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) + Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) + FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) } -func (b *BlackModel) Create(ctx context.Context, blacks []*relation.Black) (err error) { - return b.db.Create(ctx, blacks) +type BlackController struct { + database BlackDatabaseInterface } -func (b *BlackModel) Delete(ctx context.Context, blacks []*relation.Black) (err error) { - return b.db.Delete(ctx, blacks) +func NewBlackController(db *gorm.DB) *BlackController { + return &BlackController{database: NewBlackDatabase(db)} +} +func (f *BlackController) Create(ctx context.Context, blacks []*relation.Black) (err error) { + return f.database.Create(ctx, blacks) +} +func (f *BlackController) Delete(ctx context.Context, blacks []*relation.Black) (err error) { + return f.database.Delete(ctx, blacks) +} +func (f *BlackController) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) { + return f.database.UpdateByMap(ctx, ownerUserID, blockUserID, args) +} +func (f *BlackController) Update(ctx context.Context, blacks []*relation.Black) (err error) { + return f.database.Update(ctx, blacks) +} +func (f *BlackController) Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) { + return f.database.Find(ctx, blacks) +} +func (f *BlackController) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) { + return f.database.Take(ctx, ownerUserID, blockUserID) +} +func (f *BlackController) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) { + return f.database.FindByOwnerUserID(ctx, ownerUserID) } -func (b *BlackModel) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) { - return b.db.UpdateByMap(ctx, ownerUserID, blockUserID, args) +type BlackDatabaseInterface interface { + Create(ctx context.Context, blacks []*relation.Black) (err error) + Delete(ctx context.Context, blacks []*relation.Black) (err error) + UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, blacks []*relation.Black) (err error) + Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) + Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) + FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) } -func (b *BlackModel) Update(ctx context.Context, blacks []*relation.Black) (err error) { - return b.db.Update(ctx, blacks) +type BlackDatabase struct { + sqlDB *relation.Black } -func (b *BlackModel) Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) { - return b.db.Find(ctx, blacks) -} - -func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) { - return b.db.Take(ctx, ownerUserID, blockUserID) -} - -func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) { - return b.db.FindByOwnerUserID(ctx, ownerUserID) -} - -func (b *BlackModel) IsExist(ctx context.Context, ownerUserID, blockUserID string) (bool, error) { - if _, err := b.Take(ctx, ownerUserID, blockUserID); err == nil { - return true, nil - } else if errors.Is(err, gorm.ErrRecordNotFound) { - return false, nil - } else { - return false, err +func NewBlackDatabase(db *gorm.DB) *BlackDatabase { + sqlDB := relation.NewBlack(db) + database := &BlackDatabase{ + sqlDB: sqlDB, } + return database +} + +func (f *BlackDatabase) Create(ctx context.Context, blacks []*relation.Black) (err error) { + return f.sqlDB.Create(ctx, blacks) +} +func (f *BlackDatabase) Delete(ctx context.Context, blacks []*relation.Black) (err error) { + return f.sqlDB.Delete(ctx, blacks) +} +func (f *BlackDatabase) UpdateByMap(ctx context.Context, ownerUserID, blockUserID string, args map[string]interface{}) (err error) { + return f.sqlDB.UpdateByMap(ctx, ownerUserID, blockUserID, args) +} +func (f *BlackDatabase) Update(ctx context.Context, blacks []*relation.Black) (err error) { + return f.sqlDB.Update(ctx, blacks) +} +func (f *BlackDatabase) Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) { + return f.sqlDB.Find(ctx, blacks) +} +func (f *BlackDatabase) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) { + return f.sqlDB.Take(ctx, ownerUserID, blockUserID) +} +func (f *BlackDatabase) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) { + return f.sqlDB.FindByOwnerUserID(ctx, ownerUserID) } diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index e59c71b5a..c047f9421 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -1,60 +1,107 @@ package controller import ( - "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/relation" "context" - "errors" "gorm.io/gorm" ) -type FriendModel struct { - db *relation.Friend - cache *cache.GroupCache +type FriendInterface interface { + Create(ctx context.Context, friends []*relation.Friend) (err error) + Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) + UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, friends []*relation.Friend) (err error) + UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) + FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) + FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) + Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) + FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) } -func (f *FriendModel) Create(ctx context.Context, friends []*relation.Friend) (err error) { - return f.db.Create(ctx, friends) +type FriendController struct { + database FriendDatabaseInterface } -func (f *FriendModel) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { - return f.db.Delete(ctx, ownerUserID, friendUserIDs) +func NewFriendController(db *gorm.DB) *FriendController { + return &FriendController{database: NewFriendDatabase(db)} } -func (f *FriendModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { - return f.db.UpdateByMap(ctx, ownerUserID, args) +func (f *FriendController) Create(ctx context.Context, friends []*relation.Friend) (err error) { + return f.database.Create(ctx, friends) +} +func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { + return f.database.Delete(ctx, ownerUserID, friendUserIDs) +} +func (f *FriendController) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.database.UpdateByMap(ctx, ownerUserID, args) +} +func (f *FriendController) Update(ctx context.Context, friends []*relation.Friend) (err error) { + return f.database.Update(ctx, friends) +} +func (f *FriendController) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { + return f.database.UpdateRemark(ctx, ownerUserID, friendUserID, remark) +} +func (f *FriendController) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) { + return f.database.FindOwnerUserID(ctx, ownerUserID) +} +func (f *FriendController) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) { + return f.database.FindFriendUserID(ctx, friendUserID) +} +func (f *FriendController) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) { + return f.database.Take(ctx, ownerUserID, friendUserID) +} +func (f *FriendController) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) { + return f.database.FindUserState(ctx, userID1, userID2) } -func (f *FriendModel) Update(ctx context.Context, friends []*relation.Friend) (err error) { - return f.db.Update(ctx, friends) +type FriendDatabaseInterface interface { + Create(ctx context.Context, friends []*relation.Friend) (err error) + Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) + UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, friends []*relation.Friend) (err error) + UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) + FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) + FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) + Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) + FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) } -func (f *FriendModel) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { - return f.db.UpdateRemark(ctx, ownerUserID, friendUserID, remark) +type FriendDatabase struct { + sqlDB *relation.Friend } -func (f *FriendModel) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) { - return f.db.FindOwnerUserID(ctx, ownerUserID) -} - -func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) { - return f.db.FindFriendUserID(ctx, friendUserID) -} - -func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) { - return f.db.Take(ctx, ownerUserID, friendUserID) -} - -func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) { - return f.db.FindUserState(ctx, userID1, userID2) -} - -func (f *FriendModel) IsExist(ctx context.Context, ownerUserID, friendUserID string) (bool, error) { - if _, err := f.Take(ctx, ownerUserID, friendUserID); err == nil { - return true, nil - } else if errors.Is(err, gorm.ErrRecordNotFound) { - return false, nil - } else { - return false, err +func NewFriendDatabase(db *gorm.DB) *FriendDatabase { + sqlDB := relation.NewFriendDB(db) + database := &FriendDatabase{ + sqlDB: sqlDB, } + return database +} + +func (f *FriendDatabase) Create(ctx context.Context, friends []*relation.Friend) (err error) { + return f.sqlDB.Create(ctx, friends) +} +func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) { + return f.sqlDB.Delete(ctx, ownerUserID, friendUserIDs) +} +func (f *FriendDatabase) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.sqlDB.UpdateByMap(ctx, ownerUserID, args) +} +func (f *FriendDatabase) Update(ctx context.Context, friends []*relation.Friend) (err error) { + return f.sqlDB.Update(ctx, friends) +} +func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) { + return f.sqlDB.UpdateRemark(ctx, ownerUserID, friendUserID, remark) +} +func (f *FriendDatabase) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) { + return f.sqlDB.FindOwnerUserID(ctx, ownerUserID) +} +func (f *FriendDatabase) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) { + return f.sqlDB.FindFriendUserID(ctx, friendUserID) +} +func (f *FriendDatabase) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) { + return f.sqlDB.Take(ctx, ownerUserID, friendUserID) +} +func (f *FriendDatabase) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) { + return f.sqlDB.FindUserState(ctx, userID1, userID2) } diff --git a/pkg/common/db/controller/friend_request.go b/pkg/common/db/controller/friend_request.go index e301bc92a..d4c2b3032 100644 --- a/pkg/common/db/controller/friend_request.go +++ b/pkg/common/db/controller/friend_request.go @@ -1,44 +1,110 @@ package controller import ( - "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/relation" "context" + "gorm.io/gorm" ) -type FriendRequestModel struct { - db *relation.FriendRequest - cache *cache.GroupCache +type FriendRequestInterface interface { + Create(ctx context.Context, friends []*relation.FriendRequest) (err error) + Delete(ctx context.Context, fromUserID, toUserID string) (err error) + UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, friends []*relation.FriendRequest) (err error) + Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) + Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) + FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) + FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) } -func (f *FriendRequestModel) Create(ctx context.Context, friends []*relation.FriendRequest) (err error) { - return f.db.Create(ctx, friends) +type FriendRequestController struct { + database FriendRequestInterface } -func (f *FriendRequestModel) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { - return f.db.Delete(ctx, fromUserID, toUserID) +func NewFriendRequestController(db *gorm.DB) *FriendRequestController { + return &FriendRequestController{database: NewFriendRequestDatabase(db)} } -func (f *FriendRequestModel) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { - return f.db.UpdateByMap(ctx, ownerUserID, args) +func (f *FriendRequestController) Create(ctx context.Context, friends []*relation.FriendRequest) (err error) { + return f.database.Create(ctx, friends) +} +func (f *FriendRequestController) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { + return f.database.Delete(ctx, fromUserID, toUserID) +} +func (f *FriendRequestController) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.database.UpdateByMap(ctx, ownerUserID, args) +} +func (f *FriendRequestController) Update(ctx context.Context, friends []*relation.FriendRequest) (err error) { + return f.database.Update(ctx, friends) +} +func (f *FriendRequestController) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) { + return f.database.Find(ctx, ownerUserID) +} +func (f *FriendRequestController) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) { + return f.database.Take(ctx, fromUserID, toUserID) +} +func (f *FriendRequestController) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) { + return f.database.FindToUserID(ctx, toUserID) +} +func (f *FriendRequestController) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) { + return f.database.FindFromUserID(ctx, fromUserID) } -func (f *FriendRequestModel) Update(ctx context.Context, friends []*relation.FriendRequest) (err error) { - return f.db.Update(ctx, friends) +type FriendRequestDatabaseInterface interface { + Create(ctx context.Context, friends []*relation.FriendRequest) (err error) + Delete(ctx context.Context, fromUserID, toUserID string) (err error) + UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) + Update(ctx context.Context, friends []*relation.FriendRequest) (err error) + Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) + Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) + FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) + FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) } -func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) { - return f.db.Find(ctx, ownerUserID) +type FriendRequestDatabase struct { + sqlDB *relation.FriendRequest + friend *FriendDatabase } -func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) { - return f.db.Take(ctx, fromUserID, toUserID) +func (f *FriendRequestDatabase) Update(ctx context.Context, friends []*relation.FriendRequest) (err error) { + return f.sqlDB.DB.Transaction(func(tx *gorm.DB) error { + if err := f.sqlDB.Update(ctx, friends); err != nil { + return err + } + if err := f.friend.Update(); err != nil { + return err + } + return nil + }) } -func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) { - return f.db.FindToUserID(ctx, toUserID) +func NewFriendRequestDatabase(db *gorm.DB) *FriendRequestDatabase { + sqlDB := relation.NewFriendRequest(db) + database := &FriendRequestDatabase{ + sqlDB: sqlDB, + } + return database } -func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) { - return f.db.FindFromUserID(ctx, fromUserID) +func (f *FriendRequestDatabase) Create(ctx context.Context, friends []*relation.FriendRequest) (err error) { + return f.sqlDB.Create(ctx, friends) +} +func (f *FriendRequestDatabase) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { + return f.sqlDB.Delete(ctx, fromUserID, toUserID) +} +func (f *FriendRequestDatabase) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) { + return f.sqlDB.UpdateByMap(ctx, ownerUserID, args) +} + +func (f *FriendRequestDatabase) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) { + return f.sqlDB.Find(ctx, ownerUserID) +} +func (f *FriendRequestDatabase) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) { + return f.sqlDB.Take(ctx, fromUserID, toUserID) +} +func (f *FriendRequestDatabase) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) { + return f.sqlDB.FindToUserID(ctx, toUserID) +} +func (f *FriendRequestDatabase) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) { + return f.sqlDB.FindFromUserID(ctx, fromUserID) } diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 1fcba9060..ec7b3a02e 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -45,7 +45,7 @@ func (u *UserController) GetByNameAndID(ctx context.Context, content string, sho func (u *UserController) Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation.User, count int64, err error) { return u.database.Get(ctx, showNumber, pageNumber) } -func NewUserController(db *gorm.DB) UserInterface { +func NewUserController(db *gorm.DB) *UserController { controller := &UserController{database: newUserDatabase(db)} return controller } @@ -58,14 +58,14 @@ type UserDatabaseInterface interface { UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation.User, count int64, err error) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation.User, count int64, err error) - Get(ctx context.Context, showNumber, pageNumber int32) (users []*User, count int64, err error) + Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation.User, count int64, err error) } type UserDatabase struct { sqlDB *relation.User } -func newUserDatabase(db *gorm.DB) UserDatabaseInterface { +func newUserDatabase(db *gorm.DB) *UserDatabase { sqlDB := relation.NewUserDB(db) database := &UserDatabase{ sqlDB: sqlDB, diff --git a/pkg/common/db/relation/black.go b/pkg/common/db/relation/black.go index 3f1faa32e..dded6ee97 100644 --- a/pkg/common/db/relation/black.go +++ b/pkg/common/db/relation/black.go @@ -20,7 +20,7 @@ type Black struct { func NewBlack(db *gorm.DB) *Black { var black Black - black.DB = initModel(db, &black) + black.DB = db return &black } diff --git a/pkg/common/db/relation/friend_model_k.go b/pkg/common/db/relation/friend_model_k.go index 26f1fd006..bca654a17 100644 --- a/pkg/common/db/relation/friend_model_k.go +++ b/pkg/common/db/relation/friend_model_k.go @@ -19,7 +19,7 @@ type Friend struct { DB *gorm.DB `gorm:"-"` } -func NewFriend(db *gorm.DB) *Friend { +func NewFriendDB(db *gorm.DB) *Friend { var friend Friend friend.DB = initModel(db, friend) return &friend diff --git a/pkg/common/db/relation/friend_request_model.go b/pkg/common/db/relation/friend_request_model.go index aae25a33a..eb9d69d97 100644 --- a/pkg/common/db/relation/friend_request_model.go +++ b/pkg/common/db/relation/friend_request_model.go @@ -25,7 +25,7 @@ type FriendRequest struct { func NewFriendRequest(db *gorm.DB) *FriendRequest { var fr FriendRequest - fr.DB = initModel(db, &fr) + fr.DB = db return &fr }