You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1Panel/backend/app/repo/snapshot.go

59 lines
1.5 KiB

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)
Page(limit, offset int, opts ...DBOption) (int64, []model.Snapshot, error)
Create(snapshot *model.Snapshot) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
}
func NewISnapshotRepo() ISnapshotRepo {
return &SnapshotRepo{}
}
type SnapshotRepo struct{}
func (u *SnapshotRepo) Get(opts ...DBOption) (model.Snapshot, error) {
var snapshot model.Snapshot
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&snapshot).Error
return snapshot, err
}
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
}
func (u *SnapshotRepo) Create(snapshot *model.Snapshot) error {
return global.DB.Create(snapshot).Error
}
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
}