This commit is contained in:
JMARyA 2024-05-03 10:30:05 +02:00
parent e32c3825a0
commit 4de834b385
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 38 additions and 42 deletions

View file

@ -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<ItemInventoryEntry> =
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<String> {
let variants: Collection<ItemVariantEntry> =
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<Self> {
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<mongodb::bson::Document> for Price {
}
}
impl From<String> for Price {
fn from(val: std::string::String) -> Self {
Self::new(&val)
impl TryFrom<String> for Price {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
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<mongodb::bson::Document> = 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<mongodb::bson::Document> = 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
}
}

View file

@ -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;