48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
//! SQLite adapter for app settings (AppSettingsRepository).
|
|
|
|
use async_trait::async_trait;
|
|
use sqlx::SqlitePool;
|
|
|
|
use domain::{
|
|
ports::settings::AppSettingsRepository,
|
|
DomainError, DomainResult,
|
|
};
|
|
|
|
pub struct SqliteAppSettings {
|
|
pool: SqlitePool,
|
|
}
|
|
|
|
impl SqliteAppSettings {
|
|
pub fn new(pool: SqlitePool) -> Self {
|
|
Self { pool }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl AppSettingsRepository for SqliteAppSettings {
|
|
async fn get(&self, key: &str) -> DomainResult<Option<String>> {
|
|
sqlx::query_scalar::<_, String>("SELECT value FROM app_settings WHERE key = ?")
|
|
.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 OR REPLACE INTO app_settings (key, value) VALUES (?, ?)")
|
|
.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()))
|
|
}
|
|
}
|