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