diff --git a/crates/core/database/src/models/bots/ops/mongodb.rs b/crates/core/database/src/models/bots/ops/mongodb.rs index 7b4c7eee..bda3ae02 100644 --- a/crates/core/database/src/models/bots/ops/mongodb.rs +++ b/crates/core/database/src/models/bots/ops/mongodb.rs @@ -12,31 +12,25 @@ static COL: &str = "bots"; impl AbstractBots for MongoDb { /// Fetch a bot by its id async fn fetch_bot(&self, id: &str) -> Result { - self.find_one_by_id(COL, id) - .await - .map_err(|_| create_database_error!("find_one", COL))? - .ok_or_else(|| create_error!(NotFound)) + query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound)) } /// Fetch a bot by its token async fn fetch_bot_by_token(&self, token: &str) -> Result { - self.find_one( + query!( + self, + find_one, COL, doc! { "token": token - }, - ) - .await - .map_err(|_| create_database_error!("find_one", COL))? + } + )? .ok_or_else(|| create_error!(NotFound)) } /// Insert new bot into the database async fn insert_bot(&self, bot: &Bot) -> Result<()> { - self.insert_one(COL, &bot) - .await - .map(|_| ()) - .map_err(|_| create_database_error!("insert_one", COL)) + query!(self, insert_one, COL, &bot).map(|_| ()) } /// Update bot with new information @@ -46,49 +40,46 @@ impl AbstractBots for MongoDb { partial: &PartialBot, remove: Vec, ) -> Result<()> { - self.update_one_by_id( + query!( + self, + update_one_by_id, COL, id, partial, remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(), - None, + None ) - .await .map(|_| ()) - .map_err(|_| create_database_error!("update_one", COL)) } /// Delete a bot from the database async fn delete_bot(&self, id: &str) -> Result<()> { - self.delete_one_by_id(COL, id) - .await - .map(|_| ()) - .map_err(|_| create_database_error!("delete_one", COL)) + query!(self, delete_one_by_id, COL, id).map(|_| ()) } /// Fetch bots owned by a user async fn fetch_bots_by_user(&self, user_id: &str) -> Result> { - self.find( + query!( + self, + find, COL, doc! { "owner": user_id - }, + } ) - .await - .map_err(|_| create_database_error!("find", COL)) } /// Get the number of bots owned by a user async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result { - self.count_documents( + query!( + self, + count_documents, COL, doc! { "owner": user_id - }, + } ) - .await .map(|v| v as usize) - .map_err(|_| create_database_error!("count", COL)) } } diff --git a/crates/core/result/src/lib.rs b/crates/core/result/src/lib.rs index ada4d55f..9a476b3f 100644 --- a/crates/core/result/src/lib.rs +++ b/crates/core/result/src/lib.rs @@ -119,7 +119,7 @@ pub enum ErrorType { #[macro_export] macro_rules! create_error { - ( $error:ident $( $tt:tt )? ) => { + ( $error: ident $( $tt:tt )? ) => { $crate::Error { error_type: $crate::ErrorType::$error $( $tt )?, location: format!("{}:{}:{}", file!(), line!(), column!()), @@ -129,7 +129,7 @@ macro_rules! create_error { #[macro_export] macro_rules! create_database_error { - ( $operation:expr, $collection:expr ) => { + ( $operation: expr, $collection: expr ) => { create_error!(DatabaseError { operation: $operation.to_string(), collection: $collection.to_string() @@ -137,6 +137,23 @@ macro_rules! create_database_error { }; } +#[macro_export] +#[cfg(debug_assertions)] +macro_rules! query { + ( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => { + Ok($self.$type($collection, $($rest),+).await.unwrap()) + }; +} + +#[macro_export] +#[cfg(not(debug_assertions))] +macro_rules! query { + ( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => { + $self.$type($collection, $($rest),+).await + .map_err(|_| create_database_error!(stringify!($type), $collection)) + }; +} + #[cfg(test)] mod tests { use crate::ErrorType;