2022-09-16 18:53:45 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-12-21 15:54:34 +08:00
|
|
|
"context"
|
2024-01-24 17:41:56 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-27 23:09:39 +08:00
|
|
|
"gorm.io/gorm"
|
2022-09-16 18:53:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type BackupRepo struct{}
|
|
|
|
|
|
|
|
type IBackupRepo interface {
|
2022-09-19 19:42:06 +08:00
|
|
|
Get(opts ...DBOption) (model.BackupAccount, error)
|
2022-10-27 23:09:39 +08:00
|
|
|
ListRecord(opts ...DBOption) ([]model.BackupRecord, error)
|
|
|
|
PageRecord(page, size int, opts ...DBOption) (int64, []model.BackupRecord, error)
|
2022-09-19 19:42:06 +08:00
|
|
|
List(opts ...DBOption) ([]model.BackupAccount, error)
|
2022-09-16 18:53:45 +08:00
|
|
|
Create(backup *model.BackupAccount) error
|
2022-10-27 23:09:39 +08:00
|
|
|
CreateRecord(record *model.BackupRecord) error
|
2022-09-16 18:53:45 +08:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
|
|
|
Delete(opts ...DBOption) error
|
2022-12-21 15:54:34 +08:00
|
|
|
DeleteRecord(ctx context.Context, opts ...DBOption) error
|
2023-12-18 17:54:15 +08:00
|
|
|
UpdateRecord(record *model.BackupRecord) error
|
2022-10-27 23:09:39 +08:00
|
|
|
WithByDetailName(detailName string) DBOption
|
2023-03-28 18:00:06 +08:00
|
|
|
WithByFileName(fileName string) DBOption
|
|
|
|
WithByType(backupType string) DBOption
|
2024-01-24 17:41:56 +08:00
|
|
|
WithByCronID(cronjobID uint) DBOption
|
2024-08-13 15:28:55 +08:00
|
|
|
WithFileNameStartWith(filePrefix string) DBOption
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
|
|
|
|
2022-09-26 18:40:00 +08:00
|
|
|
func NewIBackupRepo() IBackupRepo {
|
2022-09-16 18:53:45 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:09:39 +08:00
|
|
|
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)
|
|
|
|
}
|
2024-08-13 15:28:55 +08:00
|
|
|
err := db.Debug().Find(&users).Error
|
2022-10-27 23:09:39 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-20 22:00:05 +08:00
|
|
|
func (u *BackupRepo) WithByDetailName(detailName string) DBOption {
|
2022-10-27 23:09:39 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
if len(detailName) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("detail_name = ?", detailName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 22:00:05 +08:00
|
|
|
func (u *BackupRepo) WithByFileName(fileName string) DBOption {
|
2022-11-08 19:03:38 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
if len(fileName) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("file_name = ?", fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 15:28:55 +08:00
|
|
|
func (u *BackupRepo) WithFileNameStartWith(filePrefix string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("file_name LIKE ?", filePrefix+"%")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 22:00:05 +08:00
|
|
|
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 19:42:06 +08:00
|
|
|
func (u *BackupRepo) List(opts ...DBOption) ([]model.BackupAccount, error) {
|
2022-09-16 18:53:45 +08:00
|
|
|
var ops []model.BackupAccount
|
|
|
|
db := global.DB.Model(&model.BackupAccount{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2022-09-19 19:42:06 +08:00
|
|
|
err := db.Find(&ops).Error
|
|
|
|
return ops, err
|
2022-09-16 18:53:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupRepo) Create(backup *model.BackupAccount) error {
|
|
|
|
return global.DB.Create(backup).Error
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:09:39 +08:00
|
|
|
func (u *BackupRepo) CreateRecord(record *model.BackupRecord) error {
|
|
|
|
return global.DB.Create(record).Error
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:54:15 +08:00
|
|
|
func (u *BackupRepo) UpdateRecord(record *model.BackupRecord) error {
|
|
|
|
return global.DB.Save(record).Error
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:53:45 +08:00
|
|
|
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
|
|
|
|
}
|
2022-10-27 23:09:39 +08:00
|
|
|
|
2022-12-21 15:54:34 +08:00
|
|
|
func (u *BackupRepo) DeleteRecord(ctx context.Context, opts ...DBOption) error {
|
|
|
|
return getTx(ctx, opts...).Delete(&model.BackupRecord{}).Error
|
2022-10-27 23:09:39 +08:00
|
|
|
}
|
2024-01-24 17:41:56 +08:00
|
|
|
|
|
|
|
func (u *BackupRepo) WithByCronID(cronjobID uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("cronjob_id = ?", cronjobID)
|
|
|
|
}
|
|
|
|
}
|