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

72 lines
1.8 KiB
Go
Raw Normal View History

2022-10-09 16:17:15 +08:00
package repo
import (
2022-10-18 18:39:45 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
2022-10-09 16:17:15 +08:00
)
type ImageRepoRepo struct{}
type IImageRepoRepo interface {
Get(opts ...DBOption) (model.ImageRepo, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.ImageRepo, error)
List(opts ...DBOption) ([]model.ImageRepo, error)
2022-10-09 16:17:15 +08:00
Create(imageRepo *model.ImageRepo) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
}
func NewIImageRepoRepo() IImageRepoRepo {
return &ImageRepoRepo{}
}
func (u *ImageRepoRepo) Get(opts ...DBOption) (model.ImageRepo, error) {
var imageRepo model.ImageRepo
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&imageRepo).Error
return imageRepo, err
}
func (u *ImageRepoRepo) Page(page, size int, opts ...DBOption) (int64, []model.ImageRepo, error) {
var ops []model.ImageRepo
db := global.DB.Model(&model.ImageRepo{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&ops).Error
return count, ops, err
}
func (u *ImageRepoRepo) List(opts ...DBOption) ([]model.ImageRepo, error) {
var ops []model.ImageRepo
db := global.DB.Model(&model.ImageRepo{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Find(&ops).Error
return ops, err
}
2022-10-09 16:17:15 +08:00
func (u *ImageRepoRepo) Create(imageRepo *model.ImageRepo) error {
return global.DB.Create(imageRepo).Error
}
func (u *ImageRepoRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.ImageRepo{}).Where("id = ?", id).Updates(vars).Error
}
func (u *ImageRepoRepo) Delete(opts ...DBOption) error {
db := global.DB
for _, opt := range opts {
db = opt(db)
}
return db.Delete(&model.ImageRepo{}).Error
}