adapter-postgres: all repository implementations
This commit is contained in:
92
crates/adapters/postgres/src/provider_config.rs
Normal file
92
crates/adapters/postgres/src/provider_config.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
//! PostgreSQL adapter for provider config (ProviderConfigCommand + ProviderConfigQuery).
|
||||
|
||||
use async_trait::async_trait;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use adapter_common::map_sqlx_error;
|
||||
use domain::{
|
||||
ports::provider_config::{ProviderConfigCommand, ProviderConfigQuery},
|
||||
DomainResult, ProviderConfigRow,
|
||||
};
|
||||
|
||||
pub struct PgProviderConfig {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PgProviderConfig {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ProviderConfigCommand for PgProviderConfig {
|
||||
async fn upsert(&self, row: &ProviderConfigRow) -> DomainResult<()> {
|
||||
sqlx::query(
|
||||
r#"INSERT INTO provider_configs (id, provider_type, config_json, enabled, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
provider_type = EXCLUDED.provider_type,
|
||||
config_json = EXCLUDED.config_json,
|
||||
enabled = EXCLUDED.enabled,
|
||||
updated_at = EXCLUDED.updated_at"#,
|
||||
)
|
||||
.bind(row.id())
|
||||
.bind(row.provider_type())
|
||||
.bind(row.config_json())
|
||||
.bind(row.enabled())
|
||||
.bind(row.updated_at())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, id: &str) -> DomainResult<()> {
|
||||
sqlx::query("DELETE FROM provider_configs WHERE id = $1")
|
||||
.bind(id)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ProviderConfigQuery for PgProviderConfig {
|
||||
async fn get_all(&self) -> DomainResult<Vec<ProviderConfigRow>> {
|
||||
let rows: Vec<(String, String, String, bool, String)> = sqlx::query_as(
|
||||
"SELECT id, provider_type, config_json, enabled, updated_at FROM provider_configs",
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|(id, provider_type, config_json, enabled, updated_at)| {
|
||||
ProviderConfigRow::from_persistence(
|
||||
id,
|
||||
provider_type,
|
||||
config_json,
|
||||
enabled,
|
||||
updated_at,
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn get_by_id(&self, id: &str) -> DomainResult<Option<ProviderConfigRow>> {
|
||||
let row: Option<(String, String, String, bool, String)> = sqlx::query_as(
|
||||
"SELECT id, provider_type, config_json, enabled, updated_at FROM provider_configs WHERE id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(row.map(|(id, provider_type, config_json, enabled, updated_at)| {
|
||||
ProviderConfigRow::from_persistence(id, provider_type, config_json, enabled, updated_at)
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user