diff --git a/src/db/bots.rs b/src/db/bots.rs new file mode 100644 index 0000000..44bc500 --- /dev/null +++ b/src/db/bots.rs @@ -0,0 +1,51 @@ +use bson::doc; +use bson::oid::ObjectId; +use chrono::{DateTime, FixedOffset, Local}; +use futures::StreamExt; +use futures::TryStreamExt; +use serde::{Deserialize, Serialize}; + +use super::DbCollection; +use super::DbResult; +use crate::db::GetCollection; +use crate::query_call_consume; +use crate::CallDB; + +#[derive(Serialize, Deserialize, Default)] +pub struct BotInstance { + pub _id: bson::oid::ObjectId, + pub name: String, + pub token: String, + pub script: String, + pub created_at: DateTime, +} + +impl DbCollection for BotInstance { + const COLLECTION: &str = "bots"; +} + +impl BotInstance { + pub fn new(name: String, token: String, script: String) -> Self { + Self { + _id: Default::default(), + name, + token, + script, + created_at: Local::now().into(), + } + } + + query_call_consume!(store, self, db, Self, { + let bi = db.get_collection::().await; + + bi.insert_one(&self).await?; + + Ok(self) + }); + + pub async fn get_all(db: &mut D) -> DbResult> { + let bi = db.get_collection::().await; + + Ok(bi.find(doc! {}).await?.try_collect().await?) + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index d1e22e6..b703147 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,4 +1,5 @@ pub mod application; +pub mod bots; pub mod callback_info; pub mod message_forward;