create BotInstance collection
This commit is contained in:
parent
a7433cd8cc
commit
4c149b6922
51
src/db/bots.rs
Normal file
51
src/db/bots.rs
Normal 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?)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
pub mod application;
|
pub mod application;
|
||||||
|
pub mod bots;
|
||||||
pub mod callback_info;
|
pub mod callback_info;
|
||||||
pub mod message_forward;
|
pub mod message_forward;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user