2022-11-03 16:47:36 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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-07-20 18:15:56 +03:00
|
|
|
stmt := `
|
2023-07-02 13:56:25 +03:00
|
|
|
INSERT INTO system_setting (
|
|
|
|
name, value, description
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(name) DO UPDATE
|
|
|
|
SET
|
|
|
|
value = EXCLUDED.value,
|
|
|
|
description = EXCLUDED.description
|
|
|
|
`
|
2023-07-20 18:15:56 +03:00
|
|
|
if _, err := s.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
systemSetting := upsert
|
|
|
|
return systemSetting, nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
|
2023-07-20 18:15:56 +03:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.Name != "" {
|
|
|
|
where, args = append(where, "name = ?"), append(args, find.Name)
|
2023-06-26 18:06:53 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
description
|
|
|
|
FROM system_setting
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
|
|
|
|
rows, err := s.db.QueryContext(ctx, query, args...)
|
2023-06-26 18:06:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-20 18:15:56 +03:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*SystemSetting{}
|
|
|
|
for rows.Next() {
|
|
|
|
systemSettingMessage := &SystemSetting{}
|
|
|
|
if err := rows.Scan(
|
|
|
|
&systemSettingMessage.Name,
|
|
|
|
&systemSettingMessage.Value,
|
|
|
|
&systemSettingMessage.Description,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list = append(list, systemSettingMessage)
|
|
|
|
}
|
2023-06-26 18:06:53 +03:00
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
if err := rows.Err(); err != nil {
|
2023-07-06 16:56:42 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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-07-02 13:56:25 +03: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
|
|
|
|
}
|