Skip to content

Commit

Permalink
Simplify database API
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 14, 2024
1 parent 7801ca0 commit 79e03c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
30 changes: 15 additions & 15 deletions src/admin/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::Result;
use deadpool_sqlite::Pool;
use rusqlite::{named_params, Connection, OptionalExtension, Row};
#[cfg(not(test))]
use std::thread::sleep;
#[cfg(not(test))]
use std::time::Duration;

#[derive(Debug, PartialEq, Eq)]
pub struct Admin {
Expand All @@ -21,7 +17,14 @@ const COL_ALLOWED_ACTIONS: &str = "allowed_actions";
const MAPPER_PROJECTION: &str = "id, name, allowed_actions";

impl Admin {
pub fn insert(name: &str, password: &str, conn: &Connection) -> Result<Option<Admin>> {
pub async fn insert_async(name: String, password: String, pool: &Pool) -> Result<Admin> {
pool.get()
.await?
.interact(move |conn| Admin::insert(&name, &password, conn))
.await?
}

pub fn insert(name: &str, password: &str, conn: &Connection) -> Result<Admin> {
let sql = format!(
r#"
INSERT INTO {TABLE_NAME} (
Expand All @@ -33,16 +36,15 @@ impl Admin {
);
"#
);
#[cfg(not(test))]
sleep(Duration::from_millis(10));
conn.execute(
&sql,
named_params! {
":name": name,
":password": password,
},
)?;
Admin::select_by_password(password, conn)
Admin::select_by_password(password, conn)?
.ok_or("failed to select newly inserted admin".into())
}

pub fn select_by_id(id: i64, conn: &Connection) -> Result<Option<Admin>> {
Expand Down Expand Up @@ -108,8 +110,6 @@ impl Admin {
WHERE {COL_ID} = :{COL_ID}
"#
);
#[cfg(not(test))]
sleep(Duration::from_millis(10));
conn.execute(
&sql,
named_params! {
Expand Down Expand Up @@ -139,15 +139,15 @@ mod test {
#[test]
fn insert() -> Result<()> {
let conn = mock_conn();
let admin = Admin::insert("name", "pwd", &conn)?.ok_or("can't insert admin")?;
let admin = Admin::insert("name", "pwd", &conn)?;
assert_eq!(Some(admin), Admin::select_by_id(1, &conn)?);
Ok(())
}

#[test]
fn select_by_id() -> Result<()> {
let conn = mock_conn();
let admin = Admin::insert("name", "pwd", &conn)?.ok_or("can't insert admin")?;
let admin = Admin::insert("name", "pwd", &conn)?;
assert_eq!(Some(admin), Admin::select_by_id(1, &conn)?);
Ok(())
}
Expand All @@ -156,7 +156,7 @@ mod test {
fn select_by_name() -> Result<()> {
let conn = mock_conn();
let name = "name";
let admin = Admin::insert(name, "pwd", &conn)?.ok_or("can't insert admin")?;
let admin = Admin::insert(name, "pwd", &conn)?;
assert_eq!(Some(admin), Admin::select_by_name(name, &conn)?);
Ok(())
}
Expand All @@ -165,15 +165,15 @@ mod test {
fn select_by_password() -> Result<()> {
let conn = mock_conn();
let password = "pwd";
let admin = Admin::insert("name", password, &conn)?.ok_or("can't insert admin")?;
let admin = Admin::insert("name", password, &conn)?;
assert_eq!(Some(admin), Admin::select_by_password(password, &conn)?);
Ok(())
}

#[test]
fn update_allowed_actions() -> Result<()> {
let conn = mock_conn();
let admin = Admin::insert("name", "pwd", &conn)?.ok_or("can't insert admin")?;
let admin = Admin::insert("name", "pwd", &conn)?;
let actions = vec!["action_1".into(), "action_2".into()];
Admin::update_allowed_actions(admin.id, &actions, &conn)?;
assert_eq!(
Expand Down
12 changes: 4 additions & 8 deletions src/rpc/add_admin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
admin::{self, Admin},
admin::{service::check_rpc, Admin},
discord, Result,
};
use deadpool_sqlite::Pool;
Expand All @@ -24,13 +24,9 @@ pub struct Res {
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Res> {
let admin = admin::service::check_rpc(&args.password, NAME, &pool).await?;
let new_admin = pool
.get()
.await?
.interact(move |conn| Admin::insert(&args.new_admin_name, &args.new_admin_password, conn))
.await??
.ok_or("Failed to insert new admin user record")?;
let admin = check_rpc(&args.password, NAME, &pool).await?;
let new_admin =
Admin::insert_async(args.new_admin_name, args.new_admin_password, &pool).await?;
let log_message = format!(
"{} added new admin user {} with the following allowed actions: {}",
admin.name,
Expand Down

0 comments on commit 79e03c4

Please sign in to comment.