fix reinit
This commit is contained in:
parent
6a34099e17
commit
a8e72efea0
2 changed files with 27 additions and 11 deletions
|
@ -30,6 +30,13 @@ macro_rules! get_mongo {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! id_of {
|
||||||
|
($id:expr) => {
|
||||||
|
doc! { "_id": $id}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ItemDB {
|
pub struct ItemDB {
|
||||||
index: mdq::Index,
|
index: mdq::Index,
|
||||||
mongodb: mongodb::Client,
|
mongodb: mongodb::Client,
|
||||||
|
|
23
src/item.rs
23
src/item.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::collect_results;
|
use crate::{collect_results, id_of};
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use mongodb::{bson::doc, ClientSession, Collection};
|
use mongodb::{bson::doc, ClientSession, Collection};
|
||||||
|
|
||||||
|
@ -60,19 +60,28 @@ impl ItemEntry {
|
||||||
log::info!("Adding item {} to DB", self.name);
|
log::info!("Adding item {} to DB", self.name);
|
||||||
let items: Collection<mongodb::bson::Document> =
|
let items: Collection<mongodb::bson::Document> =
|
||||||
mongodb.database("cdb").collection("items");
|
mongodb.database("cdb").collection("items");
|
||||||
items
|
|
||||||
.insert_one(
|
let doc = mongodb::bson::doc! {
|
||||||
mongodb::bson::doc! {
|
|
||||||
"_id": self.name.clone(),
|
"_id": self.name.clone(),
|
||||||
"name": self.name.clone(),
|
"name": self.name.clone(),
|
||||||
"category": self.category.clone()
|
"category": self.category.clone()
|
||||||
},
|
};
|
||||||
None,
|
|
||||||
)
|
if items
|
||||||
|
.find_one(id_of!(&self.name), None)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.is_none()
|
||||||
|
{
|
||||||
|
items.insert_one(doc, None).await.unwrap();
|
||||||
|
} else {
|
||||||
|
items
|
||||||
|
.find_one_and_update(id_of!(&self.name), doc, None)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Represents a single item in a collection of physical goods.
|
/// Represents a single item in a collection of physical goods.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in a new issue