45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package platformsettings
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var AllowedKeys = map[string]bool{
|
|
"platform_name": true,
|
|
"platform_subtitle": true,
|
|
"platform_logo": true,
|
|
"admin_primary_color": true,
|
|
"admin_accent_color": true,
|
|
}
|
|
|
|
func GetSettings(ctx context.Context, pool *pgxpool.Pool) (map[string]string, error) {
|
|
rows, err := pool.Query(ctx, `SELECT key, value FROM public.platform_settings`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("platform settings: get: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
result := map[string]string{}
|
|
for rows.Next() {
|
|
var k, v string
|
|
if err := rows.Scan(&k, &v); err != nil {
|
|
return nil, fmt.Errorf("platform settings: scan: %w", err)
|
|
}
|
|
result[k] = v
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
func SetSetting(ctx context.Context, pool *pgxpool.Pool, key, value string) error {
|
|
_, err := pool.Exec(ctx,
|
|
`INSERT INTO public.platform_settings (key, value) VALUES ($1, $2)
|
|
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()`,
|
|
key, value)
|
|
if err != nil {
|
|
return fmt.Errorf("platform settings: set %s: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|