Changeset 0.17.1 (#76)
This commit is contained in:
58
server/store/sqlite/global_config.go
Normal file
58
server/store/sqlite/global_config.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
)
|
||||
|
||||
type GlobalConfigStore struct{}
|
||||
|
||||
func NewGlobalConfigStore() *GlobalConfigStore { return &GlobalConfigStore{} }
|
||||
|
||||
func (s *GlobalConfigStore) Get(ctx context.Context, key string) (models.JSONMap, error) {
|
||||
var valueJSON string
|
||||
err := DB.QueryRowContext(ctx,
|
||||
"SELECT value FROM global_settings WHERE key = ?", key).Scan(&valueJSON)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result models.JSONMap
|
||||
json.Unmarshal([]byte(valueJSON), &result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *GlobalConfigStore) Set(ctx context.Context, key string, value models.JSONMap, updatedBy string) error {
|
||||
valueJSON := ToJSON(value)
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO global_settings (key, value, updated_by, updated_at)
|
||||
VALUES (?, ?, ?, datetime('now'))
|
||||
ON CONFLICT (key) DO UPDATE SET value = excluded.value, updated_by = excluded.updated_by, updated_at = datetime('now')`,
|
||||
key, valueJSON, updatedBy)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *GlobalConfigStore) GetAll(ctx context.Context) (map[string]models.JSONMap, error) {
|
||||
rows, err := DB.QueryContext(ctx, "SELECT key, value FROM global_settings")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make(map[string]models.JSONMap)
|
||||
for rows.Next() {
|
||||
var key, valueJSON string
|
||||
if err := rows.Scan(&key, &valueJSON); err != nil {
|
||||
continue
|
||||
}
|
||||
var m models.JSONMap
|
||||
json.Unmarshal([]byte(valueJSON), &m)
|
||||
result[key] = m
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user