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"
|
2023-03-02 13:54:07 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
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
|
2022-12-30 16:13:13 +08:00
|
|
|
WithGroupID(groupId uint) DBOption
|
2023-01-03 16:56:36 +08:00
|
|
|
WithDefaultServer() DBOption
|
2023-03-02 14:56:06 +08:00
|
|
|
WithDomainLike(domain string) DBOption
|
2023-04-06 00:09:58 +08:00
|
|
|
WithRuntimeID(runtimeID uint) DBOption
|
2024-03-15 15:38:08 +08:00
|
|
|
WithIDs(ids []uint) DBOption
|
2022-12-13 17:20:13 +08:00
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.Website, error)
|
2022-12-30 15:23:29 +08:00
|
|
|
List(opts ...DBOption) ([]model.Website, error)
|
2022-12-13 17:20:13 +08:00
|
|
|
GetFirst(opts ...DBOption) (model.Website, error)
|
|
|
|
GetBy(opts ...DBOption) ([]model.Website, error)
|
|
|
|
Save(ctx context.Context, app *model.Website) error
|
2023-03-02 13:54:07 +08:00
|
|
|
SaveWithoutCtx(app *model.Website) error
|
2022-12-05 15:50:53 +08:00
|
|
|
DeleteBy(ctx context.Context, opts ...DBOption) error
|
2022-12-13 17:20:13 +08:00
|
|
|
Create(ctx context.Context, app *model.Website) error
|
2023-08-23 10:42:13 +08:00
|
|
|
DeleteAll(ctx context.Context) error
|
2022-12-05 15:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIWebsiteRepo() IWebsiteRepo {
|
2022-12-13 17:20:13 +08:00
|
|
|
return &WebsiteRepo{}
|
2022-12-05 15:50:53 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
type WebsiteRepo struct {
|
2022-10-28 17:04:57 +08:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:38:08 +08:00
|
|
|
func (w *WebsiteRepo) WithAppInstallId(appInstallID uint) DBOption {
|
2022-11-03 17:06:48 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
2024-03-15 15:38:08 +08:00
|
|
|
return db.Where("app_install_id = ?", appInstallID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebsiteRepo) WithIDs(ids []uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("id in (?)", ids)
|
2022-11-03 17:06:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 00:09:58 +08:00
|
|
|
func (w *WebsiteRepo) WithRuntimeID(runtimeID uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("runtime_id = ?", runtimeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:56:06 +08:00
|
|
|
func (w *WebsiteRepo) WithDomainLike(domain string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("primary_domain like ?", "%"+domain+"%")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +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-13 17:20:13 +08:00
|
|
|
func (w *WebsiteRepo) WithWebsiteSSLID(sslId uint) DBOption {
|
2022-12-05 15:50:53 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
2022-12-30 16:13:13 +08:00
|
|
|
return db.Where("website_ssl_id = ?", sslId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebsiteRepo) WithGroupID(groupId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("website_group_id = ?", groupId)
|
2022-12-05 15:50:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:56:36 +08:00
|
|
|
func (w *WebsiteRepo) WithDefaultServer() DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("default_server = 1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w *WebsiteRepo) Page(page, size int, opts ...DBOption) (int64, []model.Website, error) {
|
|
|
|
var websites []model.Website
|
|
|
|
db := getDb(opts...).Model(&model.Website{})
|
2022-10-28 17:04:57 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-02-09 16:13:06 +08:00
|
|
|
err := db.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-30 15:23:29 +08:00
|
|
|
func (w *WebsiteRepo) List(opts ...DBOption) ([]model.Website, error) {
|
|
|
|
var websites []model.Website
|
2023-11-29 20:24:08 +08:00
|
|
|
err := getDb(opts...).Model(&model.Website{}).Preload("Domains").Preload("WebsiteSSL").Find(&websites).Error
|
2022-12-30 15:23:29 +08:00
|
|
|
return websites, err
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w *WebsiteRepo) GetFirst(opts ...DBOption) (model.Website, error) {
|
|
|
|
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-13 17:20:13 +08:00
|
|
|
func (w *WebsiteRepo) GetBy(opts ...DBOption) ([]model.Website, error) {
|
|
|
|
var websites []model.Website
|
|
|
|
db := getDb(opts...).Model(&model.Website{})
|
2022-10-28 17:04:57 +08:00
|
|
|
if err := db.Find(&websites).Error; err != nil {
|
|
|
|
return websites, err
|
|
|
|
}
|
|
|
|
return websites, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +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-13 17:20:13 +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
|
|
|
|
2023-03-02 13:54:07 +08:00
|
|
|
func (w *WebsiteRepo) SaveWithoutCtx(website *model.Website) error {
|
|
|
|
return global.DB.Save(website).Error
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w *WebsiteRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
|
|
|
|
return getTx(ctx, opts...).Delete(&model.Website{}).Error
|
2022-11-02 15:19:14 +08:00
|
|
|
}
|
2023-08-23 10:42:13 +08:00
|
|
|
|
|
|
|
func (w *WebsiteRepo) DeleteAll(ctx context.Context) error {
|
|
|
|
return getTx(ctx).Where("1 = 1 ").Delete(&model.Website{}).Error
|
|
|
|
}
|