From 4de834b3850557d833173b67ce8575af180e1310 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 3 May 2024 10:30:05 +0200 Subject: [PATCH] work --- src/item.rs | 74 +++++++++++++++++++----------------------- src/routes/item/mod.rs | 6 +++- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/item.rs b/src/item.rs index 1b04f95..3b074ad 100644 --- a/src/item.rs +++ b/src/item.rs @@ -141,25 +141,6 @@ impl Item { hashset.into_iter().collect() } - pub async fn add_inventory(&self, variant: &str, origin: &str, price: f64) -> String { - // todo : implement - let quantities: Collection = - self.db.database("cdb").collection("item_quantities"); - let id = quantities - .insert_one( - ItemInventoryEntry { - item_name: self.item.name.clone(), - variant_name: variant.to_owned(), - origin: origin.to_owned(), - price, - }, - None, - ) - .await - .unwrap(); - return id.inserted_id.as_object_id().unwrap().to_string(); - } - pub async fn get_variants(&self) -> Vec { let variants: Collection = self.db.database("cdb").collection("item_variants"); @@ -227,15 +208,22 @@ pub struct Price { } impl Price { - fn new(price: &str) -> Self { - // todo : implement - + pub fn new(value: f64, currency: &str) -> Self { Self { - value: 0.0, - currency: "€".to_string(), + value, + currency: currency.to_string(), } } + fn parse(price: &str) -> Option { + let (value, currency) = price.split_once(' ')?; + + Some(Self { + value: value.parse().ok()?, + currency: currency.to_string(), + }) + } + fn as_bson(&self) -> mongodb::bson::Bson { mongodb::bson::bson!({ "value": self.value, @@ -253,9 +241,11 @@ impl From for Price { } } -impl From for Price { - fn from(val: std::string::String) -> Self { - Self::new(&val) +impl TryFrom for Price { + type Error = (); + + fn try_from(value: String) -> Result { + Self::parse(&value).ok_or(()) } } @@ -404,25 +394,27 @@ impl Variant { .unwrap(); } - 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(); + // batch transaction + let ret_uuid = if amount == 1 { + transactions.first().unwrap().uuid.clone() + } 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(); + batch.uuid + }; - // todo : batch transaction // todo : transaction overlap cache -> scale ses.commit_transaction().await.unwrap(); - if amount == 1 { - transactions.first().unwrap().uuid.clone() - } else { - batch.uuid.clone() - } + ret_uuid } } diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 77d8b44..584a405 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -34,10 +34,14 @@ pub async fn supply_route( .variant(&form.variant) .await .ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?; + let transaction_id = variant .supply( form.amount.unwrap_or(1), - form.price.clone().into(), + form.price + .clone() + .try_into() + .map_err(|_| bad_req("Price malformed"))?, &form.origin, ) .await;