This commit is contained in:
JMARyA 2024-09-23 09:26:11 +02:00
parent 7f32f2429e
commit 65bfb5f8e2
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 60 additions and 55 deletions

View file

@ -1,10 +1,12 @@
use std::collections::HashMap;
use mongod::Model;
use crate::item::Item;
/// Item database
pub struct ItemDB {
index: mdq::Index,
index: HashMap<String, Item>,
}
impl ItemDB {
@ -13,34 +15,27 @@ impl ItemDB {
/// The directory should contain markdown documents with valid frontmatter to be parsed into `Item`s
pub async fn new(dir: &str) -> Self {
// scan for markdown item entries
let index = mdq::Index::new(dir, true);
let index = mdq::Index::new(dir, false);
let mut items = HashMap::new();
for item in &index.documents {
let item = Item::new(item);
item.insert_overwrite().await.unwrap();
log::info!("Adding item {} to DB", item.name);
items.insert(item._id.clone(), item);
}
Self { index }
Self { index: items }
}
/// Retrieves an item by name
pub fn get_item(&self, item: &str) -> Option<Item> {
self.index
.documents
.iter()
.map(Item::new) // <-- todo : performance?
.find(|x| x._id == item)
pub fn get_item(&self, item: &str) -> Option<&Item> {
self.index.get(item)
}
/// Get all items
pub fn items(&self) -> Vec<String> {
let mut ret = vec![];
for item in &self.index.documents {
let item = Item::new(item);
ret.push(item._id.clone());
}
ret
pub fn items(&self) -> Vec<&String> {
self.index.keys().collect()
}
}