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

45 lines
1.3 KiB
Go
Raw Normal View History

2022-10-28 17:04:57 +08:00
package repo
import (
"github.com/1Panel-dev/1Panel/backend/app/model"
2022-11-25 18:59:49 +08:00
"github.com/1Panel-dev/1Panel/backend/global"
2022-10-28 17:04:57 +08:00
"gorm.io/gorm/clause"
)
2022-12-13 17:20:13 +08:00
type WebsiteGroupRepo struct {
2022-10-28 17:04:57 +08:00
}
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteGroup, error) {
var groups []model.WebsiteGroup
db := getDb(opts...).Model(&model.WebsiteGroup{})
2022-10-28 17:04:57 +08:00
count := int64(0)
db = db.Count(&count)
2022-11-25 18:59:49 +08:00
err := db.Limit(size).Offset(size * (page - 1)).Order("`default` desc").Find(&groups).Error
2022-10-28 17:04:57 +08:00
return count, groups, err
}
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) GetBy(opts ...DBOption) ([]model.WebsiteGroup, error) {
var groups []model.WebsiteGroup
db := getDb(opts...).Model(&model.WebsiteGroup{})
2022-11-25 18:59:49 +08:00
if err := db.Order("`default` desc").Find(&groups).Error; err != nil {
2022-10-28 17:04:57 +08:00
return groups, err
}
return groups, nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) Create(app *model.WebsiteGroup) error {
2022-10-28 17:04:57 +08:00
return getDb().Omit(clause.Associations).Create(app).Error
}
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) Save(app *model.WebsiteGroup) error {
2022-10-28 17:04:57 +08:00
return getDb().Omit(clause.Associations).Save(app).Error
}
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) DeleteBy(opts ...DBOption) error {
return getDb(opts...).Delete(&model.WebsiteGroup{}).Error
2022-10-28 17:04:57 +08:00
}
2022-11-25 18:59:49 +08:00
2022-12-13 17:20:13 +08:00
func (w WebsiteGroupRepo) CancelDefault() error {
return global.DB.Model(&model.WebsiteGroup{}).Where("`default` = 1").Updates(map[string]interface{}{"default": 0}).Error
2022-11-25 18:59:49 +08:00
}