mirror of
https://github.com/usememos/memos.git
synced 2024-11-28 14:23:15 +03:00
41eba71f0f
* Add new database interface for SQL operations * Move SQL code of Activity into Database * Rename `Database` into `Driver` * Move SQL code of SystemSetting into Driver * Fix store.New in text code * Change database into driver in the variables * Change sqlite3.New into sqlite3.NewDriver
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type SystemSetting struct {
|
|
Name string
|
|
Value string
|
|
Description string
|
|
}
|
|
|
|
type FindSystemSetting struct {
|
|
Name string
|
|
}
|
|
|
|
func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error) {
|
|
return s.driver.UpsertSystemSetting(ctx, upsert)
|
|
}
|
|
|
|
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
|
|
list, err := s.driver.ListSystemSettings(ctx, find)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, systemSettingMessage := range list {
|
|
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSetting) (*SystemSetting, error) {
|
|
if find.Name != "" {
|
|
if cache, ok := s.systemSettingCache.Load(find.Name); ok {
|
|
return cache.(*SystemSetting), nil
|
|
}
|
|
}
|
|
|
|
list, err := s.ListSystemSettings(ctx, find)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(list) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
systemSettingMessage := list[0]
|
|
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
|
return systemSettingMessage, nil
|
|
}
|
|
|
|
func (s *Store) GetSystemSettingValueWithDefault(ctx *context.Context, settingName string, defaultValue string) string {
|
|
if setting, err := s.GetSystemSetting(*ctx, &FindSystemSetting{
|
|
Name: settingName,
|
|
}); err == nil && setting != nil {
|
|
return setting.Value
|
|
}
|
|
return defaultValue
|
|
}
|