Initial commit.
This commit is contained in:
72
mlc-client/src/daos/user.rs
Normal file
72
mlc-client/src/daos/user.rs
Normal file
@ -0,0 +1,72 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::OptionalExtension;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserdataStorage {
|
||||
pub connection: Arc<Mutex<Connection>>,
|
||||
}
|
||||
|
||||
pub struct Userdata {
|
||||
pub user_id: String,
|
||||
pub secret_key: Vec<u8>,
|
||||
pub public_key: Vec<u8>,
|
||||
}
|
||||
|
||||
impl UserdataStorage {
|
||||
pub fn new(connection: Arc<Mutex<Connection>>) -> Self {
|
||||
Self { connection }
|
||||
}
|
||||
|
||||
pub fn get_user(&self) -> anyhow::Result<Option<Userdata>> {
|
||||
let conn = self
|
||||
.connection
|
||||
.lock()
|
||||
.expect("Connection mutext is poisoned. It's a critical error. Exiting.");
|
||||
let res = conn
|
||||
.query_row(
|
||||
"SELECT user_id, secret_key, public_key FROM mls_cli_userdata",
|
||||
[],
|
||||
|row| {
|
||||
let user_id = row.get(0)?;
|
||||
let secret_key = row.get(1)?;
|
||||
let public_key = row.get(2)?;
|
||||
Ok(Userdata {
|
||||
user_id,
|
||||
secret_key,
|
||||
public_key,
|
||||
})
|
||||
},
|
||||
)
|
||||
.optional()?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn truncate(&self) -> anyhow::Result<()> {
|
||||
let conn = self
|
||||
.connection
|
||||
.lock()
|
||||
.expect("Connection mutext is poisoned. It's a critical error. Exiting.");
|
||||
conn.execute("DELETE FROM mls_cli_userdata", [])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_user(
|
||||
&self,
|
||||
user_id: &str,
|
||||
public_key: &[u8],
|
||||
secret_key: &[u8],
|
||||
) -> anyhow::Result<()> {
|
||||
let conn = self
|
||||
.connection
|
||||
.lock()
|
||||
.expect("Connection mutext is poisoned. It's a critical error. Exiting.");
|
||||
conn.execute(
|
||||
"INSERT INTO mls_cli_userdata (user_id, secret_key, public_key) VALUES (?1, ?2, ?3)",
|
||||
rusqlite::params![user_id, secret_key, public_key],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user