2022-10-09 23:35:24 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-10-09 23:35:24 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DatabaseRepo struct {
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:10:53 +08:00
|
|
|
func (d DatabaseRepo) ByAppInstallId(installId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("app_install_id = ?", installId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:15:11 +08:00
|
|
|
func (d DatabaseRepo) Create(ctx context.Context, database *model.AppDatabase) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
return getTx(ctx).Model(&model.AppDatabase{}).Create(&database).Error
|
2022-10-09 23:35:24 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 15:10:53 +08:00
|
|
|
func (d DatabaseRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
return getTx(ctx, opts...).Model(&model.AppDatabase{}).Delete(&model.AppDatabase{}).Error
|
2022-10-10 15:10:53 +08:00
|
|
|
}
|
2022-10-09 23:35:24 +08:00
|
|
|
|
2022-10-20 16:15:11 +08:00
|
|
|
func (d DatabaseRepo) GetBy(opts ...DBOption) ([]model.AppDatabase, error) {
|
2022-10-28 17:04:57 +08:00
|
|
|
db := getDb(opts...)
|
2022-10-20 16:15:11 +08:00
|
|
|
var databases []model.AppDatabase
|
2022-10-10 15:10:53 +08:00
|
|
|
err := db.Find(&databases).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return databases, nil
|
|
|
|
}
|
2022-10-09 23:35:24 +08:00
|
|
|
|
2022-10-20 16:15:11 +08:00
|
|
|
func (d DatabaseRepo) GetFirst(opts ...DBOption) (model.AppDatabase, error) {
|
2022-10-28 17:04:57 +08:00
|
|
|
db := getDb(opts...)
|
2022-10-20 16:15:11 +08:00
|
|
|
var database model.AppDatabase
|
2022-10-10 15:10:53 +08:00
|
|
|
err := db.Find(&database).Error
|
|
|
|
if err != nil {
|
|
|
|
return database, err
|
|
|
|
}
|
|
|
|
return database, nil
|
|
|
|
}
|