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

99 lines
3.1 KiB
Go
Raw Normal View History

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