Replace find with take

pull/168/head
skiffer-git 3 years ago
parent 05fbbfcf39
commit ed65ad842f

@ -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 {

@ -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
}
}

@ -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) {
//

Loading…
Cancel
Save