create BotInstance collection

This commit is contained in:
Akulij 2025-05-26 20:10:54 +05:00
parent a7433cd8cc
commit 4c149b6922
2 changed files with 52 additions and 0 deletions

51
src/db/bots.rs Normal file
View File

@ -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<FixedOffset>,
}
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::<Self>().await;
bi.insert_one(&self).await?;
Ok(self)
});
pub async fn get_all<D: CallDB>(db: &mut D) -> DbResult<Vec<Self>> {
let bi = db.get_collection::<Self>().await;
Ok(bi.find(doc! {}).await?.try_collect().await?)
}
}

View File

@ -1,4 +1,5 @@
pub mod application;
pub mod bots;
pub mod callback_info;
pub mod message_forward;