2023-01-06 10:53:25 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ISnapshotRepo interface {
|
|
|
|
Get(opts ...DBOption) (model.Snapshot, error)
|
2023-01-09 14:55:10 +00:00
|
|
|
GetList(opts ...DBOption) ([]model.Snapshot, error)
|
|
|
|
Create(snap *model.Snapshot) error
|
2023-01-06 10:53:25 +00:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
2023-01-09 14:55:10 +00:00
|
|
|
Page(limit, offset int, opts ...DBOption) (int64, []model.Snapshot, error)
|
2023-01-06 10:53:25 +00:00
|
|
|
Delete(opts ...DBOption) error
|
2023-08-23 09:16:18 +00:00
|
|
|
|
|
|
|
GetStatus(snapID uint) (model.SnapshotStatus, error)
|
|
|
|
GetStatusList(opts ...DBOption) ([]model.SnapshotStatus, error)
|
|
|
|
CreateStatus(snap *model.SnapshotStatus) error
|
|
|
|
DeleteStatus(snapID uint) error
|
|
|
|
UpdateStatus(id uint, vars map[string]interface{}) error
|
2023-01-06 10:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewISnapshotRepo() ISnapshotRepo {
|
|
|
|
return &SnapshotRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SnapshotRepo struct{}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) Get(opts ...DBOption) (model.Snapshot, error) {
|
2023-01-09 14:55:10 +00:00
|
|
|
var Snapshot model.Snapshot
|
2023-01-06 10:53:25 +00:00
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2023-01-09 14:55:10 +00:00
|
|
|
err := db.First(&Snapshot).Error
|
|
|
|
return Snapshot, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) GetList(opts ...DBOption) ([]model.Snapshot, error) {
|
|
|
|
var snaps []model.Snapshot
|
|
|
|
db := global.DB.Model(&model.Snapshot{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&snaps).Error
|
|
|
|
return snaps, err
|
2023-01-06 10:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) Page(page, size int, opts ...DBOption) (int64, []model.Snapshot, error) {
|
|
|
|
var users []model.Snapshot
|
|
|
|
db := global.DB.Model(&model.Snapshot{})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-09 14:55:10 +00:00
|
|
|
func (u *SnapshotRepo) Create(Snapshot *model.Snapshot) error {
|
|
|
|
return global.DB.Create(Snapshot).Error
|
2023-01-06 10:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) Update(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.Snapshot{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) Delete(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.Snapshot{}).Error
|
|
|
|
}
|
2023-08-23 09:16:18 +00:00
|
|
|
|
|
|
|
func (u *SnapshotRepo) GetStatus(snapID uint) (model.SnapshotStatus, error) {
|
|
|
|
var data model.SnapshotStatus
|
|
|
|
if err := global.DB.Where("snap_id = ?", snapID).First(&data).Error; err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) GetStatusList(opts ...DBOption) ([]model.SnapshotStatus, error) {
|
|
|
|
var status []model.SnapshotStatus
|
|
|
|
db := global.DB.Model(&model.SnapshotStatus{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&status).Error
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) CreateStatus(snap *model.SnapshotStatus) error {
|
|
|
|
return global.DB.Create(snap).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) DeleteStatus(snapID uint) error {
|
|
|
|
return global.DB.Where("snap_id = ?", snapID).Delete(&model.SnapshotStatus{}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SnapshotRepo) UpdateStatus(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.SnapshotStatus{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|