use sqlx::PgPool;
#[derive(Clone)]
pub struct NbspConfig {
pub nbsp_base_url: String,
pub nbsp_homepage_notice: Option<String>,
}
impl NbspConfig {
pub async fn load(pool: &PgPool) -> sqlx::Result<Self> {
let query = "SELECT key, value FROM nbsp_config;";
let rows = sqlx::query_as::<_, (String, Option<String>)>(query)
.fetch_all(pool)
.await?;
let mut config = Self {
nbsp_base_url: String::new(),
nbsp_homepage_notice: None,
};
for (key, value) in rows {
if key == "nbsp_base_url"
&& let Some(value) = value.as_ref()
{
config.nbsp_base_url = value.to_string();
}
if key == "nbsp_homepage_notice" {
config.nbsp_homepage_notice = value;
}
}
assert!(
!config.nbsp_base_url.is_empty(),
"nbsp_base_url from nbsp_config is empty"
);
Ok(config)
}
}