2022-11-11 17:41:39 +08:00
|
|
|
package repo
|
|
|
|
|
2023-02-20 17:04:59 +08:00
|
|
|
import (
|
|
|
|
"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
|
|
|
type IAcmeAccountRepo interface {
|
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error)
|
2023-02-20 17:04:59 +08:00
|
|
|
GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error)
|
2022-12-05 15:50:53 +08:00
|
|
|
Create(account model.WebsiteAcmeAccount) error
|
|
|
|
Save(account model.WebsiteAcmeAccount) error
|
|
|
|
DeleteBy(opts ...DBOption) error
|
2023-02-20 17:04:59 +08:00
|
|
|
WithEmail(email string) DBOption
|
2023-12-15 15:48:09 +08:00
|
|
|
WithType(acType string) DBOption
|
2022-12-05 15:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAcmeAccountRepo() IAcmeAccountRepo {
|
|
|
|
return &WebsiteAcmeAccountRepo{}
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:41:39 +08:00
|
|
|
type WebsiteAcmeAccountRepo struct {
|
|
|
|
}
|
|
|
|
|
2023-02-20 17:04:59 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) WithEmail(email string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("email = ?", email)
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 15:48:09 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) WithType(acType string) DBOption {
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("type = ?", acType)
|
|
|
|
}
|
|
|
|
}
|
2023-02-20 17:04:59 +08:00
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error) {
|
2022-11-11 17:41:39 +08:00
|
|
|
var accounts []model.WebsiteAcmeAccount
|
|
|
|
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
|
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
2023-02-09 16:13:06 +08:00
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Find(&accounts).Error
|
2022-11-11 17:41:39 +08:00
|
|
|
return count, accounts, err
|
|
|
|
}
|
|
|
|
|
2023-02-20 17:04:59 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error) {
|
2022-11-16 10:31:35 +08:00
|
|
|
var account model.WebsiteAcmeAccount
|
|
|
|
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
|
|
|
|
if err := db.First(&account).Error; err != nil {
|
2023-02-20 17:04:59 +08:00
|
|
|
return nil, err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
2023-02-20 17:04:59 +08:00
|
|
|
return &account, nil
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) Create(account model.WebsiteAcmeAccount) error {
|
2022-11-11 17:41:39 +08:00
|
|
|
return getDb().Create(&account).Error
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) Save(account model.WebsiteAcmeAccount) error {
|
2022-11-11 17:41:39 +08:00
|
|
|
return getDb().Save(&account).Error
|
|
|
|
}
|
|
|
|
|
2022-12-05 15:50:53 +08:00
|
|
|
func (w *WebsiteAcmeAccountRepo) DeleteBy(opts ...DBOption) error {
|
2022-11-11 17:41:39 +08:00
|
|
|
return getDb(opts...).Debug().Delete(&model.WebsiteAcmeAccount{}).Error
|
|
|
|
}
|