2022-09-26 08:32:40 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-09 15:35:24 +00:00
|
|
|
"context"
|
2022-09-26 08:32:40 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/global"
|
2022-10-03 09:35:39 +00:00
|
|
|
"gorm.io/gorm"
|
2022-09-26 08:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppInstallRepo struct{}
|
|
|
|
|
2022-10-03 09:35:39 +00:00
|
|
|
func (a AppInstallRepo) WithDetailIdsIn(detailIds []uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_detail_id in (?)", detailIds)
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 07:49:39 +00:00
|
|
|
func (a AppInstallRepo) WithAppId(appId uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_id = ?", appId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (a AppInstallRepo) WithStatus(status string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 09:35:39 +00:00
|
|
|
|
2022-09-26 08:32:40 +00:00
|
|
|
func (a AppInstallRepo) GetBy(opts ...DBOption) ([]model.AppInstall, error) {
|
|
|
|
db := global.DB.Model(&model.AppInstall{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
var install []model.AppInstall
|
2022-09-29 10:16:56 +00:00
|
|
|
err := db.Preload("App").Preload("Containers").Find(&install).Error
|
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppInstallRepo) GetFirst(opts ...DBOption) (model.AppInstall, error) {
|
|
|
|
db := global.DB.Model(&model.AppInstall{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
var install model.AppInstall
|
|
|
|
err := db.Preload("App").Preload("Containers").First(&install).Error
|
2022-09-26 08:32:40 +00:00
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:35:24 +00:00
|
|
|
func (a AppInstallRepo) Create(ctx context.Context, install *model.AppInstall) error {
|
|
|
|
db := ctx.Value("db").(*gorm.DB).Model(&model.AppInstall{})
|
2022-09-26 08:32:40 +00:00
|
|
|
return db.Create(&install).Error
|
|
|
|
}
|
2022-09-26 14:54:38 +00:00
|
|
|
|
2022-09-26 08:32:40 +00:00
|
|
|
func (a AppInstallRepo) Save(install model.AppInstall) error {
|
2022-09-27 08:57:23 +00:00
|
|
|
db := global.DB
|
2022-09-26 08:32:40 +00:00
|
|
|
return db.Save(&install).Error
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:35:24 +00:00
|
|
|
func (a AppInstallRepo) DeleteBy(opts ...DBOption) error {
|
2022-09-26 14:54:38 +00:00
|
|
|
db := global.DB.Model(&model.AppInstall{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.AppInstall{}).Error
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:35:24 +00:00
|
|
|
func (a AppInstallRepo) Delete(install model.AppInstall) error {
|
|
|
|
db := global.DB
|
|
|
|
return db.Delete(&install).Error
|
|
|
|
}
|
|
|
|
|
2022-09-26 08:32:40 +00:00
|
|
|
func (a AppInstallRepo) Page(page, size int, opts ...DBOption) (int64, []model.AppInstall, error) {
|
|
|
|
var apps []model.AppInstall
|
|
|
|
db := global.DB.Model(&model.AppInstall{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2022-09-29 10:16:56 +00:00
|
|
|
err := db.Debug().Limit(size).Offset(size * (page - 1)).Preload("App").Preload("Containers").Find(&apps).Error
|
2022-09-26 08:32:40 +00:00
|
|
|
return count, apps, err
|
|
|
|
}
|
2022-10-03 09:35:39 +00:00
|
|
|
|
|
|
|
func (a AppInstallRepo) BatchUpdateBy(update model.AppInstall, opts ...DBOption) error {
|
|
|
|
db := global.DB.Model(model.AppInstall{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Updates(update).Error
|
|
|
|
}
|