Files
open-im-server/pkg/common/db/relation/friend_request_model.go
T

88 lines
4.2 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2021-05-26 19:32:30 +08:00
import (
2023-02-03 16:51:35 +08:00
"Open_IM/pkg/common/db/table/relation"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2021-12-29 11:02:55 +08:00
"Open_IM/pkg/utils"
2023-01-13 18:20:48 +08:00
"context"
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2021-05-26 19:32:30 +08:00
)
2023-01-30 21:47:29 +08:00
//var FriendRequestDB *gorm.DB
2023-01-11 16:23:16 +08:00
2023-02-01 16:44:50 +08:00
func NewFriendRequestGorm(db *gorm.DB) *FriendRequestGorm {
var fr FriendRequestGorm
2023-01-30 15:26:39 +08:00
fr.DB = db
2023-01-17 14:54:08 +08:00
return &fr
2023-01-13 18:20:48 +08:00
}
2023-02-01 16:44:50 +08:00
type FriendRequestGorm struct {
DB *gorm.DB `gorm:"-"`
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) Create(ctx context.Context, friends []*relation.FriendRequestModel, tx ...*gorm.DB) (err error) {
2023-01-13 18:20:48 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
2023-01-13 18:20:48 +08:00
}()
2023-02-03 16:51:35 +08:00
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Create(&friends).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-01 16:44:50 +08:00
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
2023-01-13 18:20:48 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID)
2023-01-13 18:20:48 +08:00
}()
2023-02-03 16:51:35 +08:00
return utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Delete(&relation.FriendRequestModel{}).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
2023-01-13 18:20:48 +08:00
defer func() {
2023-02-03 16:51:35 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "formUserID", formUserID, "toUserID", toUserID, "args", args)
2023-01-13 18:20:48 +08:00
}()
2023-02-03 16:51:35 +08:00
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id ", formUserID, toUserID).Updates(args).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel, tx ...*gorm.DB) (err error) {
2023-01-13 18:20:48 +08:00
defer func() {
2023-02-03 16:10:33 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendRequests", friendRequests)
2023-01-13 18:20:48 +08:00
}()
2023-02-03 16:51:35 +08:00
return utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Updates(&friendRequests).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequestModel, err error) {
friend = &relation.FriendRequestModel{}
defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend)
return friend, utils.Wrap(f.DB.Model(&relation.FriendRequestModel{}).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Take(friend).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID string, tx ...*gorm.DB) (friend *relation.FriendRequestModel, err error) {
friend = &relation.FriendRequestModel{}
2023-01-30 11:10:26 +08:00
defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "toUserID", toUserID, "friend", friend)
2023-02-03 16:51:35 +08:00
return friend, utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? and to_user_id", fromUserID, toUserID).Find(friend).Error, "")
2023-01-11 16:23:16 +08:00
}
2021-05-26 19:32:30 +08:00
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*relation.FriendRequestModel, total int64, err error) {
2023-01-16 15:53:40 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "toUserID", toUserID, "friends", friends)
2023-01-16 15:53:40 +08:00
}()
2023-02-03 16:10:33 +08:00
2023-02-03 16:51:35 +08:00
err = getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
2023-02-03 16:10:33 +08:00
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
2023-02-03 16:51:35 +08:00
err = utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
2023-02-03 16:10:33 +08:00
return
2023-01-16 15:53:40 +08:00
}
2023-02-03 16:51:35 +08:00
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32, tx ...*gorm.DB) (friends []*relation.FriendRequestModel, total int64, err error) {
2023-01-16 15:53:40 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "fromUserID", fromUserID, "friends", friends)
2023-01-16 15:53:40 +08:00
}()
2023-02-03 16:10:33 +08:00
2023-02-03 16:51:35 +08:00
err = getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
2023-02-03 16:10:33 +08:00
if err != nil {
return nil, 0, utils.Wrap(err, "")
}
2023-02-03 16:51:35 +08:00
err = utils.Wrap(getDBConn(f.DB, tx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Limit(int(showNumber)).Offset(int(pageNumber*showNumber)).Find(&friends).Error, "")
2023-02-03 16:10:33 +08:00
return
2021-12-28 11:57:12 +08:00
}