system_setting.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package store
  2. import (
  3. "context"
  4. )
  5. type SystemSetting struct {
  6. Name string
  7. Value string
  8. Description string
  9. }
  10. type FindSystemSetting struct {
  11. Name string
  12. }
  13. func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error) {
  14. return s.driver.UpsertSystemSetting(ctx, upsert)
  15. }
  16. func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
  17. list, err := s.driver.ListSystemSettings(ctx, find)
  18. if err != nil {
  19. return nil, err
  20. }
  21. for _, systemSettingMessage := range list {
  22. s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
  23. }
  24. return list, nil
  25. }
  26. func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSetting) (*SystemSetting, error) {
  27. if find.Name != "" {
  28. if cache, ok := s.systemSettingCache.Load(find.Name); ok {
  29. return cache.(*SystemSetting), nil
  30. }
  31. }
  32. list, err := s.ListSystemSettings(ctx, find)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if len(list) == 0 {
  37. return nil, nil
  38. }
  39. systemSettingMessage := list[0]
  40. s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
  41. return systemSettingMessage, nil
  42. }
  43. func (s *Store) GetSystemSettingValueWithDefault(ctx context.Context, settingName string, defaultValue string) string {
  44. if setting, err := s.GetSystemSetting(ctx, &FindSystemSetting{
  45. Name: settingName,
  46. }); err == nil && setting != nil {
  47. return setting.Value
  48. }
  49. return defaultValue
  50. }