remove transactions mongo
This commit is contained in:
parent
39905f53c2
commit
985296d366
2 changed files with 16 additions and 26 deletions
18
src/cache.rs
18
src/cache.rs
|
@ -5,7 +5,9 @@ use crate::get_mongo;
|
||||||
pub struct InventoryCache {}
|
pub struct InventoryCache {}
|
||||||
|
|
||||||
impl 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?
|
// todo : if not exists?
|
||||||
let update = doc! { "$push": { "transactions": uuid } };
|
let update = doc! { "$push": { "transactions": uuid } };
|
||||||
|
|
||||||
|
@ -14,11 +16,10 @@ impl InventoryCache {
|
||||||
.return_document(mongodb::options::ReturnDocument::After)
|
.return_document(mongodb::options::ReturnDocument::After)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let result = ses
|
let result = db
|
||||||
.client()
|
|
||||||
.database("cdb")
|
.database("cdb")
|
||||||
.collection::<mongodb::bson::Document>("cache")
|
.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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -40,7 +41,9 @@ impl InventoryCache {
|
||||||
.collect()
|
.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 update = doc! { "$pull": { "transactions": uuid}};
|
||||||
|
|
||||||
let options = mongodb::options::FindOneAndUpdateOptions::builder()
|
let options = mongodb::options::FindOneAndUpdateOptions::builder()
|
||||||
|
@ -48,11 +51,10 @@ impl InventoryCache {
|
||||||
.return_document(mongodb::options::ReturnDocument::After)
|
.return_document(mongodb::options::ReturnDocument::After)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let result = ses
|
let result = db
|
||||||
.client()
|
|
||||||
.database("cdb")
|
.database("cdb")
|
||||||
.collection::<mongodb::bson::Document>("cache")
|
.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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,11 +93,8 @@ impl Variant {
|
||||||
pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String {
|
pub async fn supply(&self, amount: usize, price: Price, origin: &str) -> String {
|
||||||
let db = get_mongo!();
|
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> =
|
let col: mongodb::Collection<mongodb::bson::Document> =
|
||||||
ses.client().database("cdb").collection("supply");
|
db.database("cdb").collection("supply");
|
||||||
|
|
||||||
let mut transactions = vec![];
|
let mut transactions = vec![];
|
||||||
for _ in 0..amount {
|
for _ in 0..amount {
|
||||||
|
@ -110,13 +107,10 @@ impl Variant {
|
||||||
}
|
}
|
||||||
|
|
||||||
for transaction in &transactions {
|
for transaction in &transactions {
|
||||||
let r = col
|
let r = col.insert_one(transaction.as_doc(), None).await.unwrap();
|
||||||
.insert_one_with_session(transaction.as_doc(), None, &mut ses)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// update cache
|
// update cache
|
||||||
InventoryCache::push(&transaction.uuid, &mut ses).await;
|
InventoryCache::push(&transaction.uuid).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// batch transaction
|
// batch transaction
|
||||||
|
@ -125,18 +119,12 @@ impl Variant {
|
||||||
} else {
|
} else {
|
||||||
let batch =
|
let batch =
|
||||||
BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect());
|
BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect());
|
||||||
let col: mongodb::Collection<mongodb::bson::Document> = ses
|
let col: mongodb::Collection<mongodb::bson::Document> =
|
||||||
.client()
|
db.database("cdb").collection("transactions_batch");
|
||||||
.database("cdb")
|
col.insert_one(batch.as_doc(), None).await.unwrap();
|
||||||
.collection("transactions_batch");
|
|
||||||
col.insert_one_with_session(batch.as_doc(), None, &mut ses)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
batch.uuid
|
batch.uuid
|
||||||
};
|
};
|
||||||
|
|
||||||
ses.commit_transaction().await.unwrap();
|
|
||||||
|
|
||||||
ret_uuid
|
ret_uuid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue