30 lines
813 B
Go
30 lines
813 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// Setting liest eine Server-Einstellung (ErrNotFound, wenn nicht gesetzt).
|
|
func (s *Store) Setting(ctx context.Context, key string) (string, error) {
|
|
var v string
|
|
err := s.db.QueryRowContext(ctx, `SELECT value FROM settings WHERE key = ?`, key).Scan(&v)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", fmt.Errorf("Einstellung %s: %w", key, ErrNotFound)
|
|
}
|
|
return v, err
|
|
}
|
|
|
|
// SetSetting setzt bzw. überschreibt eine Server-Einstellung.
|
|
func (s *Store) SetSetting(ctx context.Context, key, value string) error {
|
|
if blank(key) {
|
|
return invalid("Schlüssel fehlt")
|
|
}
|
|
_, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
|
|
key, value)
|
|
return err
|
|
}
|