From c1b9d941f31448ac3d9bf756ad2e4d115ffd84eb Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 26 Apr 2023 16:36:38 +0100 Subject: [PATCH] refactor(core/database): crud ordering in files --- crates/core/database/src/models/bots/ops.rs | 16 ++--- .../database/src/models/bots/ops/mongodb.rs | 58 +++++++++---------- .../database/src/models/bots/ops/reference.rs | 50 ++++++++-------- 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/crates/core/database/src/models/bots/ops.rs b/crates/core/database/src/models/bots/ops.rs index 3a618253..8d77673f 100644 --- a/crates/core/database/src/models/bots/ops.rs +++ b/crates/core/database/src/models/bots/ops.rs @@ -7,14 +7,20 @@ mod reference; #[async_trait] pub trait AbstractBots: Sync + Send { + /// Insert new bot into the database + async fn insert_bot(&self, bot: &Bot) -> Result<()>; + /// Fetch a bot by its id async fn fetch_bot(&self, id: &str) -> Result; /// Fetch a bot by its token async fn fetch_bot_by_token(&self, token: &str) -> Result; - /// Insert new bot into the database - async fn insert_bot(&self, bot: &Bot) -> Result<()>; + /// Fetch bots owned by a user + async fn fetch_bots_by_user(&self, user_id: &str) -> Result>; + + /// Get the number of bots owned by a user + async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result; /// Update bot with new information async fn update_bot( @@ -26,10 +32,4 @@ pub trait AbstractBots: Sync + Send { /// Delete a bot from the database async fn delete_bot(&self, id: &str) -> Result<()>; - - /// Fetch bots owned by a user - async fn fetch_bots_by_user(&self, user_id: &str) -> Result>; - - /// Get the number of bots owned by a user - async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result; } diff --git a/crates/core/database/src/models/bots/ops/mongodb.rs b/crates/core/database/src/models/bots/ops/mongodb.rs index 69da814f..b141e8c5 100644 --- a/crates/core/database/src/models/bots/ops/mongodb.rs +++ b/crates/core/database/src/models/bots/ops/mongodb.rs @@ -9,6 +9,11 @@ static COL: &str = "bots"; #[async_trait] impl AbstractBots for MongoDb { + /// Insert new bot into the database + async fn insert_bot(&self, bot: &Bot) -> Result<()> { + query!(self, insert_one, COL, &bot).map(|_| ()) + } + /// Fetch a bot by its id async fn fetch_bot(&self, id: &str) -> Result { query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound)) @@ -27,35 +32,6 @@ impl AbstractBots for MongoDb { .ok_or_else(|| create_error!(NotFound)) } - /// Insert new bot into the database - async fn insert_bot(&self, bot: &Bot) -> Result<()> { - query!(self, insert_one, COL, &bot).map(|_| ()) - } - - /// Update bot with new information - async fn update_bot( - &self, - id: &str, - partial: &PartialBot, - remove: Vec, - ) -> Result<()> { - query!( - self, - update_one_by_id, - COL, - id, - partial, - remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(), - None - ) - .map(|_| ()) - } - - /// Delete a bot from the database - async fn delete_bot(&self, id: &str) -> Result<()> { - 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> { query!( @@ -80,6 +56,30 @@ impl AbstractBots for MongoDb { ) .map(|v| v as usize) } + + /// Update bot with new information + async fn update_bot( + &self, + id: &str, + partial: &PartialBot, + remove: Vec, + ) -> Result<()> { + query!( + self, + update_one_by_id, + COL, + id, + partial, + remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(), + None + ) + .map(|_| ()) + } + + /// Delete a bot from the database + async fn delete_bot(&self, id: &str) -> Result<()> { + query!(self, delete_one_by_id, COL, id).map(|_| ()) + } } impl IntoDocumentPath for FieldsBot { diff --git a/crates/core/database/src/models/bots/ops/reference.rs b/crates/core/database/src/models/bots/ops/reference.rs index 3a8caff2..b516dde8 100644 --- a/crates/core/database/src/models/bots/ops/reference.rs +++ b/crates/core/database/src/models/bots/ops/reference.rs @@ -7,6 +7,17 @@ use super::AbstractBots; #[async_trait] impl AbstractBots for ReferenceDb { + /// Insert new bot into the database + async fn insert_bot(&self, bot: &Bot) -> Result<()> { + let mut bots = self.bots.lock().await; + if bots.contains_key(&bot.id) { + Err(create_database_error!("insert", "bot")) + } else { + bots.insert(bot.id.to_string(), bot.clone()); + Ok(()) + } + } + /// Fetch a bot by its id async fn fetch_bot(&self, id: &str) -> Result { let bots = self.bots.lock().await; @@ -22,15 +33,20 @@ impl AbstractBots for ReferenceDb { .ok_or_else(|| create_error!(NotFound)) } - /// Insert new bot into the database - async fn insert_bot(&self, bot: &Bot) -> Result<()> { - let mut bots = self.bots.lock().await; - if bots.contains_key(&bot.id) { - Err(create_database_error!("insert", "bot")) - } else { - bots.insert(bot.id.to_string(), bot.clone()); - Ok(()) - } + /// Fetch bots owned by a user + async fn fetch_bots_by_user(&self, user_id: &str) -> Result> { + let bots = self.bots.lock().await; + Ok(bots + .values() + .filter(|bot| bot.owner == user_id) + .cloned() + .collect()) + } + + /// Get the number of bots owned by a user + async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result { + let bots = self.bots.lock().await; + Ok(bots.values().filter(|bot| bot.owner == user_id).count()) } /// Update bot with new information @@ -63,20 +79,4 @@ impl AbstractBots for ReferenceDb { Err(create_error!(NotFound)) } } - - /// Fetch bots owned by a user - async fn fetch_bots_by_user(&self, user_id: &str) -> Result> { - let bots = self.bots.lock().await; - Ok(bots - .values() - .filter(|bot| bot.owner == user_id) - .cloned() - .collect()) - } - - /// Get the number of bots owned by a user - async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result { - let bots = self.bots.lock().await; - Ok(bots.values().filter(|bot| bot.owner == user_id).count()) - } }