1Panel/backend/app/repo/website_domain.go

88 lines
2.7 KiB
Go
Raw Normal View History

2022-10-28 09:04:57 +00:00
package repo
import (
"context"
"github.com/1Panel-dev/1Panel/backend/app/model"
2022-11-02 07:19:14 +00:00
"gorm.io/gorm"
2022-10-28 09:04:57 +00:00
"gorm.io/gorm/clause"
)
2022-12-13 09:20:13 +00:00
type WebsiteDomainRepo struct {
2022-10-28 09:04:57 +00:00
}
2023-03-28 10:00:06 +00:00
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{}
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) WithWebsiteId(websiteId uint) DBOption {
2022-11-02 07:19:14 +00:00
return func(db *gorm.DB) *gorm.DB {
2022-12-13 09:20:13 +00:00
return db.Where("website_id = ?", websiteId)
2022-11-02 07:19:14 +00:00
}
}
2022-11-03 09:06:48 +00:00
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) WithPort(port int) DBOption {
2022-11-03 09:06:48 +00:00
return func(db *gorm.DB) *gorm.DB {
return db.Where("port = ?", port)
}
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) WithDomain(domain string) DBOption {
2022-11-03 09:06:48 +00:00
return func(db *gorm.DB) *gorm.DB {
return db.Where("domain = ?", domain)
}
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteDomain, error) {
var domains []model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
2022-10-28 09:04:57 +00:00
count := int64(0)
db = db.Count(&count)
2023-02-09 08:13:06 +00:00
err := db.Limit(size).Offset(size * (page - 1)).Find(&domains).Error
2022-10-28 09:04:57 +00:00
return count, domains, err
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) GetFirst(opts ...DBOption) (model.WebsiteDomain, error) {
var domain model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
2022-10-28 09:04:57 +00:00
if err := db.First(&domain).Error; err != nil {
return domain, err
}
return domain, nil
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) GetBy(opts ...DBOption) ([]model.WebsiteDomain, error) {
var domains []model.WebsiteDomain
db := getDb(opts...).Model(&model.WebsiteDomain{})
2022-10-28 09:04:57 +00:00
if err := db.Find(&domains).Error; err != nil {
return domains, err
}
return domains, nil
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) BatchCreate(ctx context.Context, domains []model.WebsiteDomain) error {
return getTx(ctx).Model(&model.WebsiteDomain{}).Create(&domains).Error
2022-10-28 09:04:57 +00:00
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) Create(ctx context.Context, app *model.WebsiteDomain) error {
2022-10-28 09:04:57 +00:00
return getTx(ctx).Omit(clause.Associations).Create(app).Error
}
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) Save(ctx context.Context, app *model.WebsiteDomain) error {
2022-10-28 09:04:57 +00:00
return getTx(ctx).Omit(clause.Associations).Save(app).Error
}
2022-11-02 07:19:14 +00:00
2022-12-13 09:20:13 +00:00
func (w WebsiteDomainRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
return getTx(ctx, opts...).Delete(&model.WebsiteDomain{}).Error
2022-11-02 07:19:14 +00:00
}