2022-09-22 16:16:04 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-21 19:06:24 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-09-22 16:16:04 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppRepo struct {
|
|
|
|
}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
type IAppRepo interface {
|
|
|
|
WithKey(key string) DBOption
|
|
|
|
WithType(typeStr string) DBOption
|
|
|
|
OrderByRecommend() DBOption
|
|
|
|
GetRecommend() DBOption
|
2023-04-08 14:02:14 +08:00
|
|
|
WithResource(resource string) DBOption
|
2023-03-28 18:00:06 +08:00
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.App, error)
|
|
|
|
GetFirst(opts ...DBOption) (model.App, error)
|
|
|
|
GetBy(opts ...DBOption) ([]model.App, error)
|
|
|
|
BatchCreate(ctx context.Context, apps []model.App) error
|
|
|
|
GetByKey(ctx context.Context, key string) (model.App, error)
|
|
|
|
Create(ctx context.Context, app *model.App) error
|
|
|
|
Save(ctx context.Context, app *model.App) error
|
2023-05-15 22:40:05 +08:00
|
|
|
BatchDelete(ctx context.Context, apps []model.App) error
|
2023-03-28 18:00:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAppRepo() IAppRepo {
|
|
|
|
return &AppRepo{}
|
|
|
|
}
|
|
|
|
|
2022-10-07 15:49:39 +08:00
|
|
|
func (a AppRepo) WithKey(key string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("key = ?", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:04:57 +08:00
|
|
|
func (a AppRepo) WithType(typeStr string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("type = ?", typeStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 16:21:17 +08:00
|
|
|
func (a AppRepo) OrderByRecommend() DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Order("recommend asc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppRepo) GetRecommend() DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("recommend < 9999")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 14:02:14 +08:00
|
|
|
func (a AppRepo) WithResource(resource string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("resource = ?", resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 16:16:04 +08:00
|
|
|
func (a AppRepo) Page(page, size int, opts ...DBOption) (int64, []model.App, error) {
|
|
|
|
var apps []model.App
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.App{})
|
2022-09-22 16:16:04 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-03-30 16:47:47 +08:00
|
|
|
err := db.Debug().Limit(size).Offset(size * (page - 1)).Preload("AppTags").Find(&apps).Error
|
2022-09-22 16:16:04 +08:00
|
|
|
return count, apps, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:33:55 +08:00
|
|
|
func (a AppRepo) GetFirst(opts ...DBOption) (model.App, error) {
|
|
|
|
var app model.App
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.App{})
|
2022-10-09 23:35:24 +08:00
|
|
|
if err := db.Preload("AppTags").First(&app).Error; err != nil {
|
2022-09-23 16:33:55 +08:00
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
2022-09-30 17:56:06 +08:00
|
|
|
func (a AppRepo) GetBy(opts ...DBOption) ([]model.App, error) {
|
|
|
|
var apps []model.App
|
2022-10-12 10:54:09 +08:00
|
|
|
db := getDb(opts...).Model(&model.App{})
|
2022-09-30 17:56:06 +08:00
|
|
|
if err := db.Preload("Details").Preload("AppTags").Find(&apps).Error; err != nil {
|
|
|
|
return apps, err
|
|
|
|
}
|
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppRepo) BatchCreate(ctx context.Context, apps []model.App) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
return getTx(ctx).Omit(clause.Associations).Create(&apps).Error
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppRepo) GetByKey(ctx context.Context, key string) (model.App, error) {
|
|
|
|
var app model.App
|
2022-10-12 10:54:09 +08:00
|
|
|
if err := getTx(ctx).Where("key = ?", key).First(&app).Error; err != nil {
|
2022-09-22 16:16:04 +08:00
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppRepo) Create(ctx context.Context, app *model.App) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
return getTx(ctx).Omit(clause.Associations).Create(app).Error
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppRepo) Save(ctx context.Context, app *model.App) error {
|
2022-10-12 10:54:09 +08:00
|
|
|
return getTx(ctx).Omit(clause.Associations).Save(app).Error
|
2022-09-22 16:16:04 +08:00
|
|
|
}
|
2023-05-15 22:40:05 +08:00
|
|
|
|
|
|
|
func (a AppRepo) BatchDelete(ctx context.Context, apps []model.App) error {
|
|
|
|
return getTx(ctx).Omit(clause.Associations).Delete(&apps).Error
|
|
|
|
}
|