Inicial
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var AllowedKeys = map[string]bool{
|
||||
"company_name": true,
|
||||
"company_nif": true,
|
||||
"company_address": true,
|
||||
"company_iban": true,
|
||||
"company_phone": true,
|
||||
"company_email": true,
|
||||
}
|
||||
|
||||
func GetSettings(ctx context.Context, conn *pgxpool.Conn) (map[string]string, error) {
|
||||
rows, err := conn.Query(ctx, `SELECT key, value FROM tenant_settings`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("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("settings: scan: %w", err)
|
||||
}
|
||||
result[k] = v
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func SetSetting(ctx context.Context, conn *pgxpool.Conn, key, value string) error {
|
||||
_, err := conn.Exec(ctx,
|
||||
`INSERT INTO tenant_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("settings: set %s: %w", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user