From ed65ad842f3d201ea968b367e5bf58454b16f277 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Mon, 28 Feb 2022 17:20:40 +0800 Subject: [PATCH] Replace find with take --- .../im_mysql_model/group_member_model.go | 7 +++++-- .../mysql_model/im_mysql_model/group_model.go | 19 ++++++++----------- .../im_mysql_model/group_request_model.go | 3 +-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go index 4dafa30a6..63f12c115 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go @@ -4,6 +4,7 @@ import ( "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" "Open_IM/pkg/utils" + "errors" "fmt" "time" ) @@ -43,6 +44,7 @@ func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) { } var groupMemberList []db.GroupMember err = dbConn.Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error + //err = dbConn.Table("group_members").Where("user_id=?", userID).Take(&groupMemberList).Error if err != nil { return nil, err } @@ -82,7 +84,7 @@ func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*db.GroupMemb return nil, err } var groupMember db.GroupMember - err = dbConn.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Find(&groupMember).Error + err = dbConn.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Take(&groupMember).Error if err != nil { return nil, err } @@ -149,7 +151,8 @@ func GetGroupOwnerInfoByGroupID(groupID string) (*db.GroupMember, error) { return &v, nil } } - return nil, nil + + return nil, utils.Wrap(errors.New("no owner"), "") } func IsExistGroupMember(groupID, userID string) bool { diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_model.go index a9fe13236..587e3672e 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_model.go @@ -45,7 +45,7 @@ func GetGroupInfoByGroupID(groupId string) (*db.Group, error) { return nil, utils.Wrap(err, "") } var groupInfo db.Group - err = dbConn.Table("groups").Where("group_id=?", groupId).Find(&groupInfo).Error + err = dbConn.Table("groups").Where("group_id=?", groupId).Take(&groupInfo).Error if err != nil { return nil, err } @@ -86,11 +86,10 @@ func GetGroups(pageNumber, showNumber int) ([]db.Group, error) { return groups, nil } - func OperateGroupStatus(groupId string, groupStatus int32) error { group := db.Group{ GroupID: groupId, - Status: groupStatus, + Status: groupStatus, } if err := SetGroupInfo(group); err != nil { return err @@ -98,7 +97,6 @@ func OperateGroupStatus(groupId string, groupStatus int32) error { return nil } - func DeleteGroup(groupId string) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { @@ -129,15 +127,14 @@ func OperateGroupRole(userId, groupId string, roleLevel int32) (string, string, updateInfo := db.GroupMember{ RoleLevel: roleLevel, } - groupMaster := db.GroupMember{ - } + groupMaster := db.GroupMember{} switch roleLevel { case constant.GroupOwner: err = dbConn.Transaction(func(tx *gorm.DB) error { result := dbConn.Table("group_members").Where("group_id = ? and role_level = ?", groupId, constant.GroupOwner).First(&groupMaster).Update(&db.GroupMember{ RoleLevel: constant.GroupOrdinaryUsers, }) - if result.Error != nil { + if result.Error != nil { return result.Error } if result.RowsAffected == 0 { @@ -145,7 +142,7 @@ func OperateGroupRole(userId, groupId string, roleLevel int32) (string, string, } result = dbConn.Table("group_members").First(&groupMember).Update(updateInfo) - if result.Error != nil { + if result.Error != nil { return result.Error } if result.RowsAffected == 0 { @@ -161,7 +158,7 @@ func OperateGroupRole(userId, groupId string, roleLevel int32) (string, string, return result.Error } if result.RowsAffected == 0 { - return errors.New(fmt.Sprintf("user %s not exist in group %s or already operate", userId, groupId)) + return errors.New(fmt.Sprintf("user %s not exist in group %s or already operate", userId, groupId)) } if groupMaster.UserID == userId { return errors.New(fmt.Sprintf("user %s is master of %s, cant set to ordinary user", userId, groupId)) @@ -171,7 +168,7 @@ func OperateGroupRole(userId, groupId string, roleLevel int32) (string, string, return result.Error } if result.RowsAffected == 0 { - return errors.New(fmt.Sprintf("user %s not exist in group %s or already operate", userId, groupId)) + return errors.New(fmt.Sprintf("user %s not exist in group %s or already operate", userId, groupId)) } } return nil @@ -219,4 +216,4 @@ func GetGroupMaster(groupId string) (db.GroupMember, error) { return groupMember, err } return groupMember, nil -} \ No newline at end of file +} diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go index 745ad0034..836f3f0bf 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go @@ -63,7 +63,7 @@ func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*db.GroupRequest return nil, err } var groupRequest db.GroupRequest - err = dbConn.Table("group_requests").Where("user_id=? and group_id=?", userID, groupID).Find(&groupRequest).Error + err = dbConn.Table("group_requests").Where("user_id=? and group_id=?", userID, groupID).Take(&groupRequest).Error if err != nil { return nil, err } @@ -128,7 +128,6 @@ func GetUserReqGroupByUserID(userID string) ([]db.GroupRequest, error) { return groupRequestList, err } - // //func GroupApplicationResponse(pb *group.GroupApplicationResponseReq) (*group.CommonResp, error) { //