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

44 lines
1.3 KiB
Go
Raw Normal View History

2023-03-03 17:20:36 +08:00
package relation
import (
"context"
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-03-03 17:20:36 +08:00
"gorm.io/gorm"
2023-03-06 16:40:57 +08:00
"time"
2023-03-03 17:20:36 +08:00
)
2023-03-15 18:35:06 +08:00
type ObjectInfoGorm struct {
*MetaDB
}
2023-03-03 17:20:36 +08:00
func NewObjectInfo(db *gorm.DB) relation.ObjectInfoModelInterface {
return &ObjectInfoGorm{
2023-03-15 18:35:06 +08:00
NewMetaDB(db, &relation.ObjectInfoModel{}),
2023-03-03 17:20:36 +08:00
}
}
func (o *ObjectInfoGorm) NewTx(tx any) relation.ObjectInfoModelInterface {
return &ObjectInfoGorm{
2023-03-15 18:35:06 +08:00
NewMetaDB(tx.(*gorm.DB), &relation.ObjectInfoModel{}),
2023-03-03 17:20:36 +08:00
}
}
func (o *ObjectInfoGorm) SetObject(ctx context.Context, obj *relation.ObjectInfoModel) (err error) {
return utils.Wrap1(o.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Where("name = ?", obj.Name).Delete(&relation.ObjectInfoModel{}).Error; err != nil {
return err
}
return tx.Create(obj).Error
}))
}
func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectInfoModel, err error) {
info = &relation.ObjectInfoModel{}
return info, utils.Wrap1(o.DB.Where("name = ?", name).Take(info).Error)
}
2023-03-06 16:40:57 +08:00
func (o *ObjectInfoGorm) DeleteExpiration(ctx context.Context, expiration time.Time) (err error) {
return utils.Wrap1(o.DB.Where("expiration_time IS NOT NULL AND expiration_time <= ?", expiration).Delete(&relation.ObjectInfoModel{}).Error)
}