2022-08-30 18:49:07 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-08-31 23:16:10 +08:00
|
|
|
"gorm.io/gorm"
|
2022-08-30 18:49:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type GroupRepo struct{}
|
|
|
|
|
|
|
|
type IGroupRepo interface {
|
|
|
|
Get(opts ...DBOption) (model.Group, error)
|
|
|
|
GetList(opts ...DBOption) ([]model.Group, error)
|
2022-08-31 23:16:10 +08:00
|
|
|
WithByType(groupType string) DBOption
|
2022-08-30 18:49:07 +08:00
|
|
|
Create(group *model.Group) error
|
|
|
|
Update(id uint, vars map[string]interface{}) error
|
|
|
|
Delete(opts ...DBOption) error
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:40:00 +08:00
|
|
|
func NewIGroupRepo() IGroupRepo {
|
2022-08-30 18:49:07 +08:00
|
|
|
return &GroupRepo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *GroupRepo) Get(opts ...DBOption) (model.Group, error) {
|
|
|
|
var group model.Group
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&group).Error
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *GroupRepo) GetList(opts ...DBOption) ([]model.Group, error) {
|
|
|
|
var groups []model.Group
|
|
|
|
db := global.DB.Model(&model.Group{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&groups).Error
|
|
|
|
return groups, err
|
|
|
|
}
|
|
|
|
|
2022-08-31 23:16:10 +08:00
|
|
|
func (c *GroupRepo) WithByType(groupType string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("type = ?", groupType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
func (u *GroupRepo) Create(group *model.Group) error {
|
|
|
|
return global.DB.Create(group).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *GroupRepo) Update(id uint, vars map[string]interface{}) error {
|
|
|
|
return global.DB.Model(&model.Group{}).Where("id = ?", id).Updates(vars).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *GroupRepo) Delete(opts ...DBOption) error {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db.Delete(&model.Group{}).Error
|
|
|
|
}
|