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

75 lines
3.5 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-01-13 18:20:48 +08:00
"context"
2023-03-16 20:21:10 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2021-05-26 19:32:30 +08:00
)
2023-03-15 18:35:06 +08:00
type FriendRequestGorm struct {
*MetaDB
2023-01-13 18:20:48 +08:00
}
2023-03-15 18:35:06 +08:00
func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface {
2023-03-16 20:32:58 +08:00
return &FriendRequestGorm{NewMetaDB(db, &relation.FriendRequestModel{})}
2023-02-21 12:59:44 +08:00
}
func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface {
2023-03-16 20:32:58 +08:00
return &FriendRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendRequestModel{})}
2023-02-01 16:44:50 +08:00
}
2023-02-07 20:28:34 +08:00
// 插入多条记录
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
2023-03-16 20:21:10 +08:00
return utils.Wrap(f.db(ctx).Create(&friendRequests).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-07 20:28:34 +08:00
// 删除记录
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
2023-03-16 20:21:10 +08:00
return utils.Wrap(f.db(ctx).Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID).Delete(&relation.FriendRequestModel{}).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-07 20:28:34 +08:00
// 更新零值
2023-03-17 15:04:01 +08:00
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, fromUserID string, toUserID string, args map[string]interface{}) (err error) {
return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID).Updates(args).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-03-17 15:04:01 +08:00
// 更新记录 (非零值)
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return utils.Wrap(f.db(ctx).Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).Updates(friendRequest).Error, "")
2023-01-13 18:20:48 +08:00
}
2023-02-07 20:28:34 +08:00
// 获取来指定用户的好友申请 未找到 不返回错误
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
2023-02-07 20:28:34 +08:00
friendRequest = &relation.FriendRequestModel{}
2023-03-17 11:26:34 +08:00
err = utils.Wrap(f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error, "")
return friendRequest, err
2023-01-13 18:20:48 +08:00
}
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
2023-02-07 20:28:34 +08:00
friendRequest = &relation.FriendRequestModel{}
2023-03-17 11:26:34 +08:00
err = utils.Wrap(f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error, "")
return friendRequest, err
2023-01-11 16:23:16 +08:00
}
2021-05-26 19:32:30 +08:00
2023-02-07 20:28:34 +08:00
// 获取toUserID收到的好友申请列表
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) FindToUserID(ctx context.Context, toUserID string, pageNumber, showNumber int32) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
2023-03-16 20:21:10 +08:00
err = f.db(ctx).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-05-09 18:23:28 +08:00
err = utils.Wrap(f.db(ctx).Where("to_user_id = ? ", toUserID).Limit(int(showNumber)).Offset(int(pageNumber-1)*int(showNumber)).Find(&friendRequests).Error, "")
2023-02-03 16:10:33 +08:00
return
2023-01-16 15:53:40 +08:00
}
2023-02-07 20:28:34 +08:00
// 获取fromUserID发出去的好友申请列表
2023-02-21 12:59:44 +08:00
func (f *FriendRequestGorm) FindFromUserID(ctx context.Context, fromUserID string, pageNumber, showNumber int32) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
2023-03-16 20:21:10 +08:00
err = f.db(ctx).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-05-09 18:40:35 +08:00
err = utils.Wrap(f.db(ctx).Where("from_user_id = ? ", fromUserID).Limit(int(showNumber)).Offset(int(pageNumber-1)*int(showNumber)).Find(&friendRequests).Error, "")
2023-02-03 16:10:33 +08:00
return
2021-12-28 11:57:12 +08:00
}