2022-09-26 16:32:40 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-09 23:35:24 +08:00
|
|
|
"context"
|
2022-09-26 16:32:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/app/model"
|
2022-10-03 17:35:39 +08:00
|
|
|
"gorm.io/gorm"
|
2022-09-26 16:32:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppInstallRepo struct{}
|
|
|
|
|
2022-10-03 17:35:39 +08: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-12 10:54:09 +08:00
|
|
|
|
|
|
|
func (a AppInstallRepo) WithDetailIdNotIn(detailIds []uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_detail_id not in (?)", detailIds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 15:49:39 +08: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 17:35:39 +08:00
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func (a AppInstallRepo) WithServiceName(serviceName string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("service_name = ?", serviceName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:32:40 +08:00
|
|
|
func (a AppInstallRepo) GetBy(opts ...DBOption) ([]model.AppInstall, error) {
|
|
|
|
var install []model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2022-10-12 18:57:22 +08:00
|
|
|
err := db.Preload("App").Preload("Backups").Find(&install).Error
|
2022-09-29 18:16:56 +08:00
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppInstallRepo) GetFirst(opts ...DBOption) (model.AppInstall, error) {
|
|
|
|
var install model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2022-10-12 18:57:22 +08:00
|
|
|
err := db.Preload("App").Preload("Backups").First(&install).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
return install, err
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:35:24 +08:00
|
|
|
func (a AppInstallRepo) Create(ctx context.Context, install *model.AppInstall) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
db := getTx(ctx).Model(&model.AppInstall{})
|
2022-09-26 16:32:40 +08:00
|
|
|
return db.Create(&install).Error
|
|
|
|
}
|
2022-09-26 22:54:38 +08:00
|
|
|
|
2022-10-13 16:46:38 +08:00
|
|
|
func (a AppInstallRepo) Save(install *model.AppInstall) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
return getDb().Save(&install).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
}
|
|
|
|
|
2022-10-09 23:35:24 +08:00
|
|
|
func (a AppInstallRepo) DeleteBy(opts ...DBOption) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
return getDb(opts...).Delete(&model.AppInstall{}).Error
|
2022-09-26 22:54:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 15:10:53 +08:00
|
|
|
func (a AppInstallRepo) Delete(ctx context.Context, install model.AppInstall) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getTx(ctx).Model(&model.AppInstall{})
|
2022-10-09 23:35:24 +08:00
|
|
|
return db.Delete(&install).Error
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:32:40 +08:00
|
|
|
func (a AppInstallRepo) Page(page, size int, opts ...DBOption) (int64, []model.AppInstall, error) {
|
|
|
|
var apps []model.AppInstall
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
2022-09-26 16:32:40 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2022-10-12 18:57:22 +08:00
|
|
|
err := db.Debug().Limit(size).Offset(size * (page - 1)).Preload("App").Preload("Backups").Find(&apps).Error
|
2022-09-26 16:32:40 +08:00
|
|
|
return count, apps, err
|
|
|
|
}
|
2022-10-03 17:35:39 +08:00
|
|
|
|
2022-10-12 10:54:09 +08:00
|
|
|
func (a AppInstallRepo) BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error {
|
|
|
|
|
|
|
|
db := getDb(opts...).Model(&model.AppInstall{})
|
|
|
|
if len(opts) == 0 {
|
|
|
|
db = db.Where("1=1")
|
2022-10-03 17:35:39 +08:00
|
|
|
}
|
2022-10-12 10:54:09 +08:00
|
|
|
return db.Updates(&maps).Error
|
2022-10-03 17:35:39 +08:00
|
|
|
}
|