Added settings to backend and frontend.
This commit is contained in:
@@ -3,3 +3,4 @@ pub mod article;
|
||||
pub mod variant;
|
||||
pub mod unit;
|
||||
pub mod label;
|
||||
pub mod settings;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Arguments, Decode, Encode, FromRow, PgPool, Row};
|
||||
use sqlx::postgres::{PgArguments, PgRow};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, FromRow, Decode, Encode)]
|
||||
pub struct Settings {
|
||||
pub id: i64,
|
||||
pub mhd: String,
|
||||
pub bio: String,
|
||||
}
|
||||
|
||||
fn from_postgres_row_small(row: PgRow) -> Settings {
|
||||
Settings {
|
||||
id: row.get(0),
|
||||
mhd: row.get(1),
|
||||
bio: row.get(2),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn load_settings(pool: &PgPool) -> Option<Settings> {
|
||||
let statement = format!(
|
||||
"SELECT * FROM {}",
|
||||
"settings",
|
||||
);
|
||||
|
||||
let res = sqlx::query(
|
||||
statement.as_str())
|
||||
.fetch_one(pool).await;
|
||||
|
||||
match res {
|
||||
Ok(record) => {
|
||||
Some(from_postgres_row_small(record))
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Error while loading settings.");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_settings(settings: &Settings, pool: PgPool) -> Option<Settings> {
|
||||
let mut args = PgArguments::default();
|
||||
args.add(&settings.id);
|
||||
args.add(&settings.mhd);
|
||||
args.add(&settings.bio);
|
||||
|
||||
let statement = format!(
|
||||
"UPDATE {} SET mhd = $2, bio = $3 WHERE id = $1 RETURNING *",
|
||||
"settings",
|
||||
);
|
||||
|
||||
let res = sqlx::query_with(
|
||||
statement.as_str(),
|
||||
args)
|
||||
.fetch_one(&pool).await;
|
||||
|
||||
info!("{:#?}", res);
|
||||
|
||||
match res {
|
||||
Ok(record) => {
|
||||
let label = from_postgres_row_small(record);
|
||||
Some(label)
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Error while updating settings.");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user