From 80bde864c16b339e4554327f6fcf570c6e58030d Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Wed, 1 Feb 2023 16:19:44 +0800 Subject: [PATCH] friend update --- pkg/common/db/controller/friend.go | 3 +- pkg/common/db/relation/friend_model_k.go | 35 +++++++++--------------- pkg/common/db/table/table.go | 13 +++++++++ 3 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 pkg/common/db/table/table.go diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index d4c498b65..47d763777 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -2,6 +2,7 @@ package controller import ( "Open_IM/pkg/common/db/relation" + "Open_IM/pkg/common/db/table" "context" "gorm.io/gorm" ) @@ -95,7 +96,7 @@ type FriendDatabaseInterface interface { // AddFriendRequest 增加或者更新好友申请 AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) // BecomeFriend 先判断是否在好友表,如果在则不插入 - BecomeFriend(ctx context.Context, friends []*relation.Friend) (err error) + BecomeFriend(ctx context.Context, friends []*table.FriendModel) (err error) // RefuseFriendRequest 拒绝好友申请 RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequest) (err error) // AgreeFriendRequest 同意好友申请 diff --git a/pkg/common/db/relation/friend_model_k.go b/pkg/common/db/relation/friend_model_k.go index 5355dcc9b..7b78de93d 100644 --- a/pkg/common/db/relation/friend_model_k.go +++ b/pkg/common/db/relation/friend_model_k.go @@ -1,31 +1,22 @@ package relation import ( + "Open_IM/pkg/common/db/table" "Open_IM/pkg/common/tracelog" "Open_IM/pkg/utils" "context" "gorm.io/gorm" - "time" ) type FriendDB interface { - Create(ctx context.Context, friends []*FriendModel) (err error) + Create(ctx context.Context, friends []*table.FriendModel) (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 []*FriendModel) (err error) + Update(ctx context.Context, friends []*table.FriendModel) (err error) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) - FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error) + FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*table.FriendModel, err error) } -type FriendModel struct { - OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"` - FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"` - Remark string `gorm:"column:remark;size:255"` - CreateTime time.Time `gorm:"column:create_time"` - AddSource int32 `gorm:"column:add_source"` - OperatorUserID string `gorm:"column:operator_user_id;size:64"` - Ex string `gorm:"column:ex;size:1024"` -} type FriendGorm struct { DB *gorm.DB `gorm:"-"` } @@ -39,7 +30,7 @@ type FriendUser struct { Nickname string `gorm:"column:name;size:255"` } -func (f *FriendGorm) Create(ctx context.Context, friends []*FriendModel) (err error) { +func (f *FriendGorm) Create(ctx context.Context, friends []*table.FriendModel) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends) }() @@ -50,7 +41,7 @@ func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserI defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs) }() - err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&FriendModel{}).Error, "") + err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&table.FriendModel{}).Error, "") return err } @@ -61,7 +52,7 @@ func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args m return utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "") } -func (f *FriendGorm) Update(ctx context.Context, friends []*FriendModel) (err error) { +func (f *FriendGorm) Update(ctx context.Context, friends []*table.FriendModel) (err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends) }() @@ -72,30 +63,30 @@ func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark) }() - return utils.Wrap(f.DB.Model(&FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "") + return utils.Wrap(f.DB.Model(&table.FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "") } -func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error) { +func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*table.FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends) }() return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "") } -func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*FriendModel, err error) { +func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*table.FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends) }() return friends, utils.Wrap(f.DB.Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "") } -func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendModel, err error) { - friend = &FriendModel{} +func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *table.FriendModel, err error) { + friend = &table.FriendModel{} defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend) return friend, utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "") } -func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*FriendModel, err error) { +func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*table.FriendModel, err error) { defer func() { tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "userID1", userID1, "userID2", userID2) }() diff --git a/pkg/common/db/table/table.go b/pkg/common/db/table/table.go new file mode 100644 index 000000000..78b3eec49 --- /dev/null +++ b/pkg/common/db/table/table.go @@ -0,0 +1,13 @@ +package table + +import "time" + +type FriendModel struct { + OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"` + FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"` + Remark string `gorm:"column:remark;size:255"` + CreateTime time.Time `gorm:"column:create_time"` + AddSource int32 `gorm:"column:add_source"` + OperatorUserID string `gorm:"column:operator_user_id;size:64"` + Ex string `gorm:"column:ex;size:1024"` +}