2022-10-11 16:27:58 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-29 17:39:10 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-11 16:27:58 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppInstallResourceRpo struct {
|
|
|
|
}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
type IAppInstallResourceRpo interface {
|
|
|
|
WithAppInstallId(appInstallId uint) DBOption
|
|
|
|
WithLinkId(linkId uint) DBOption
|
|
|
|
WithResourceId(resourceId uint) DBOption
|
|
|
|
GetBy(opts ...DBOption) ([]model.AppInstallResource, error)
|
|
|
|
GetFirst(opts ...DBOption) (model.AppInstallResource, error)
|
|
|
|
Create(ctx context.Context, resource *model.AppInstallResource) error
|
|
|
|
DeleteBy(ctx context.Context, opts ...DBOption) error
|
2023-06-12 17:48:11 +08:00
|
|
|
BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error
|
2023-03-28 18:00:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAppInstallResourceRpo() IAppInstallResourceRpo {
|
|
|
|
return &AppInstallResourceRpo{}
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func (a AppInstallResourceRpo) WithAppInstallId(appInstallId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("app_install_id = ?", appInstallId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 19:25:02 +08:00
|
|
|
func (a AppInstallResourceRpo) WithLinkId(linkId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("link_id = ?", linkId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:28:03 +08:00
|
|
|
func (a AppInstallResourceRpo) WithResourceId(resourceId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("resource_id = ?", resourceId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func (a AppInstallResourceRpo) GetBy(opts ...DBOption) ([]model.AppInstallResource, error) {
|
|
|
|
db := global.DB.Model(&model.AppInstallResource{})
|
|
|
|
var resources []model.AppInstallResource
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&resources).Error
|
|
|
|
return resources, err
|
|
|
|
}
|
|
|
|
|
2022-11-29 17:39:10 +08:00
|
|
|
func (a AppInstallResourceRpo) GetFirst(opts ...DBOption) (model.AppInstallResource, error) {
|
|
|
|
db := global.DB.Model(&model.AppInstallResource{})
|
|
|
|
var resources model.AppInstallResource
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&resources).Error
|
|
|
|
return resources, err
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func (a AppInstallResourceRpo) Create(ctx context.Context, resource *model.AppInstallResource) error {
|
|
|
|
db := getTx(ctx).Model(&model.AppInstallResource{})
|
|
|
|
return db.Create(&resource).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppInstallResourceRpo) DeleteBy(ctx context.Context, opts ...DBOption) error {
|
|
|
|
return getTx(ctx, opts...).Delete(&model.AppInstallResource{}).Error
|
|
|
|
}
|
2023-06-12 17:48:11 +08:00
|
|
|
|
|
|
|
func (a *AppInstallResourceRpo) BatchUpdateBy(maps map[string]interface{}, opts ...DBOption) error {
|
|
|
|
db := getDb(opts...).Model(&model.AppInstallResource{})
|
|
|
|
if len(opts) == 0 {
|
|
|
|
db = db.Where("1=1")
|
|
|
|
}
|
|
|
|
return db.Updates(&maps).Error
|
|
|
|
}
|