1Panel/backend/app/repo/backup.go

121 lines
3.1 KiB
Go
Raw Normal View History

package repo
import (
"context"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
"gorm.io/gorm"
)
type BackupRepo struct{}
type IBackupRepo interface {
2022-09-19 11:42:06 +00:00
Get(opts ...DBOption) (model.BackupAccount, error)
ListRecord(opts ...DBOption) ([]model.BackupRecord, error)
PageRecord(page, size int, opts ...DBOption) (int64, []model.BackupRecord, error)
2022-09-19 11:42:06 +00:00
List(opts ...DBOption) ([]model.BackupAccount, error)
Create(backup *model.BackupAccount) error
CreateRecord(record *model.BackupRecord) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
DeleteRecord(ctx context.Context, opts ...DBOption) error
WithByDetailName(detailName string) DBOption
}
func NewIBackupRepo() IBackupRepo {
return &BackupRepo{}
}
func (u *BackupRepo) Get(opts ...DBOption) (model.BackupAccount, error) {
var backup model.BackupAccount
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&backup).Error
return backup, err
}
func (u *BackupRepo) ListRecord(opts ...DBOption) ([]model.BackupRecord, error) {
var users []model.BackupRecord
db := global.DB.Model(&model.BackupRecord{})
for _, opt := range opts {
db = opt(db)
}
2023-02-09 08:13:06 +00:00
err := db.Find(&users).Error
return users, err
}
func (u *BackupRepo) PageRecord(page, size int, opts ...DBOption) (int64, []model.BackupRecord, error) {
var users []model.BackupRecord
db := global.DB.Model(&model.BackupRecord{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&users).Error
return count, users, err
}
func (u *BackupRepo) WithByDetailName(detailName string) DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(detailName) == 0 {
return g
}
return g.Where("detail_name = ?", detailName)
}
}
func (u *BackupRepo) WithByFileName(fileName string) DBOption {
2022-11-08 11:03:38 +00:00
return func(g *gorm.DB) *gorm.DB {
if len(fileName) == 0 {
return g
}
return g.Where("file_name = ?", fileName)
}
}
func (u *BackupRepo) WithByType(backupType string) DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(backupType) == 0 {
return g
}
return g.Where("type = ?", backupType)
}
}
2022-09-19 11:42:06 +00:00
func (u *BackupRepo) List(opts ...DBOption) ([]model.BackupAccount, error) {
var ops []model.BackupAccount
db := global.DB.Model(&model.BackupAccount{})
for _, opt := range opts {
db = opt(db)
}
2022-09-19 11:42:06 +00:00
err := db.Find(&ops).Error
return ops, err
}
func (u *BackupRepo) Create(backup *model.BackupAccount) error {
return global.DB.Create(backup).Error
}
func (u *BackupRepo) CreateRecord(record *model.BackupRecord) error {
return global.DB.Create(record).Error
}
func (u *BackupRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.BackupAccount{}).Where("id = ?", id).Updates(vars).Error
}
func (u *BackupRepo) Delete(opts ...DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.BackupAccount{}).Error
}
func (u *BackupRepo) DeleteRecord(ctx context.Context, opts ...DBOption) error {
return getTx(ctx, opts...).Delete(&model.BackupRecord{}).Error
}