forked from 1Panel-dev/1Panel
/
website_domain.go
87 lines (75 loc) · 2.68 KB
/
website_domain.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package repo
import (
"context"
"1Panel/backend/app/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type WebsiteDomainRepo struct {
}
type IWebsiteDomainRepo interface {
WithWebsiteId(websiteId uint) DBOption
WithPort(port int) DBOption
WithDomain(domain string) DBOption
Page(page, size int, opts ...DBOption) (int64, []model.WebsiteDomain, error)
GetFirst(opts ...DBOption) (model.WebsiteDomain, error)
GetBy(opts ...DBOption) ([]model.WebsiteDomain, error)
BatchCreate(ctx context.Context, domains []model.WebsiteDomain) error
Create(ctx context.Context, app *model.WebsiteDomain) error
Save(ctx context.Context, app *model.WebsiteDomain) error
DeleteBy(ctx context.Context, opts ...DBOption) error
}
func NewIWebsiteDomainRepo() IWebsiteDomainRepo {
return &WebsiteDomainRepo{}
}
func (w WebsiteDomainRepo) WithWebsiteId(websiteId uint) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("website_id = ?", websiteId)
}
}
func (w WebsiteDomainRepo) WithPort(port int) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("port = ?", port)
}
}
func (w WebsiteDomainRepo) WithDomain(domain string) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("domain = ?", domain)
}
}
func (w WebsiteDomainRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteDomain, error) {
var domains []model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&domains).Error
return count, domains, err
}
func (w WebsiteDomainRepo) GetFirst(opts ...DBOption) (model.WebsiteDomain, error) {
var domain model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
if err := db.First(&domain).Error; err != nil {
return domain, err
}
return domain, nil
}
func (w WebsiteDomainRepo) GetBy(opts ...DBOption) ([]model.WebsiteDomain, error) {
var domains []model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
if err := db.Find(&domains).Error; err != nil {
return domains, err
}
return domains, nil
}
func (w WebsiteDomainRepo) BatchCreate(ctx context.Context, domains []model.WebsiteDomain) error {
return getTx(ctx).Model(&model.WebsiteDomain{}).Create(&domains).Error
}
func (w WebsiteDomainRepo) Create(ctx context.Context, app *model.WebsiteDomain) error {
return getTx(ctx).Omit(clause.Associations).Create(app).Error
}
func (w WebsiteDomainRepo) Save(ctx context.Context, app *model.WebsiteDomain) error {
return getTx(ctx).Omit(clause.Associations).Save(app).Error
}
func (w WebsiteDomainRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
return getTx(ctx, opts...).Delete(&model.WebsiteDomain{}).Error
}