1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-20 00:39:17 +08:00
1Panel/backend/app/repo/app_detail.go

52 lines
1.3 KiB
Go
Raw Normal View History

package repo
import (
"context"
"github.com/1Panel-dev/1Panel/app/model"
2022-09-23 16:33:55 +08:00
"github.com/1Panel-dev/1Panel/global"
"gorm.io/gorm"
)
type AppDetailRepo struct {
}
2022-09-23 16:33:55 +08:00
func (a AppDetailRepo) WithVersion(version string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("version = ?", version)
}
}
func (a AppDetailRepo) WithAppId(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("app_id = ?", id)
}
}
func (a AppDetailRepo) GetAppDetail(opts ...DBOption) (model.AppDetail, error) {
var detail model.AppDetail
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&detail).Error
return detail, err
}
func (a AppDetailRepo) BatchCreate(ctx context.Context, details []*model.AppDetail) error {
db := ctx.Value("db").(*gorm.DB)
return db.Model(&model.AppDetail{}).Create(&details).Error
}
func (a AppDetailRepo) DeleteByAppIds(ctx context.Context, appIds []uint) error {
db := ctx.Value("db").(*gorm.DB)
return db.Where("app_id in (?)", appIds).Delete(&model.AppDetail{}).Error
}
2022-09-23 16:33:55 +08:00
func (a AppDetailRepo) GetByAppId(ctx context.Context, appId uint) ([]model.AppDetail, error) {
db := ctx.Value("db").(*gorm.DB)
var details []model.AppDetail
if err := db.Where("app_id = ?", appId).Find(&details).Error; err != nil {
return nil, err
}
return details, nil
}