use std::error::Error;
use bb8_postgres::bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use bb8_postgres::tokio_postgres::{GenericClient, NoTls, Row};
const GET: &str = "SELECT cabinet_announce.announce FROM rec23.cabinet_announce cabinet_announce";
const DELETE: &str = "DELETE FROM rec23.cabinet_announce";
pub struct Cabinet {
pub announce: String,
}
impl Cabinet {
pub async fn delete(pool: &Pool<PostgresConnectionManager<NoTls>>) -> Result<(), Box<dyn Error + Send + Sync>> {
let connection = pool.get().await?;
let client = connection.client();
client.execute(DELETE, &[]).await?;
Ok(())
}
pub async fn get(pool: &Pool<PostgresConnectionManager<NoTls>>) -> Result<Option<Self>, Box<dyn Error + Send + Sync>> {
let connection = pool.get().await?;
let client = connection.client();
match client.query_opt(GET, &[]).await? {
Some(row) => Ok(Self::convert_from_row(row)),
None => Ok(None)
}
}
fn convert_from_row(row: Row) -> Option<Self> {
Some(Cabinet {
announce: row.get("announce")
})
}
}