use async_trait::async_trait; use sqlx::PgPool; use domain::{ ports::settings::AppSettingsRepository, DomainError, DomainResult, }; pub struct PgAppSettings { pool: PgPool, } impl PgAppSettings { pub fn new(pool: PgPool) -> Self { Self { pool } } } #[async_trait] impl AppSettingsRepository for PgAppSettings { async fn get(&self, key: &str) -> DomainResult> { sqlx::query_scalar::<_, String>("SELECT value FROM app_settings WHERE key = $1") .bind(key) .fetch_optional(&self.pool) .await .map_err(|e| DomainError::InfrastructureError(e.to_string())) } async fn set(&self, key: &str, value: &str) -> DomainResult<()> { sqlx::query( "INSERT INTO app_settings (key, value) VALUES ($1, $2) ON CONFLICT(key) DO UPDATE SET value = EXCLUDED.value", ) .bind(key) .bind(value) .execute(&self.pool) .await .map(|_| ()) .map_err(|e| DomainError::InfrastructureError(e.to_string())) } async fn get_all(&self) -> DomainResult> { sqlx::query_as::<_, (String, String)>("SELECT key, value FROM app_settings ORDER BY key") .fetch_all(&self.pool) .await .map_err(|e| DomainError::InfrastructureError(e.to_string())) } }