Add config controller in server

This commit is contained in:
2025-09-09 19:06:03 +02:00
parent 7a7a909440
commit 88e5f3d694
7 changed files with 285 additions and 3 deletions

View File

@@ -0,0 +1,44 @@
use crate::utils::{error::*, DbPool};
use sqlx::Row;
pub struct ConfigManager;
impl ConfigManager {
pub async fn get_config(pool: &DbPool, key: &str) -> AppResult<Option<String>> {
let row = sqlx::query("SELECT value FROM config WHERE key = ?")
.bind(key)
.fetch_optional(pool)
.await?;
if let Some(row) = row {
Ok(Some(row.get("value")))
} else {
Ok(None)
}
}
pub async fn set_config(pool: &DbPool, key: &str, value: &str) -> AppResult<()> {
sqlx::query(
r#"
INSERT INTO config (key, value, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(key) DO UPDATE SET
value = excluded.value,
updated_at = CURRENT_TIMESTAMP
"#,
)
.bind(key)
.bind(value)
.execute(pool)
.await?;
Ok(())
}
pub async fn get_external_url(pool: &DbPool) -> AppResult<String> {
match Self::get_config(pool, "EXTERNAL_URL").await? {
Some(url) => Ok(url),
None => Err(internal_error("EXTERNAL_URL not configured")),
}
}
}