mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 16:15:59 +08:00
Error code standardization
This commit is contained in:
@@ -6,33 +6,34 @@ import (
|
||||
"Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FriendInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
// 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, friends []*relation.FriendModel, revFriends []*relation.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
// (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可
|
||||
BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error)
|
||||
// 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
// 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 不是好友则建立双向好友关系 (4)修改申请记录 已同意
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
|
||||
// Delete 删除好友
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
// UpdateRemark 更新好友备注
|
||||
// 删除好友 外部判断是否好友关系
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
|
||||
// 更新好友备注 零值也支持
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
// 获取ownerUserID的好友列表 无结果不返回错误
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
// friendUserID在哪些人的好友列表中
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
// 获取我发出去的好友申请 无结果不返回错误
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
// 获取我收到的的好友申请 无结果不返回错误
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
// 获取某人指定好友的信息 如果有一个不存在也返回错误
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ func NewFriendController(db *gorm.DB) *FriendController {
|
||||
return &FriendController{database: NewFriendDatabase(db)}
|
||||
}
|
||||
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
func (f *FriendController) CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||||
return f.database.CheckIn(ctx, user1, user2)
|
||||
}
|
||||
@@ -55,8 +56,8 @@ func (f *FriendController) AddFriendRequest(ctx context.Context, fromUserID, toU
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendController) BecomeFriend(ctx context.Context, ownerUserID string, friends []*relation.FriendModel) (err error) {
|
||||
return f.database.BecomeFriend(ctx, ownerUserID, friends)
|
||||
func (f *FriendController) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error) {
|
||||
return f.database.BecomeFriends(ctx, ownerUserID, friendUserIDs, addSource, OperatorUserID)
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
@@ -70,7 +71,7 @@ func (f *FriendController) AgreeFriendRequest(ctx context.Context, friendRequest
|
||||
}
|
||||
|
||||
// Delete 删除好友
|
||||
func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
|
||||
func (f *FriendController) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||||
return f.database.Delete(ctx, ownerUserID, friendUserIDs)
|
||||
}
|
||||
|
||||
@@ -102,33 +103,32 @@ func (f *FriendController) FindFriendRequestToMe(ctx context.Context, userID str
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
func (f *FriendController) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
|
||||
return f.database.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||||
|
||||
}
|
||||
|
||||
type FriendDatabaseInterface interface {
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
// 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
CheckIn(ctx context.Context, user1, user2 string) (inUser1Friends bool, inUser2Friends bool, err error)
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
// 增加或者更新好友申请
|
||||
AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error)
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriend(ctx context.Context, ownerUserID string, friends []*relation.FriendModel) (err error)
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
// 先判断是否在好友表,如果在则不插入
|
||||
BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error)
|
||||
// 拒绝好友申请
|
||||
RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
// 同意好友申请
|
||||
AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error)
|
||||
// Delete 删除好友
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
|
||||
// UpdateRemark 更新好友备注
|
||||
// 删除好友
|
||||
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
|
||||
// 更新好友备注
|
||||
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
// 获取ownerUserID的好友列表
|
||||
FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
// friendUserID在哪些人的好友列表中
|
||||
FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error)
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
// 获取我发出去的好友申请
|
||||
FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
// 获取我收到的的好友申请
|
||||
FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error)
|
||||
// FindFriends 获取某人指定好友的信息
|
||||
// 获取某人指定好友的信息
|
||||
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error)
|
||||
}
|
||||
|
||||
@@ -141,9 +141,12 @@ func NewFriendDatabase(db *gorm.DB) *FriendDatabase {
|
||||
return &FriendDatabase{friend: relation1.NewFriendGorm(db), friendRequest: relation1.NewFriendRequestGorm(db)}
|
||||
}
|
||||
|
||||
// CheckIn 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
// ok 检查user2是否在user1的好友列表中(inUser1Friends==true) 检查user1是否在user2的好友列表中(inUser2Friends==true)
|
||||
func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Friends bool, inUser2Friends bool, err error) {
|
||||
friends, err := f.friend.FindUserState(ctx, userID1, userID2)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
for _, v := range friends {
|
||||
if v.OwnerUserID == userID1 && v.FriendUserID == userID2 {
|
||||
inUser1Friends = true
|
||||
@@ -155,14 +158,16 @@ func (f *FriendDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (
|
||||
return
|
||||
}
|
||||
|
||||
// AddFriendRequest 增加或者更新好友申请
|
||||
// 增加或者更新好友申请 如果之前有记录则更新,没有记录则新增
|
||||
func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUserID string, reqMsg string, ex string) (err error) {
|
||||
return f.friendRequest.DB.Transaction(func(tx *gorm.DB) error {
|
||||
fq, err := f.friendRequest.Find(ctx, fromUserID, toUserID, tx)
|
||||
if err != nil {
|
||||
_, err := f.friendRequest.Take(ctx, fromUserID, toUserID, tx)
|
||||
//有db错误
|
||||
if err != nil && errors.Unwrap(err) != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
if fq != nil { //
|
||||
//无错误 则更新
|
||||
if err == nil {
|
||||
m := make(map[string]interface{}, 1)
|
||||
m["handle_result"] = 0
|
||||
m["handle_msg"] = ""
|
||||
@@ -171,44 +176,45 @@ func (f *FriendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
|
||||
if err := f.friendRequest.UpdateByMap(ctx, fromUserID, toUserID, m, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := f.friendRequest.Create(ctx, []*relation.FriendRequestModel{&relation.FriendRequestModel{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
//gorm.ErrRecordNotFound 错误,则新增
|
||||
if err := f.friendRequest.Create(ctx, []*relation.FriendRequestModel{&relation.FriendRequestModel{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// BecomeFriend 先判断是否在好友表,如果在则不插入
|
||||
func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, friends []*relation.FriendModel) (err error) {
|
||||
// (1)先判断是否在好友表 (在不在都不返回错误) (2)对于不在好友列表的 插入即可
|
||||
func (f *FriendDatabase) BecomeFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, addSource int32, OperatorUserID string) (err error) {
|
||||
return f.friend.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//先find 找出重复的 去掉重复的
|
||||
friendUserIDs := make([]string, 0, len(friends))
|
||||
for _, v := range friends {
|
||||
friendUserIDs = append(friendUserIDs, v.FriendUserID)
|
||||
}
|
||||
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fs1 = append(fs1, friends...)
|
||||
for _, v := range friendUserIDs {
|
||||
fs1 = append(fs1, &relation.FriendModel{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: OperatorUserID})
|
||||
}
|
||||
fs11 := utils.DistinctAny(fs1, func(e *relation.FriendModel) string {
|
||||
return utils.UniqueJoin(e.OwnerUserID, e.FriendUserID)
|
||||
return e.FriendUserID
|
||||
})
|
||||
|
||||
err = f.friend.Create(ctx, fs11, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range friends {
|
||||
fs2 = append(fs2, &relation.FriendModel{OwnerUserID: v.FriendUserID, FriendUserID: ownerUserID})
|
||||
for _, v := range friendUserIDs {
|
||||
fs2 = append(fs2, &relation.FriendModel{OwnerUserID: v, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: OperatorUserID})
|
||||
}
|
||||
fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string {
|
||||
return utils.UniqueJoin(e.OwnerUserID, e.FriendUserID)
|
||||
return e.OwnerUserID
|
||||
})
|
||||
err = f.friend.Create(ctx, fs22, tx)
|
||||
if err != nil {
|
||||
@@ -218,8 +224,12 @@ func (f *FriendDatabase) BecomeFriend(ctx context.Context, ownerUserID string, f
|
||||
})
|
||||
}
|
||||
|
||||
// RefuseFriendRequest 拒绝好友申请
|
||||
// 拒绝好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)修改申请记录 已拒绝
|
||||
func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
|
||||
_, err = f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
friendRequest.HandleResult = constant.FriendResponseRefuse
|
||||
err = f.friendRequest.Update(ctx, []*relation.FriendRequestModel{friendRequest})
|
||||
if err != nil {
|
||||
@@ -228,79 +238,90 @@ func (f *FriendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest
|
||||
return nil
|
||||
}
|
||||
|
||||
// AgreeFriendRequest 同意好友申请
|
||||
// 同意好友申请 (1)检查是否有申请记录且为未处理状态 (没有记录返回错误) (2)检查是否好友(不返回错误) (3) 不是好友则建立双向好友关系 (4)修改申请记录 已同意
|
||||
func (f *FriendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
|
||||
return f.friend.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//先find 找出重复的 去掉重复的
|
||||
fs1, err := f.friend.FindFriends(ctx, friendRequest.FromUserID, []string{friendRequest.ToUserID}, tx)
|
||||
_, err = f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(fs1) == 0 {
|
||||
err = f.friend.Create(ctx, []*relation.FriendModel{
|
||||
&relation.FriendModel{
|
||||
OwnerUserID: friendRequest.FromUserID,
|
||||
FriendUserID: friendRequest.ToUserID,
|
||||
OperatorUserID: friendRequest.ToUserID,
|
||||
AddSource: constant.BecomeFriendByApply,
|
||||
}}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, friendRequest.ToUserID, []string{friendRequest.FromUserID}, tx)
|
||||
if len(fs2) == 0 {
|
||||
err = f.friend.Create(ctx, []*relation.FriendModel{
|
||||
&relation.FriendModel{
|
||||
OwnerUserID: friendRequest.ToUserID,
|
||||
FriendUserID: friendRequest.FromUserID,
|
||||
OperatorUserID: friendRequest.ToUserID,
|
||||
AddSource: constant.BecomeFriendByApply,
|
||||
}}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
friendRequest.HandlerUserID = friendRequest.FromUserID
|
||||
friendRequest.HandleResult = constant.FriendResponseAgree
|
||||
err = f.friendRequest.Update(ctx, []*relation.FriendRequestModel{friendRequest}, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ownerUserID := friendRequest.FromUserID
|
||||
friendUserIDs := []string{friendRequest.ToUserID}
|
||||
addSource := int32(constant.BecomeFriendByApply)
|
||||
OperatorUserID := friendRequest.FromUserID
|
||||
//先find 找出重复的 去掉重复的
|
||||
fs1, err := f.friend.FindFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range friendUserIDs {
|
||||
fs1 = append(fs1, &relation.FriendModel{OwnerUserID: ownerUserID, FriendUserID: v, AddSource: addSource, OperatorUserID: OperatorUserID})
|
||||
}
|
||||
fs11 := utils.DistinctAny(fs1, func(e *relation.FriendModel) string {
|
||||
return e.FriendUserID
|
||||
})
|
||||
|
||||
err = f.friend.Create(ctx, fs11, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fs2, err := f.friend.FindReversalFriends(ctx, ownerUserID, friendUserIDs, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range friendUserIDs {
|
||||
fs2 = append(fs2, &relation.FriendModel{OwnerUserID: v, FriendUserID: ownerUserID, AddSource: addSource, OperatorUserID: OperatorUserID})
|
||||
}
|
||||
fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string {
|
||||
return e.OwnerUserID
|
||||
})
|
||||
err = f.friend.Create(ctx, fs22, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除好友
|
||||
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserID string) (err error) {
|
||||
return f.friend.Delete(ctx, ownerUserID, friendUserID)
|
||||
// 删除好友 外部判断是否好友关系
|
||||
func (f *FriendDatabase) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) {
|
||||
return f.friend.Delete(ctx, ownerUserID, friendUserIDs)
|
||||
}
|
||||
|
||||
// UpdateRemark 更新好友备注
|
||||
// 更新好友备注 零值也支持
|
||||
func (f *FriendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
|
||||
return f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark)
|
||||
}
|
||||
|
||||
// FindOwnerFriends 获取ownerUserID的好友列表
|
||||
// 获取ownerUserID的好友列表 无结果不返回错误
|
||||
func (f *FriendDatabase) FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
|
||||
return f.friend.FindOwnerFriends(ctx, ownerUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindInWhoseFriends friendUserID在哪些人的好友列表中
|
||||
// friendUserID在哪些人的好友列表中
|
||||
func (f *FriendDatabase) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*relation.FriendModel, total int64, err error) {
|
||||
return f.friend.FindInWhoseFriends(ctx, friendUserID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestFromMe 获取我发出去的好友申请
|
||||
// 获取我发出去的好友申请 无结果不返回错误
|
||||
func (f *FriendDatabase) FindFriendRequestFromMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
|
||||
return f.friendRequest.FindFromUserID(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriendRequestToMe 获取我收到的的好友申请
|
||||
// 获取我收到的的好友申请 无结果不返回错误
|
||||
func (f *FriendDatabase) FindFriendRequestToMe(ctx context.Context, userID string, pageNumber, showNumber int32) (friends []*relation.FriendRequestModel, total int64, err error) {
|
||||
return f.friendRequest.FindToUserID(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
// FindFriends 获取某人指定好友的信息 如果有一个不存在,也返回错误
|
||||
// 获取某人指定好友的信息 如果有好友不存在,也返回错误
|
||||
func (f *FriendDatabase) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*relation.FriendModel, err error) {
|
||||
friends, err = f.friend.FindFriends(ctx, ownerUserID, friendUserIDs)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,20 +9,16 @@ import (
|
||||
)
|
||||
|
||||
type UserInterface interface {
|
||||
//获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
//获取指定用户的信息 如有userID未找到 也返回错误
|
||||
Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error)
|
||||
//插入
|
||||
Create(ctx context.Context, users []*relation2.UserModel) error
|
||||
//更新
|
||||
//插入多条 外部保证userID 不重复 且在db中不存在
|
||||
Create(ctx context.Context, users []*relation2.UserModel) (err error)
|
||||
//更新(非零值) 外部保证userID存在
|
||||
Update(ctx context.Context, users []*relation2.UserModel) (err error)
|
||||
//更新带零值的
|
||||
//更新(零值) 外部保证userID存在
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
//通过名字搜索
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
//通过名字和id搜索
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
//获取,如果没找到,不不返回错误
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
//获取,如果没找到,不返回错误
|
||||
Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
//userIDs是否存在 只要有一个存在就为true
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
}
|
||||
@@ -45,20 +41,12 @@ func (u *UserController) UpdateByMap(ctx context.Context, userID string, args ma
|
||||
return u.database.UpdateByMap(ctx, userID, args)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.database.GetByName(ctx, userName, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.database.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.database.Get(ctx, showNumber, pageNumber)
|
||||
func (u *UserController) Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.database.Get(ctx, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (u *UserController) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
return u.IsExist(ctx, userIDs)
|
||||
return u.database.IsExist(ctx, userIDs)
|
||||
}
|
||||
func NewUserController(db *gorm.DB) *UserController {
|
||||
controller := &UserController{database: newUserDatabase(db)}
|
||||
@@ -70,27 +58,25 @@ type UserDatabaseInterface interface {
|
||||
Create(ctx context.Context, users []*relation2.UserModel) error
|
||||
Update(ctx context.Context, users []*relation2.UserModel) (err error)
|
||||
UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error)
|
||||
GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
Get(ctx context.Context, pageNumber, showNumber int32) (users []*relation2.UserModel, count int64, err error)
|
||||
IsExist(ctx context.Context, userIDs []string) (exist bool, err error)
|
||||
}
|
||||
|
||||
type UserDatabase struct {
|
||||
sqlDB *relation.UserGorm
|
||||
user *relation.UserGorm
|
||||
}
|
||||
|
||||
func newUserDatabase(db *gorm.DB) *UserDatabase {
|
||||
sqlDB := relation.NewUserGorm(db)
|
||||
database := &UserDatabase{
|
||||
sqlDB: sqlDB,
|
||||
user: sqlDB,
|
||||
}
|
||||
return database
|
||||
}
|
||||
|
||||
// 获取指定用户的信息 如果有记录未找到 也返回错误
|
||||
// 获取指定用户的信息 如有userID未找到 也返回错误
|
||||
func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*relation2.UserModel, err error) {
|
||||
users, err = u.sqlDB.Find(ctx, userIDs)
|
||||
users, err = u.user.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -100,33 +86,31 @@ func (u *UserDatabase) Find(ctx context.Context, userIDs []string) (users []*rel
|
||||
return
|
||||
}
|
||||
|
||||
// 插入多条 外部保证userID 不重复 且在db中不存在
|
||||
func (u *UserDatabase) Create(ctx context.Context, users []*relation2.UserModel) (err error) {
|
||||
return u.sqlDB.Create(ctx, users)
|
||||
return u.user.Create(ctx, users)
|
||||
}
|
||||
|
||||
// 更新(非零值) 外部保证userID存在
|
||||
func (u *UserDatabase) Update(ctx context.Context, users []*relation2.UserModel) (err error) {
|
||||
return u.sqlDB.Update(ctx, users)
|
||||
return u.user.Update(ctx, users)
|
||||
}
|
||||
|
||||
// 更新(零值) 外部保证userID存在
|
||||
func (u *UserDatabase) UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) {
|
||||
return u.sqlDB.UpdateByMap(ctx, userID, args)
|
||||
}
|
||||
func (u *UserDatabase) GetByName(ctx context.Context, userName string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.sqlDB.GetByName(ctx, userName, showNumber, pageNumber)
|
||||
}
|
||||
func (u *UserDatabase) GetByNameAndID(ctx context.Context, content string, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.sqlDB.GetByNameAndID(ctx, content, showNumber, pageNumber)
|
||||
return u.user.UpdateByMap(ctx, userID, args)
|
||||
}
|
||||
|
||||
// 获取,如果没找到,不返回错误
|
||||
func (u *UserDatabase) Get(ctx context.Context, showNumber, pageNumber int32) (users []*relation2.UserModel, count int64, err error) {
|
||||
return u.sqlDB.Get(ctx, showNumber, pageNumber)
|
||||
return u.user.Get(ctx, showNumber, pageNumber)
|
||||
}
|
||||
|
||||
// userIDs是否存在 只要有一个存在就为true
|
||||
func (u *UserDatabase) IsExist(ctx context.Context, userIDs []string) (exist bool, err error) {
|
||||
users, err := u.sqlDB.Find(ctx, userIDs)
|
||||
users, err := u.user.Find(ctx, userIDs)
|
||||
if err != nil {
|
||||
return
|
||||
return false, err
|
||||
}
|
||||
if len(users) > 0 {
|
||||
return true, nil
|
||||
|
||||
Reference in New Issue
Block a user