mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-06-10 19:39:20 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
commit
c5974781fb
@ -1,11 +1,11 @@
|
||||
package relation
|
||||
package ormutil
|
||||
|
||||
import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
func GormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
var count int64
|
||||
if err := db.Count(&count).Error; err != nil {
|
||||
return 0, nil, utils.Wrap(err, "")
|
||||
@ -17,7 +17,7 @@ func gormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, e
|
||||
return uint32(count), es, nil
|
||||
}
|
||||
|
||||
func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
func GormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
||||
if len(fields) > 0 && value != "" {
|
||||
value = "%" + value + "%"
|
||||
if len(fields) == 1 {
|
||||
@ -30,17 +30,17 @@ func gormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, s
|
||||
db = db.Where(t)
|
||||
}
|
||||
}
|
||||
return gormPage[E](db, pageNumber, showNumber)
|
||||
return GormPage[E](db, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func gormIn[E any](db **gorm.DB, field string, es []E) {
|
||||
func GormIn[E any](db **gorm.DB, field string, es []E) {
|
||||
if len(es) == 0 {
|
||||
return
|
||||
}
|
||||
*db = (*db).Where(field+" in (?)", es)
|
||||
}
|
||||
|
||||
func mapCount(db *gorm.DB, field string) (map[string]uint32, error) {
|
||||
func MapCount(db *gorm.DB, field string) (map[string]uint32, error) {
|
||||
var items []struct {
|
||||
ID string `gorm:"column:id"`
|
||||
Count uint32 `gorm:"column:count"`
|
@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
@ -50,7 +51,7 @@ func (b *BlackGorm) FindOwnerBlacks(ctx context.Context, ownerUserID string, pag
|
||||
if err != nil {
|
||||
return nil, 0, utils.Wrap(err, "")
|
||||
}
|
||||
totalUint32, blacks, err := gormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
|
||||
totalUint32, blacks, err := ormutil.GormPage[relation.BlackModel](b.db(ctx), pageNumber, showNumber)
|
||||
total = int64(totalUint32)
|
||||
return
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package relation
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
@ -71,14 +72,14 @@ func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string) (groupM
|
||||
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, groupList []*relation.GroupMemberModel, err error) {
|
||||
db := g.DB
|
||||
gormIn(&db, "group_id", groupIDs)
|
||||
gormIn(&db, "user_id", userIDs)
|
||||
gormIn(&db, "role_level", roleLevels)
|
||||
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
ormutil.GormIn(&db, "group_id", groupIDs)
|
||||
ormutil.GormIn(&db, "user_id", userIDs)
|
||||
ormutil.GormIn(&db, "role_level", roleLevels)
|
||||
return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string) (count map[string]uint32, err error) {
|
||||
return mapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
|
||||
return ormutil.MapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error) {
|
||||
|
@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
@ -43,7 +44,7 @@ func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.G
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
|
||||
return gormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
|
||||
return ormutil.GormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
||||
|
@ -2,6 +2,7 @@ package relation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/ormutil"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
@ -39,5 +40,5 @@ func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID stri
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupRequestModel, err error) {
|
||||
return gormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
return ormutil.GormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ const (
|
||||
|
||||
// 群组错误码
|
||||
GroupIDNotFoundError = 1201 //GroupID不存在
|
||||
GroupIDIDExisted = 1202 //GroupID已存在
|
||||
GroupIDExisted = 1202 //GroupID已存在
|
||||
OnlyOneOwnerError = 1203 //只能有一个群主
|
||||
InGroupAlreadyError = 1204 //已在群组中
|
||||
NotInGroupYetError = 1205 //不在群组中
|
||||
|
@ -11,7 +11,8 @@ type CodeError interface {
|
||||
Msg() string
|
||||
Detail() string
|
||||
WithDetail(detail string) CodeError
|
||||
Is(err error) bool
|
||||
// Is 判断是否是某个错误, loose为false时, 只有错误码相同就认为是同一个错误, 默认为true
|
||||
Is(err error, loose ...bool) bool
|
||||
Wrap(msg ...string) error
|
||||
error
|
||||
}
|
||||
@ -59,13 +60,23 @@ func (e *codeError) Wrap(w ...string) error {
|
||||
return errors.Wrap(e, strings.Join(w, ", "))
|
||||
}
|
||||
|
||||
func (e *codeError) Is(err error) bool {
|
||||
func (e *codeError) Is(err error, loose ...bool) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var allowSubclasses bool
|
||||
if len(loose) == 0 {
|
||||
allowSubclasses = true
|
||||
} else {
|
||||
allowSubclasses = loose[0]
|
||||
}
|
||||
codeErr, ok := Unwrap(err).(CodeError)
|
||||
if ok {
|
||||
return codeErr.Code() == e.code
|
||||
if allowSubclasses {
|
||||
return Relation.Is(e.code, codeErr.Code())
|
||||
} else {
|
||||
return codeErr.Code() == e.code
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ var (
|
||||
|
||||
ErrUserIDNotFound = NewCodeError(UserIDNotFoundError, "UserIDNotFoundError")
|
||||
ErrGroupIDNotFound = NewCodeError(GroupIDNotFoundError, "GroupIDNotFoundError")
|
||||
ErrGroupIDExisted = NewCodeError(GroupIDIDExisted, "GroupIDExisted")
|
||||
ErrGroupIDExisted = NewCodeError(GroupIDExisted, "GroupIDExisted")
|
||||
ErrUserIDExisted = NewCodeError(UserIDExisted, "UserIDExisted")
|
||||
|
||||
ErrRecordNotFound = NewCodeError(RecordNotFoundError, "RecordNotFoundError")
|
||||
|
43
pkg/errs/relation.go
Normal file
43
pkg/errs/relation.go
Normal file
@ -0,0 +1,43 @@
|
||||
package errs
|
||||
|
||||
var Relation = &relation{m: make(map[int]map[int]struct{})}
|
||||
|
||||
func init() {
|
||||
Relation.Add(RecordNotFoundError, UserIDNotFoundError)
|
||||
Relation.Add(RecordNotFoundError, GroupIDNotFoundError)
|
||||
Relation.Add(DuplicateKeyError, UserIDExisted)
|
||||
Relation.Add(DuplicateKeyError, GroupIDExisted)
|
||||
}
|
||||
|
||||
type relation struct {
|
||||
m map[int]map[int]struct{}
|
||||
}
|
||||
|
||||
func (r *relation) Add(codes ...int) {
|
||||
if len(codes) < 2 {
|
||||
panic("codes length must be greater than 2")
|
||||
}
|
||||
for i := 1; i < len(codes); i++ {
|
||||
parent := codes[i-1]
|
||||
s, ok := r.m[parent]
|
||||
if !ok {
|
||||
s = make(map[int]struct{})
|
||||
r.m[parent] = s
|
||||
}
|
||||
for _, code := range codes[i:] {
|
||||
s[code] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *relation) Is(parent, child int) bool {
|
||||
if parent == child {
|
||||
return true
|
||||
}
|
||||
s, ok := r.m[parent]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, ok = s[child]
|
||||
return ok
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user