2022-11-11 17:41:39 +08:00
|
|
|
package repo
|
|
|
|
|
2022-11-16 10:31:35 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2022-11-11 17:41:39 +08:00
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func NewISSLRepo() ISSLRepo {
|
|
|
|
return &WebsiteSSLRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ISSLRepo interface {
|
|
|
|
WithByAlias(alias string) DBOption
|
|
|
|
WithByAcmeAccountId(acmeAccountId uint) DBOption
|
|
|
|
WithByDnsAccountId(dnsAccountId uint) DBOption
|
2022-12-13 17:20:13 +08:00
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.WebsiteSSL, error)
|
|
|
|
GetFirst(opts ...DBOption) (model.WebsiteSSL, error)
|
|
|
|
List(opts ...DBOption) ([]model.WebsiteSSL, error)
|
|
|
|
Create(ctx context.Context, ssl *model.WebsiteSSL) error
|
|
|
|
Save(ssl model.WebsiteSSL) error
|
2022-12-05 15:50:53 +08:00
|
|
|
DeleteBy(opts ...DBOption) error
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:39 +08:00
|
|
|
type WebsiteSSLRepo struct {
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w WebsiteSSLRepo) WithByAlias(alias string) DBOption {
|
2022-11-16 10:31:35 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("alias = ?", alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w WebsiteSSLRepo) WithByAcmeAccountId(acmeAccountId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("acme_account_id = ?", acmeAccountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteSSLRepo) WithByDnsAccountId(dnsAccountId uint) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("dns_account_id = ?", dnsAccountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteSSL, error) {
|
|
|
|
var sslList []model.WebsiteSSL
|
|
|
|
db := getDb(opts...).Model(&model.WebsiteSSL{})
|
2022-11-11 17:41:39 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-01-04 14:26:22 +08:00
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Preload("AcmeAccount").Preload("Websites").Find(&sslList).Error
|
2022-11-11 17:41:39 +08:00
|
|
|
return count, sslList, err
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLRepo) GetFirst(opts ...DBOption) (model.WebsiteSSL, error) {
|
|
|
|
var website model.WebsiteSSL
|
|
|
|
db := getDb(opts...).Model(&model.WebsiteSSL{})
|
2022-12-05 15:50:53 +08:00
|
|
|
if err := db.Preload("AcmeAccount").First(&website).Error; err != nil {
|
2022-11-16 10:31:35 +08:00
|
|
|
return website, err
|
|
|
|
}
|
|
|
|
return website, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLRepo) List(opts ...DBOption) ([]model.WebsiteSSL, error) {
|
|
|
|
var websites []model.WebsiteSSL
|
|
|
|
db := getDb(opts...).Model(&model.WebsiteSSL{})
|
2022-12-05 15:50:53 +08:00
|
|
|
if err := db.Preload("AcmeAccount").Find(&websites).Error; err != nil {
|
2022-11-20 18:32:56 +08:00
|
|
|
return websites, err
|
|
|
|
}
|
|
|
|
return websites, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLRepo) Create(ctx context.Context, ssl *model.WebsiteSSL) error {
|
2022-11-16 10:31:35 +08:00
|
|
|
return getTx(ctx).Create(ssl).Error
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLRepo) Save(ssl model.WebsiteSSL) error {
|
2022-11-11 17:41:39 +08:00
|
|
|
return getDb().Save(&ssl).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteSSLRepo) DeleteBy(opts ...DBOption) error {
|
2022-12-13 17:20:13 +08:00
|
|
|
return getDb(opts...).Delete(&model.WebsiteSSL{}).Error
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|