This commit is contained in:
JMARyA 2024-05-03 10:58:34 +02:00
parent fa9f394070
commit dccdf7fffe
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -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::<mongodb::bson::Document>($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<String> {
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<mongodb::bson::Document> =
ses.client().database("cdb").collection("transactions");
ses.client().database("cdb").collection("supply");
let mut transactions = vec![];
for _ in 0..amount {