use std::sync::Arc; use tokio::sync::Mutex; use tokio_postgres::Client; use super::db::DBError; #[derive(Clone)] pub struct Keys(Arc>); impl Keys { pub fn new(client: Arc>) -> Self { Self(client) } pub async fn get_key(&self, key: &str) -> Result { Ok(self .0 .lock() .await .query("select value from keys where key = $1", &[&key]) .await? .first() .ok_or_else(|| DBError::NotFound)? .get("value")) } pub async fn set_key(&self, key: &str, value: &str) -> Result<(), DBError> { self.0 .lock() .await .execute( "insert into keys (key, value) values ($1, $2)", &[&key, &value], ) .await?; Ok(()) } }