grouping settings

This commit is contained in:
Xhofe
2021-12-28 16:38:56 +08:00
parent 04752f7473
commit 4c00866249
3 changed files with 95 additions and 62 deletions

View File

@ -13,16 +13,33 @@ const (
CONST
)
const (
FRONT = iota
BACK
OTHER
)
type SettingItem struct {
Key string `json:"key" gorm:"primaryKey" binding:"required"`
Value string `json:"value"`
Description string `json:"description"`
Type string `json:"type"`
Group int `json:"group"`
Access int `json:"access"`
Values string `json:"values"`
Version string `json:"version"`
}
var Version = SettingItem{
Key: "version",
Value: conf.GitTag,
Description: "version",
Type: "string",
Access: CONST,
Version: conf.GitTag,
Group: OTHER,
}
func SaveSettings(items []SettingItem) error {
return conf.DB.Save(items).Error
}
@ -31,20 +48,29 @@ func SaveSetting(item SettingItem) error {
return conf.DB.Save(item).Error
}
func GetSettingsPublic() (*[]SettingItem, error) {
func GetSettingsPublic() ([]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("`group` <> ?", 1).Find(&items).Error; err != nil {
if err := conf.DB.Where("`access` <> ?", 1).Find(&items).Error; err != nil {
return nil, err
}
return &items, nil
return items, nil
}
func GetSettings() (*[]SettingItem, error) {
func GetSettingsByGroup(group int) ([]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("`group` = ?", group).Find(&items).Error; err != nil {
return nil, err
}
items = append([]SettingItem{Version}, items...)
return items, nil
}
func GetSettings() ([]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Find(&items).Error; err != nil {
return nil, err
}
return &items, nil
return items, nil
}
func DeleteSetting(key string) error {