forked from jmug/stoatchat
refactor(core/database): crud ordering in files
This commit is contained in:
@@ -7,14 +7,20 @@ mod reference;
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait AbstractBots: Sync + Send {
|
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
|
/// Fetch a bot by its id
|
||||||
async fn fetch_bot(&self, id: &str) -> Result<Bot>;
|
async fn fetch_bot(&self, id: &str) -> Result<Bot>;
|
||||||
|
|
||||||
/// Fetch a bot by its token
|
/// Fetch a bot by its token
|
||||||
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot>;
|
async fn fetch_bot_by_token(&self, token: &str) -> Result<Bot>;
|
||||||
|
|
||||||
/// Insert new bot into the database
|
/// Fetch bots owned by a user
|
||||||
async fn insert_bot(&self, bot: &Bot) -> Result<()>;
|
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>>;
|
||||||
|
|
||||||
|
/// Get the number of bots owned by a user
|
||||||
|
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize>;
|
||||||
|
|
||||||
/// Update bot with new information
|
/// Update bot with new information
|
||||||
async fn update_bot(
|
async fn update_bot(
|
||||||
@@ -26,10 +32,4 @@ pub trait AbstractBots: Sync + Send {
|
|||||||
|
|
||||||
/// Delete a bot from the database
|
/// Delete a bot from the database
|
||||||
async fn delete_bot(&self, id: &str) -> Result<()>;
|
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<Vec<Bot>>;
|
|
||||||
|
|
||||||
/// Get the number of bots owned by a user
|
|
||||||
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize>;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ static COL: &str = "bots";
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl AbstractBots for MongoDb {
|
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
|
/// Fetch a bot by its id
|
||||||
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
||||||
query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound))
|
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))
|
.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<FieldsBot>,
|
|
||||||
) -> 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
|
/// Fetch bots owned by a user
|
||||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
||||||
query!(
|
query!(
|
||||||
@@ -80,6 +56,30 @@ impl AbstractBots for MongoDb {
|
|||||||
)
|
)
|
||||||
.map(|v| v as usize)
|
.map(|v| v as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update bot with new information
|
||||||
|
async fn update_bot(
|
||||||
|
&self,
|
||||||
|
id: &str,
|
||||||
|
partial: &PartialBot,
|
||||||
|
remove: Vec<FieldsBot>,
|
||||||
|
) -> 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 {
|
impl IntoDocumentPath for FieldsBot {
|
||||||
|
|||||||
@@ -7,6 +7,17 @@ use super::AbstractBots;
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl AbstractBots for ReferenceDb {
|
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
|
/// Fetch a bot by its id
|
||||||
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
async fn fetch_bot(&self, id: &str) -> Result<Bot> {
|
||||||
let bots = self.bots.lock().await;
|
let bots = self.bots.lock().await;
|
||||||
@@ -22,15 +33,20 @@ impl AbstractBots for ReferenceDb {
|
|||||||
.ok_or_else(|| create_error!(NotFound))
|
.ok_or_else(|| create_error!(NotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert new bot into the database
|
/// Fetch bots owned by a user
|
||||||
async fn insert_bot(&self, bot: &Bot) -> Result<()> {
|
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
||||||
let mut bots = self.bots.lock().await;
|
let bots = self.bots.lock().await;
|
||||||
if bots.contains_key(&bot.id) {
|
Ok(bots
|
||||||
Err(create_database_error!("insert", "bot"))
|
.values()
|
||||||
} else {
|
.filter(|bot| bot.owner == user_id)
|
||||||
bots.insert(bot.id.to_string(), bot.clone());
|
.cloned()
|
||||||
Ok(())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the number of bots owned by a user
|
||||||
|
async fn get_number_of_bots_by_user(&self, user_id: &str) -> Result<usize> {
|
||||||
|
let bots = self.bots.lock().await;
|
||||||
|
Ok(bots.values().filter(|bot| bot.owner == user_id).count())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update bot with new information
|
/// Update bot with new information
|
||||||
@@ -63,20 +79,4 @@ impl AbstractBots for ReferenceDb {
|
|||||||
Err(create_error!(NotFound))
|
Err(create_error!(NotFound))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch bots owned by a user
|
|
||||||
async fn fetch_bots_by_user(&self, user_id: &str) -> Result<Vec<Bot>> {
|
|
||||||
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<usize> {
|
|
||||||
let bots = self.bots.lock().await;
|
|
||||||
Ok(bots.values().filter(|bot| bot.owner == user_id).count())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user