40 lines
893 B
Rust
40 lines
893 B
Rust
use std::sync::Arc;
|
|
|
|
use tokio::sync::Mutex;
|
|
use tokio_postgres::Client;
|
|
|
|
use super::db::DBError;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Keys(Arc<Mutex<Client>>);
|
|
impl Keys {
|
|
pub fn new(client: Arc<Mutex<Client>>) -> Self {
|
|
Self(client)
|
|
}
|
|
|
|
pub async fn get_key(&self, key: &str) -> Result<String, DBError> {
|
|
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(())
|
|
}
|
|
}
|