From 985296d366382d44e660bf838d5ce7d97a61ac89 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 10 May 2024 11:03:34 +0200 Subject: [PATCH] remove transactions mongo --- src/cache.rs | 18 ++++++++++-------- src/variant.rs | 24 ++++++------------------ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index a36e089..63d35f0 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -5,7 +5,9 @@ use crate::get_mongo; pub struct InventoryCache {} impl InventoryCache { - pub async fn push(uuid: &str, ses: &mut ClientSession) { + pub async fn push(uuid: &str) { + let db = get_mongo!(); + // todo : if not exists? let update = doc! { "$push": { "transactions": uuid } }; @@ -14,11 +16,10 @@ impl InventoryCache { .return_document(mongodb::options::ReturnDocument::After) .build(); - let result = ses - .client() + let result = db .database("cdb") .collection::("cache") - .find_one_and_update_with_session(doc! { "_id": "inventory"}, update, options, ses) + .find_one_and_update(doc! { "_id": "inventory"}, update, options) .await .unwrap(); } @@ -40,7 +41,9 @@ impl InventoryCache { .collect() } - pub async fn remove(uuid: &str, ses: &mut ClientSession) { + pub async fn remove(uuid: &str) { + let db = get_mongo!(); + let update = doc! { "$pull": { "transactions": uuid}}; let options = mongodb::options::FindOneAndUpdateOptions::builder() @@ -48,11 +51,10 @@ impl InventoryCache { .return_document(mongodb::options::ReturnDocument::After) .build(); - let result = ses - .client() + let result = db .database("cdb") .collection::("cache") - .find_one_and_update_with_session(doc! { "_id": "inventory"}, update, options, ses) + .find_one_and_update(doc! { "_id": "inventory"}, update, options) .await .unwrap(); } diff --git a/src/variant.rs b/src/variant.rs index c59b1f6..9c3c40f 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -93,11 +93,8 @@ impl Variant { pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String { let db = get_mongo!(); - let mut ses = db.start_session(None).await.unwrap(); - ses.start_transaction(None).await.unwrap(); - let col: mongodb::Collection = - ses.client().database("cdb").collection("supply"); + db.database("cdb").collection("supply"); let mut transactions = vec![]; for _ in 0..amount { @@ -110,13 +107,10 @@ impl Variant { } for transaction in &transactions { - let r = col - .insert_one_with_session(transaction.as_doc(), None, &mut ses) - .await - .unwrap(); + let r = col.insert_one(transaction.as_doc(), None).await.unwrap(); // update cache - InventoryCache::push(&transaction.uuid, &mut ses).await; + InventoryCache::push(&transaction.uuid).await; } // batch transaction @@ -125,18 +119,12 @@ impl Variant { } else { let batch = BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect()); - let col: mongodb::Collection = ses - .client() - .database("cdb") - .collection("transactions_batch"); - col.insert_one_with_session(batch.as_doc(), None, &mut ses) - .await - .unwrap(); + let col: mongodb::Collection = + db.database("cdb").collection("transactions_batch"); + col.insert_one(batch.as_doc(), None).await.unwrap(); batch.uuid }; - ses.commit_transaction().await.unwrap(); - ret_uuid }