2023-03-29 06:58:28 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-23 09:16:18 +00:00
|
|
|
|
2023-03-29 06:58:28 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-08-23 09:16:18 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-04-02 08:54:00 +00:00
|
|
|
"gorm.io/gorm"
|
2023-03-29 06:58:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RuntimeRepo struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type IRuntimeRepo interface {
|
2023-04-03 09:47:23 +00:00
|
|
|
WithName(name string) DBOption
|
|
|
|
WithImage(image string) DBOption
|
|
|
|
WithNotId(id uint) DBOption
|
2023-04-05 16:09:58 +00:00
|
|
|
WithStatus(status string) DBOption
|
2023-05-30 13:54:58 +00:00
|
|
|
WithDetailId(id uint) DBOption
|
2023-09-28 07:28:21 +00:00
|
|
|
WithPort(port int) DBOption
|
2023-03-29 06:58:28 +00:00
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.Runtime, error)
|
|
|
|
Create(ctx context.Context, runtime *model.Runtime) error
|
|
|
|
Save(runtime *model.Runtime) error
|
|
|
|
DeleteBy(opts ...DBOption) error
|
2023-03-31 06:02:28 +00:00
|
|
|
GetFirst(opts ...DBOption) (*model.Runtime, error)
|
2023-08-23 09:16:18 +00:00
|
|
|
List(opts ...DBOption) ([]model.Runtime, error)
|
2023-03-29 06:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIRunTimeRepo() IRuntimeRepo {
|
|
|
|
return &RuntimeRepo{}
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:47:23 +00:00
|
|
|
func (r *RuntimeRepo) WithName(name string) DBOption {
|
2023-04-02 08:54:00 +00:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-04-03 09:47:23 +00:00
|
|
|
return g.Where("name = ?", name)
|
2023-04-02 08:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:09:58 +00:00
|
|
|
func (r *RuntimeRepo) WithStatus(status string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:47:23 +00:00
|
|
|
func (r *RuntimeRepo) WithImage(image string) DBOption {
|
2023-04-02 08:54:00 +00:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-04-03 09:47:23 +00:00
|
|
|
return g.Where("image = ?", image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:54:58 +00:00
|
|
|
func (r *RuntimeRepo) WithDetailId(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("app_detail_id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:47:23 +00:00
|
|
|
func (r *RuntimeRepo) WithNotId(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id != ?", id)
|
2023-04-02 08:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:28:21 +00:00
|
|
|
func (r *RuntimeRepo) WithPort(port int) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("port = ?", port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 06:58:28 +00:00
|
|
|
func (r *RuntimeRepo) Page(page, size int, opts ...DBOption) (int64, []model.Runtime, error) {
|
|
|
|
var runtimes []model.Runtime
|
|
|
|
db := getDb(opts...).Model(&model.Runtime{})
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Find(&runtimes).Error
|
|
|
|
return count, runtimes, err
|
|
|
|
}
|
|
|
|
|
2023-08-23 09:16:18 +00:00
|
|
|
func (r *RuntimeRepo) List(opts ...DBOption) ([]model.Runtime, error) {
|
|
|
|
var runtimes []model.Runtime
|
|
|
|
db := global.DB.Model(&model.Runtime{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&runtimes).Error
|
|
|
|
return runtimes, err
|
|
|
|
}
|
|
|
|
|
2023-03-29 06:58:28 +00:00
|
|
|
func (r *RuntimeRepo) Create(ctx context.Context, runtime *model.Runtime) error {
|
|
|
|
db := getTx(ctx).Model(&model.Runtime{})
|
|
|
|
return db.Create(&runtime).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuntimeRepo) Save(runtime *model.Runtime) error {
|
|
|
|
return getDb().Save(&runtime).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuntimeRepo) DeleteBy(opts ...DBOption) error {
|
|
|
|
return getDb(opts...).Delete(&model.Runtime{}).Error
|
|
|
|
}
|
2023-03-31 06:02:28 +00:00
|
|
|
|
|
|
|
func (r *RuntimeRepo) GetFirst(opts ...DBOption) (*model.Runtime, error) {
|
|
|
|
var runtime model.Runtime
|
|
|
|
if err := getDb(opts...).First(&runtime).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &runtime, nil
|
|
|
|
}
|