remove transactions mongo

This commit is contained in:
JMARyA 2024-05-10 11:03:34 +02:00
parent 39905f53c2
commit 985296d366
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 16 additions and 26 deletions

View file

@ -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::<mongodb::bson::Document>("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::<mongodb::bson::Document>("cache")
.find_one_and_update_with_session(doc! { "_id": "inventory"}, update, options, ses)
.find_one_and_update(doc! { "_id": "inventory"}, update, options)
.await
.unwrap();
}

View file

@ -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<mongodb::bson::Document> =
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<mongodb::bson::Document> = ses
.client()
.database("cdb")
.collection("transactions_batch");
col.insert_one_with_session(batch.as_doc(), None, &mut ses)
.await
.unwrap();
let col: mongodb::Collection<mongodb::bson::Document> =
db.database("cdb").collection("transactions_batch");
col.insert_one(batch.as_doc(), None).await.unwrap();
batch.uuid
};
ses.commit_transaction().await.unwrap();
ret_uuid
}