adapter-postgres: all repository implementations

This commit is contained in:
2026-07-12 02:39:56 +02:00
parent e8179d1f53
commit 0fe80b545e
13 changed files with 1829 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
//! PostgreSQL adapter for app settings (AppSettingsRepository).
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<Option<String>> {
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<Vec<(String, String)>> {
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()))
}
}