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

83 lines
2.3 KiB
Go
Raw Normal View History

package repo
import (
"context"
"github.com/1Panel-dev/1Panel/backend/app/model"
"gorm.io/gorm"
2023-05-16 17:31:47 +08:00
"gorm.io/gorm/clause"
)
type AppDetailRepo struct {
}
2023-03-28 18:00:06 +08:00
type IAppDetailRepo interface {
WithVersion(version string) DBOption
WithAppId(id uint) DBOption
WithIgnored() DBOption
2023-03-28 18:00:06 +08:00
GetFirst(opts ...DBOption) (model.AppDetail, error)
Update(ctx context.Context, detail model.AppDetail) error
BatchCreate(ctx context.Context, details []model.AppDetail) error
DeleteByAppIds(ctx context.Context, appIds []uint) error
GetBy(opts ...DBOption) ([]model.AppDetail, error)
BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error
2023-05-16 17:31:47 +08:00
BatchDelete(ctx context.Context, appDetails []model.AppDetail) error
2023-03-28 18:00:06 +08:00
}
func NewIAppDetailRepo() IAppDetailRepo {
return &AppDetailRepo{}
}
2022-09-23 16:33:55 +08:00
func (a AppDetailRepo) WithVersion(version string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("version = ?", version)
}
}
2022-09-23 16:33:55 +08:00
func (a AppDetailRepo) WithAppId(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("app_id = ?", id)
}
}
func (a AppDetailRepo) WithIgnored() DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("ignore_upgrade = 1")
}
}
2022-10-03 17:35:39 +08:00
func (a AppDetailRepo) GetFirst(opts ...DBOption) (model.AppDetail, error) {
2022-09-23 16:33:55 +08:00
var detail model.AppDetail
err := getDb(opts...).Model(&model.AppDetail{}).Find(&detail).Error
2022-09-23 16:33:55 +08:00
return detail, err
}
2022-09-30 17:56:06 +08:00
func (a AppDetailRepo) Update(ctx context.Context, detail model.AppDetail) error {
return getTx(ctx).Save(&detail).Error
2022-09-30 17:56:06 +08:00
}
func (a AppDetailRepo) BatchCreate(ctx context.Context, details []model.AppDetail) error {
return getTx(ctx).Model(&model.AppDetail{}).Create(&details).Error
}
func (a AppDetailRepo) DeleteByAppIds(ctx context.Context, appIds []uint) error {
return getTx(ctx).Where("app_id in (?)", appIds).Delete(&model.AppDetail{}).Error
}
2022-10-03 17:35:39 +08:00
func (a AppDetailRepo) GetBy(opts ...DBOption) ([]model.AppDetail, error) {
var details []model.AppDetail
err := getDb(opts...).Find(&details).Error
2022-10-03 17:35:39 +08:00
return details, err
}
func (a AppDetailRepo) BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error {
db := getDb(opts...).Model(&model.AppDetail{})
if len(opts) == 0 {
db = db.Where("1=1")
}
return db.Updates(&maps).Error
}
2023-05-16 17:31:47 +08:00
func (a AppDetailRepo) BatchDelete(ctx context.Context, appDetails []model.AppDetail) error {
return getTx(ctx).Omit(clause.Associations).Delete(&appDetails).Error
}