From e26c8b3469782b63ff113ba0295ee95ca2d7331a Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 13 Sep 2024 14:17:55 +0200 Subject: [PATCH] fix flows --- Cargo.lock | 18 +++++++-------- src/flow.rs | 51 +++++++++++++++++++++++++----------------- src/main.rs | 5 ++++- src/routes/flow.rs | 3 +-- src/routes/item/mod.rs | 2 +- src/routes/mod.rs | 4 ++-- src/variant.rs | 4 ++++ 7 files changed, 52 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 605c377..80f48b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,9 +225,9 @@ dependencies = [ [[package]] name = "bson" -version = "2.11.0" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a88e82b9106923b5c4d6edfca9e7db958d4e98a478ec115022e81b9b38e2c8" +checksum = "80cf6f7806607bd58ad490bab34bf60e25455ea4aaf995f897a13324d41ea580" dependencies = [ "ahash", "base64 0.13.1", @@ -1281,8 +1281,8 @@ dependencies = [ [[package]] name = "mongod" -version = "0.2.1" -source = "git+https://git.hydrar.de/jmarya/mongod#949340a7f28810768e408f4039dc1f1dc9cc1a82" +version = "0.2.2" +source = "git+https://git.hydrar.de/jmarya/mongod#b0bae64efcfdc833c3b9e8508bf228c9299c1d21" dependencies = [ "chrono", "futures", @@ -1298,7 +1298,7 @@ dependencies = [ [[package]] name = "mongod_derive" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/mongod#949340a7f28810768e408f4039dc1f1dc9cc1a82" +source = "git+https://git.hydrar.de/jmarya/mongod#b0bae64efcfdc833c3b9e8508bf228c9299c1d21" dependencies = [ "case", "proc-macro2", @@ -1658,9 +1658,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ "bitflags 2.6.0", ] @@ -1929,9 +1929,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.36" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", diff --git a/src/flow.rs b/src/flow.rs index 69e6e2f..e3ca96d 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -45,6 +45,8 @@ 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::{api_error, ApiError}; use crate::transaction::{Price, Transaction}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -133,30 +135,39 @@ impl Flow { pub async fn end_with_produce( self, produced: &[HashMap], - ) -> HashMap> { + ) -> 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("::").unwrap(); - for _ in 0..*amount { - let t = Item::get(item) - .await - .unwrap() - .variant(variant) - .unwrap() - .supply( - Price::zero(), - Some(&format!("flow::{}::{}", self.kind.id(), self._id)), - None, - ) - .await; - ret.entry(item_variant.clone()) - .or_insert(Vec::new()) - .push(t._id.clone()); - produced_ref.push(t.reference()); - } + 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)?; + + t_create.push((t, amount)); + } + } + + for (item, amount) in t_create { + for _ in 0..*amount { + let t = item + .supply( + Price::zero(), + Some(&format!("flow::{}::{}", self.kind.id(), self._id)), + None, + ) + .await; + ret.entry(item.item_variant_id().clone()) + .or_insert(Vec::new()) + .push(t._id.clone()); + produced_ref.push(t.reference()); } } @@ -170,7 +181,7 @@ impl Flow { .await .unwrap(); - ret + Ok(ret) } pub async fn continue_next(self, next_flow: &Flow) -> Self { diff --git a/src/main.rs b/src/main.rs index 2f4bfa4..fd01f4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,10 @@ async fn rocket() -> _ { routes::flow::flows_list, routes::item::expired_items_route, routes::item::min_items_route, - routes::item::variant_price_history_by_origin + routes::item::variant_price_history_by_origin, + routes::flow::end_flow_route, + routes::flow::continue_flow_route, + routes::flow::create_flow_route ], ) .manage(itemdb) diff --git a/src/routes/flow.rs b/src/routes/flow.rs index 2f6f0b1..8ceea1a 100644 --- a/src/routes/flow.rs +++ b/src/routes/flow.rs @@ -123,8 +123,7 @@ pub async fn end_flow_route(id: &str, form: Json) -> FallibleApiRespons .ok_or_else(|| api_error("Flow not found"))?; if let Some(produced) = &form.produced { - // todo : add transactions - let prod = flow.end_with_produce(produced).await; + let prod = flow.end_with_produce(produced).await?; Ok(json!({"produced": prod})) } else { flow.end().await; diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 4b0c479..f94aa01 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -1,5 +1,5 @@ mod demand; -mod error; +pub mod error; mod location; mod stat; mod supply; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index f76b643..19dda34 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -6,10 +6,10 @@ use serde_json::json; pub mod flow; pub mod item; -type ApiError = BadRequest; +pub type ApiError = BadRequest; type FallibleApiResponse = Result; -fn api_error(msg: &str) -> ApiError { +pub fn api_error(msg: &str) -> ApiError { BadRequest(json!({ "error": msg })) diff --git a/src/variant.rs b/src/variant.rs index 3ce32df..084814a 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -76,6 +76,10 @@ impl Variant { } } + pub fn item_variant_id(&self) -> String { + format!("{}::{}", self.item, self.variant) + } + /// Returns the IDs of Transactions from this Item Variant. pub async fn supply_log(&self) -> Vec { let filter = doc! {