work
This commit is contained in:
parent
e32c3825a0
commit
4de834b385
2 changed files with 38 additions and 42 deletions
74
src/item.rs
74
src/item.rs
|
@ -141,25 +141,6 @@ impl Item {
|
||||||
hashset.into_iter().collect()
|
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> {
|
pub async fn get_variants(&self) -> Vec<String> {
|
||||||
let variants: Collection<ItemVariantEntry> =
|
let variants: Collection<ItemVariantEntry> =
|
||||||
self.db.database("cdb").collection("item_variants");
|
self.db.database("cdb").collection("item_variants");
|
||||||
|
@ -227,15 +208,22 @@ pub struct Price {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Price {
|
impl Price {
|
||||||
fn new(price: &str) -> Self {
|
pub fn new(value: f64, currency: &str) -> Self {
|
||||||
// todo : implement
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
value: 0.0,
|
value,
|
||||||
currency: "€".to_string(),
|
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 {
|
fn as_bson(&self) -> mongodb::bson::Bson {
|
||||||
mongodb::bson::bson!({
|
mongodb::bson::bson!({
|
||||||
"value": self.value,
|
"value": self.value,
|
||||||
|
@ -253,9 +241,11 @@ impl From<mongodb::bson::Document> for Price {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Price {
|
impl TryFrom<String> for Price {
|
||||||
fn from(val: std::string::String) -> Self {
|
type Error = ();
|
||||||
Self::new(&val)
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
Self::parse(&value).ok_or(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,25 +394,27 @@ impl Variant {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let batch = BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect());
|
// batch transaction
|
||||||
let col: mongodb::Collection<mongodb::bson::Document> = ses
|
let ret_uuid = if amount == 1 {
|
||||||
.client()
|
transactions.first().unwrap().uuid.clone()
|
||||||
.database("cdb")
|
} else {
|
||||||
.collection("transactions_batch");
|
let batch =
|
||||||
col.insert_one_with_session(batch.as_doc(), None, &mut ses)
|
BatchTransaction::new(transactions.iter().map(|x| x.uuid.clone()).collect());
|
||||||
.await
|
let col: mongodb::Collection<mongodb::bson::Document> = ses
|
||||||
.unwrap();
|
.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
|
// todo : transaction overlap cache -> scale
|
||||||
|
|
||||||
ses.commit_transaction().await.unwrap();
|
ses.commit_transaction().await.unwrap();
|
||||||
|
|
||||||
if amount == 1 {
|
ret_uuid
|
||||||
transactions.first().unwrap().uuid.clone()
|
|
||||||
} else {
|
|
||||||
batch.uuid.clone()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,10 +34,14 @@ pub async fn supply_route(
|
||||||
.variant(&form.variant)
|
.variant(&form.variant)
|
||||||
.await
|
.await
|
||||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
||||||
|
|
||||||
let transaction_id = variant
|
let transaction_id = variant
|
||||||
.supply(
|
.supply(
|
||||||
form.amount.unwrap_or(1),
|
form.amount.unwrap_or(1),
|
||||||
form.price.clone().into(),
|
form.price
|
||||||
|
.clone()
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| bad_req("Price malformed"))?,
|
||||||
&form.origin,
|
&form.origin,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
Loading…
Reference in a new issue