2022-10-28 17:04:57 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-30 10:34:44 +08:00
|
|
|
|
2022-10-28 17:04:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-11-03 17:06:48 +08:00
|
|
|
"gorm.io/gorm"
|
2022-10-28 17:04:57 +08:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
type IWebsiteRepo interface {
|
|
|
|
WithAppInstallId(appInstallId uint) DBOption
|
|
|
|
WithDomain(domain string) DBOption
|
|
|
|
WithAlias(alias string) DBOption
|
|
|
|
WithWebsiteSSLID(sslId uint) DBOption
|
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.WebSite, error)
|
|
|
|
GetFirst(opts ...DBOption) (model.WebSite, error)
|
|
|
|
GetBy(opts ...DBOption) ([]model.WebSite, error)
|
|
|
|
Save(ctx context.Context, app *model.WebSite) error
|
|
|
|
DeleteBy(ctx context.Context, opts ...DBOption) error
|
|
|
|
Create(ctx context.Context, app *model.WebSite) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIWebsiteRepo() IWebsiteRepo {
|
|
|
|
return &WebSiteRepo{}
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:04:57 +08:00
|
|
|
type WebSiteRepo struct {
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) WithAppInstallId(appInstallId uint) DBOption {
|
2022-11-03 17:06:48 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("app_install_id = ?", appInstallId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) WithDomain(domain string) DBOption {
|
2022-11-30 10:34:44 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("primary_domain = ?", domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) WithAlias(alias string) DBOption {
|
2022-12-04 16:10:48 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("alias = ?", alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) WithWebsiteSSLID(sslId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("web_site_ssl_id = ?", sslId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebSiteRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebSite, error) {
|
2022-10-28 17:04:57 +08:00
|
|
|
var websites []model.WebSite
|
|
|
|
db := getDb(opts...).Model(&model.WebSite{})
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2022-11-25 15:37:24 +08:00
|
|
|
err := db.Debug().Limit(size).Offset(size * (page - 1)).Preload("WebSiteSSL").Find(&websites).Error
|
2022-10-28 17:04:57 +08:00
|
|
|
return count, websites, err
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) GetFirst(opts ...DBOption) (model.WebSite, error) {
|
2022-10-28 17:04:57 +08:00
|
|
|
var website model.WebSite
|
|
|
|
db := getDb(opts...).Model(&model.WebSite{})
|
2022-11-16 10:31:35 +08:00
|
|
|
if err := db.Preload("Domains").First(&website).Error; err != nil {
|
2022-10-28 17:04:57 +08:00
|
|
|
return website, err
|
|
|
|
}
|
|
|
|
return website, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) GetBy(opts ...DBOption) ([]model.WebSite, error) {
|
2022-10-28 17:04:57 +08:00
|
|
|
var websites []model.WebSite
|
|
|
|
db := getDb(opts...).Model(&model.WebSite{})
|
|
|
|
if err := db.Find(&websites).Error; err != nil {
|
|
|
|
return websites, err
|
|
|
|
}
|
|
|
|
return websites, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) Create(ctx context.Context, app *model.WebSite) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
return getTx(ctx).Omit(clause.Associations).Create(app).Error
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) Save(ctx context.Context, app *model.WebSite) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
return getTx(ctx).Omit(clause.Associations).Save(app).Error
|
|
|
|
}
|
2022-11-02 15:19:14 +08:00
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebSiteRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
|
2022-11-02 15:19:14 +08:00
|
|
|
return getTx(ctx, opts...).Delete(&model.WebSite{}).Error
|
|
|
|
}
|