diff --git a/src/item.rs b/src/item.rs index 15c0deb..af2acce 100644 --- a/src/item.rs +++ b/src/item.rs @@ -5,11 +5,6 @@ use mongodb::{bson::doc, ClientSession, Collection}; // todo : api key auth -// case: export -// used an item -> demand -// mark as used -// update db - // ITEM // VARIANTS // QUANTIZATION @@ -30,6 +25,13 @@ macro_rules! collect_results { }}; } +macro_rules! cdb_col { + ($db:expr, $col:expr) => { + $db.database("cdb") + .collection::($col) + }; +} + macro_rules! get_mongo { () => { mongodb::Client::with_uri_str(std::env::var("DB_URI").unwrap()) @@ -360,9 +362,37 @@ impl Transaction { } impl Variant { - pub async fn demand(&self, uuid: &str) -> String { - // todo : transaction_out + receipt - todo!() + pub async fn demand(&self, uuid: &str, price: Price, destination: String) -> Option { + let db = get_mongo!(); + + // check if supply transaction exists + let supply_t = cdb_col!(db, "supply"); + if supply_t + .find_one(doc! { "_id": uuid }, None) + .await + .unwrap() + .is_none() + { + return None; + } + + // todo : demand batch + + // mark as used + let demand_t = cdb_col!(db, "demand"); + demand_t + .insert_one( + doc! { + "_id": uuid, + "destination": destination, + "price": price.as_bson() + }, + None, + ) + .await + .unwrap(); + + Some(uuid.to_string()) } /// Records a supply transaction in the database. @@ -382,7 +412,7 @@ impl Variant { let mut ses = db.start_session(None).await.unwrap(); let col: mongodb::Collection = - ses.client().database("cdb").collection("transactions"); + ses.client().database("cdb").collection("supply"); let mut transactions = vec![]; for _ in 0..amount {