/
website_acme_account.go
64 lines (54 loc) · 1.88 KB
/
website_acme_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package repo
import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"gorm.io/gorm"
)
type IAcmeAccountRepo interface {
Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error)
GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error)
Create(account model.WebsiteAcmeAccount) error
Save(account model.WebsiteAcmeAccount) error
DeleteBy(opts ...DBOption) error
WithEmail(email string) DBOption
WithType(acType string) DBOption
}
func NewIAcmeAccountRepo() IAcmeAccountRepo {
return &WebsiteAcmeAccountRepo{}
}
type WebsiteAcmeAccountRepo struct {
}
func (w *WebsiteAcmeAccountRepo) WithEmail(email string) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("email = ?", email)
}
}
func (w *WebsiteAcmeAccountRepo) WithType(acType string) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("type = ?", acType)
}
}
func (w *WebsiteAcmeAccountRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error) {
var accounts []model.WebsiteAcmeAccount
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&accounts).Error
return count, accounts, err
}
func (w *WebsiteAcmeAccountRepo) GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error) {
var account model.WebsiteAcmeAccount
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
if err := db.First(&account).Error; err != nil {
return nil, err
}
return &account, nil
}
func (w *WebsiteAcmeAccountRepo) Create(account model.WebsiteAcmeAccount) error {
return getDb().Create(&account).Error
}
func (w *WebsiteAcmeAccountRepo) Save(account model.WebsiteAcmeAccount) error {
return getDb().Save(&account).Error
}
func (w *WebsiteAcmeAccountRepo) DeleteBy(opts ...DBOption) error {
return getDb(opts...).Debug().Delete(&model.WebsiteAcmeAccount{}).Error
}