Projeto bot pontos
This commit is contained in:
90
backend/internal/adapters/postgres/settings.go
Normal file
90
backend/internal/adapters/postgres/settings.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"botpontos/internal/domain"
|
||||
)
|
||||
|
||||
// GetSettings devolve as configurações globais (app_settings) como texto.
|
||||
func (s *Store) GetSettings(ctx context.Context) (map[string]string, error) {
|
||||
rows, err := s.pool.Query(ctx, `SELECT key, value #>> '{}' FROM app_settings`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := map[string]string{}
|
||||
for rows.Next() {
|
||||
var key string
|
||||
var value *string
|
||||
if err := rows.Scan(&key, &value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value != nil {
|
||||
out[key] = *value
|
||||
}
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SeedSettings grava os valores padrão que ainda não existem (primeira subida).
|
||||
func (s *Store) SeedSettings(ctx context.Context, values map[string]string) error {
|
||||
return s.inTx(ctx, func(q querier) error {
|
||||
for key, value := range values {
|
||||
if _, err := q.Exec(ctx, `
|
||||
INSERT INTO app_settings (key, value) VALUES ($1, to_jsonb($2::text))
|
||||
ON CONFLICT (key) DO NOTHING`, key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) SaveSettings(ctx context.Context, values map[string]string, accountID *int64) error {
|
||||
return s.inTx(ctx, func(q querier) error {
|
||||
for key, value := range values {
|
||||
if _, err := q.Exec(ctx, `
|
||||
INSERT INTO app_settings (key, value, updated_by_account) VALUES ($1, to_jsonb($2::text), $3)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_by_account = EXCLUDED.updated_by_account`,
|
||||
key, value, accountID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// GetState lê um estado operacional (service_state) para dentro de out.
|
||||
func (s *Store) GetState(ctx context.Context, key string, out any) (bool, error) {
|
||||
var raw string
|
||||
err := s.pool.QueryRow(ctx, `SELECT value::text FROM service_state WHERE key = $1`, key).Scan(&raw)
|
||||
if err != nil {
|
||||
if mapErr(err) == domain.ErrNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), out); err != nil {
|
||||
return false, fmt.Errorf("service_state %s ilegível: %w", key, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *Store) SetState(ctx context.Context, key string, value any) error {
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.pool.Exec(ctx, `
|
||||
INSERT INTO service_state (key, value) VALUES ($1, $2::text::jsonb)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`, key, string(raw))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) DeleteState(ctx context.Context, key string) error {
|
||||
_, err := s.pool.Exec(ctx, `DELETE FROM service_state WHERE key = $1`, key)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user