2024-05-03 16:22:59 +00:00
|
|
|
use crate::item::{Item, ItemEntry};
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! collect_results {
|
|
|
|
($res:expr) => {{
|
|
|
|
let mut ret = vec![];
|
|
|
|
|
|
|
|
while let Some(doc) = $res.try_next().await.unwrap() {
|
|
|
|
ret.push(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! cdb_col {
|
|
|
|
($db:expr, $col:expr) => {
|
|
|
|
$db.database("cdb")
|
|
|
|
.collection::<mongodb::bson::Document>($col)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! get_mongo {
|
|
|
|
() => {
|
|
|
|
mongodb::Client::with_uri_str(std::env::var("DB_URI").unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:45:23 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! id_of {
|
|
|
|
($id:expr) => {
|
|
|
|
doc! { "_id": $id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:22:59 +00:00
|
|
|
pub struct ItemDB {
|
|
|
|
index: mdq::Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemDB {
|
2024-05-10 08:56:07 +00:00
|
|
|
pub async fn new(dir: &str) -> Self {
|
2024-05-03 16:22:59 +00:00
|
|
|
// scan for markdown item entries
|
|
|
|
let index = mdq::Index::new(dir, true);
|
|
|
|
let mongodb = get_mongo!();
|
|
|
|
|
|
|
|
for item in &index.documents {
|
|
|
|
let item = ItemEntry::new(item.clone());
|
|
|
|
item.init_db(&mongodb).await;
|
|
|
|
}
|
|
|
|
|
2024-05-10 08:56:07 +00:00
|
|
|
Self { index }
|
2024-05-03 16:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves an item by name
|
|
|
|
pub fn get_item(&self, item: &str) -> Option<Item> {
|
|
|
|
Some(Item::new(
|
|
|
|
self.index
|
|
|
|
.documents
|
|
|
|
.iter()
|
|
|
|
.map(|x| ItemEntry::new(x.clone()))
|
|
|
|
.find(|x| x.name == item)?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all items
|
|
|
|
pub fn items(&self) -> Vec<String> {
|
|
|
|
let mut ret = vec![];
|
|
|
|
for item in &self.index.documents {
|
|
|
|
let item = ItemEntry::new(item.clone());
|
|
|
|
ret.push(item.name);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|