2022-11-03 21:47:36 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
type SystemSetting struct {
|
2023-06-26 23:06:53 +08:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
type FindSystemSetting struct {
|
2023-06-26 23:06:53 +08:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error) {
|
2023-09-26 17:16:58 +08:00
|
|
|
return s.driver.UpsertSystemSetting(ctx, upsert)
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
|
2023-09-26 17:16:58 +08:00
|
|
|
list, err := s.driver.ListSystemSettings(ctx, find)
|
2023-06-26 23:06:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
|
2023-06-26 23:06:53 +08:00
|
|
|
for _, systemSettingMessage := range list {
|
|
|
|
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSetting) (*SystemSetting, error) {
|
2023-06-26 23:06:53 +08:00
|
|
|
if find.Name != "" {
|
|
|
|
if cache, ok := s.systemSettingCache.Load(find.Name); ok {
|
2023-06-29 22:55:03 +08:00
|
|
|
return cache.(*SystemSetting), nil
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
list, err := s.ListSystemSettings(ctx, find)
|
2023-06-26 23:06:53 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
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
|
|
|
|
}
|