alist/internal/db/settingitem.go

70 lines
2.1 KiB
Go
Raw Normal View History

2022-06-27 09:25:19 +00:00
package db
import (
"fmt"
2022-08-03 06:26:59 +00:00
2022-06-27 09:25:19 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
)
func GetSettingItems() ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Find(&settingItems).Error; err != nil {
return nil, errors.WithStack(err)
}
return settingItems, nil
}
func GetSettingItemByKey(key string) (*model.SettingItem, error) {
var settingItem model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("key")), key).First(&settingItem).Error; err != nil {
return nil, errors.WithStack(err)
}
return &settingItem, nil
}
// func GetSettingItemInKeys(keys []string) ([]model.SettingItem, error) {
// var settingItem []model.SettingItem
// if err := db.Where(fmt.Sprintf("%s in ?", columnName("key")), keys).Find(&settingItem).Error; err != nil {
// return nil, errors.WithStack(err)
// }
// return settingItem, nil
// }
2022-07-12 06:03:03 +00:00
2022-06-27 09:25:19 +00:00
func GetPublicSettingItems() ([]model.SettingItem, error) {
var settingItems []model.SettingItem
2022-07-11 14:22:30 +00:00
if err := db.Where(fmt.Sprintf("%s in ?", columnName("flag")), []int{model.PUBLIC, model.READONLY}).Find(&settingItems).Error; err != nil {
return nil, errors.WithStack(err)
2022-06-27 09:25:19 +00:00
}
return settingItems, nil
}
func GetSettingItemsByGroup(group int) ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("group")), group).Find(&settingItems).Error; err != nil {
return nil, errors.WithStack(err)
}
return settingItems, nil
}
2022-07-12 06:03:03 +00:00
func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
var settingItems []model.SettingItem
err := db.Order(columnName("index")).Where(fmt.Sprintf("%s in ?", columnName("group")), groups).Find(&settingItems).Error
if err != nil {
2022-07-12 06:03:03 +00:00
return nil, errors.WithStack(err)
}
return settingItems, nil
}
func SaveSettingItems(items []model.SettingItem) (err error) {
return errors.WithStack(db.Save(items).Error)
2022-06-27 09:25:19 +00:00
}
func SaveSettingItem(item *model.SettingItem) error {
return errors.WithStack(db.Save(item).Error)
2022-06-27 09:25:19 +00:00
}
func DeleteSettingItemByKey(key string) error {
return errors.WithStack(db.Delete(&model.SettingItem{Key: key}).Error)
2022-06-27 09:25:19 +00:00
}