2022-09-15 02:44:43 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-09-08 10:47:15 +00:00
|
|
|
"gorm.io/gorm"
|
2022-09-15 02:44:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingRepo struct{}
|
|
|
|
|
|
|
|
type ISettingRepo interface {
|
2022-09-08 10:47:15 +00:00
|
|
|
GetList(opts ...DBOption) ([]model.Setting, error)
|
|
|
|
Get(opts ...DBOption) (model.Setting, error)
|
2023-02-02 07:01:37 +00:00
|
|
|
Create(key, value string) error
|
2022-09-15 02:44:43 +00:00
|
|
|
Update(key, value string) error
|
2022-09-08 10:47:15 +00:00
|
|
|
WithByKey(key string) DBOption
|
2022-09-15 02:44:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 10:47:15 +00:00
|
|
|
func NewISettingRepo() ISettingRepo {
|
2022-09-15 02:44:43 +00:00
|
|
|
return &SettingRepo{}
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:47:15 +00:00
|
|
|
func (u *SettingRepo) GetList(opts ...DBOption) ([]model.Setting, error) {
|
2022-09-15 02:44:43 +00:00
|
|
|
var settings []model.Setting
|
|
|
|
db := global.DB.Model(&model.Setting{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.Find(&settings).Error
|
|
|
|
return settings, err
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:01:37 +00:00
|
|
|
func (u *SettingRepo) Create(key, value string) error {
|
|
|
|
setting := &model.Setting{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
return global.DB.Create(setting).Error
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:47:15 +00:00
|
|
|
func (u *SettingRepo) Get(opts ...DBOption) (model.Setting, error) {
|
|
|
|
var settings model.Setting
|
|
|
|
db := global.DB.Model(&model.Setting{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
err := db.First(&settings).Error
|
|
|
|
return settings, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SettingRepo) WithByKey(key string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("key = ?", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 02:44:43 +00:00
|
|
|
func (u *SettingRepo) Update(key, value string) error {
|
|
|
|
return global.DB.Model(&model.Setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
|
|
|
|
}
|