diff --git a/src/flow.rs b/src/flow.rs index 07f5cf7..12e9ce4 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -47,7 +47,7 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use crate::item::Item; -use crate::routes::item::{item_does_not_exist_error, variant_does_not_exist_error}; +use crate::routes::item::{item_does_not_exist_error, variant_does_not_exist_error, SupplyForm}; use crate::routes::{api_error, ApiError}; use crate::transaction::{Price, Transaction}; @@ -156,42 +156,35 @@ impl Flow { pub async fn end_with_produce( self, - produced: &[HashMap], + produced: &[SupplyForm], ) -> Result>, ApiError> { let mut ret = HashMap::new(); let mut t_create = Vec::new(); let mut produced_ref = Vec::with_capacity(ret.len()); for prod in produced { - for (item_variant, amount) in prod { - let (item, variant) = item_variant - .split_once("::") - .ok_or_else(|| api_error("Bad Request"))?; - let t = Item::get(item) - .await - .ok_or_else(item_does_not_exist_error)? - .variant(variant) - .ok_or_else(variant_does_not_exist_error)?; + let t = Item::get(&prod.item) + .await + .ok_or_else(item_does_not_exist_error)? + .variant(&prod.variant) + .ok_or_else(variant_does_not_exist_error)?; - t_create.push((t, amount)); + t_create.push((t, prod)); } - } - for (item, amount) in t_create { - for _ in 0..*amount { + for (item, info) in t_create { let t = item .supply( Price::zero(), Some(&format!("flow::{}::{}", self.kind.id(), self._id)), - None, - None, + info.location.as_ref().map(|x| x.as_str()), + info.note.as_ref().map(|x| x.as_str()), ) .await; ret.entry(item.item_variant_id().clone()) .or_insert(Vec::new()) .push(t._id.clone()); produced_ref.push(t.reference()); - } } self.change() diff --git a/src/routes/flow.rs b/src/routes/flow.rs index a134e84..6fd799e 100644 --- a/src/routes/flow.rs +++ b/src/routes/flow.rs @@ -15,7 +15,7 @@ use crate::{ transaction::{Price, Transaction}, }; -use super::ApiError; +use super::{item::SupplyForm, ApiError}; #[get("/flow//info")] pub async fn flow_info( @@ -129,7 +129,7 @@ pub struct CreateFlow { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct EndFlow { - pub produced: Option>>, + pub produced: Option>, } #[post("/flow/", data = "
")] diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index 8b32acf..fdb5f98 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -1,7 +1,7 @@ use mongod::ToAPI; use rocket::serde::json::Json; use rocket::{get, post, State}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_json::json; use crate::check_auth; @@ -14,14 +14,14 @@ use crate::{ use super::{item_does_not_exist_error, variant_does_not_exist_error}; -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, Clone, Serialize)] pub struct SupplyForm { - item: String, - variant: String, - price: String, - origin: Option, - location: Option, - note: Option, + pub item: String, + pub variant: String, + pub price: String, + pub origin: Option, + pub location: Option, + pub note: Option, } /// Route for supply action. Creates a new Transaction for the specified Item Variant.