From acf5ebae78399da09b81f38bf39d1ceb594cc5bd Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 28 Aug 2024 09:38:10 +0200 Subject: [PATCH] refactor --- src/db.rs | 15 ++++++++------- src/item.rs | 34 +++++++++++++++++++++++---------- src/routes/item/demand.rs | 6 ++++-- src/routes/item/supply.rs | 4 ++++ src/transaction.rs | 4 ++-- src/variant.rs | 40 ++++++++++++++------------------------- 6 files changed, 56 insertions(+), 47 deletions(-) diff --git a/src/db.rs b/src/db.rs index cff5906..e54638c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,3 +1,5 @@ +use mongod::Model; + use crate::item::Item; /// Item database @@ -15,6 +17,7 @@ impl ItemDB { for item in &index.documents { let item = Item::new(item); + item.insert_overwrite().await.unwrap(); log::info!("Adding item {} to DB", item.name); } @@ -23,13 +26,11 @@ impl ItemDB { /// Retrieves an item by name pub fn get_item(&self, item: &str) -> Option { - Some( - self.index - .documents - .iter() - .map(Item::new) // <-- todo : performance? - .find(|x| x.name == item)?, - ) + self.index + .documents + .iter() + .map(Item::new) // <-- todo : performance? + .find(|x| x.name == item) } /// Get all items diff --git a/src/item.rs b/src/item.rs index 5b55fe5..21fe673 100644 --- a/src/item.rs +++ b/src/item.rs @@ -27,14 +27,14 @@ use crate::variant::Variant; /// the name of the item and its category. #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct Item { - /// The ID is the same as the name + /// The ID pub _id: String, /// The name of the Item pub name: String, /// Category of the Item - pub category: String, + pub category: Option, /// The variants of an Item. - /// Each key of the `HashMap<_>` is the name of a variant and contains a `Variant` + /// Each key of the `HashMap<_>` is the ID of a variant and contains a `Variant` pub variants: HashMap, } @@ -47,17 +47,25 @@ impl Validate for Item { impl Item { /// Creates a new `Item` from a parsed markdown document pub fn new(doc: &mdq::Document) -> Self { - let name = std::path::Path::new(&doc.path) + let id = std::path::Path::new(&doc.path) .file_stem() .unwrap() .to_str() .unwrap() .to_string(); + let category = doc .frontmatter .as_mapping() .unwrap() .get("category") + .map(|x| x.as_str().unwrap().to_string()); + + let name = doc + .frontmatter + .as_mapping() + .unwrap() + .get("name") .unwrap() .as_str() .unwrap() @@ -65,22 +73,28 @@ impl Item { let mut variants = HashMap::new(); - for (variant_name, variant) in doc + let variants_lst = doc .frontmatter .as_mapping() .unwrap() .get("variants") .map(|x| x.as_mapping().unwrap().clone()) - .unwrap_or_default() - { + .unwrap(); + + assert!( + !variants_lst.is_empty(), + "Item {id} needs at least one variant." + ); + + for (variant_id, variant) in variants_lst { variants.insert( - variant_name.as_str().unwrap().to_string(), - Variant::from_yml(&variant, variant_name.as_str().unwrap(), &name), + variant_id.as_str().unwrap().to_string(), + Variant::from_yml(&variant, variant_id.as_str().unwrap(), &id), ); } Self { - _id: name.clone(), + _id: id, name, category, variants, diff --git a/src/routes/item/demand.rs b/src/routes/item/demand.rs index 32ff883..a068302 100644 --- a/src/routes/item/demand.rs +++ b/src/routes/item/demand.rs @@ -18,9 +18,10 @@ pub struct DemandForm { price: String, } +/// Consumes a Transaction with Price and Destination #[post("/demand", data = "")] pub async fn demand_route(f: Json) -> FallibleApiResponse { - let uuid = Variant::demand( + Variant::demand( &f.uuid, f.price .clone() @@ -31,9 +32,10 @@ pub async fn demand_route(f: Json) -> FallibleApiResponse { .await .ok_or_else(|| api_error("Demand failed"))?; - Ok(json!({"uuid": uuid})) + Ok(json!({"ok": 1})) } +/// Returns all used transactions for Item Variant #[get("/item///demand")] pub async fn demand_log_route( item_id: &str, diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index bdfb212..decfcd0 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -18,6 +18,7 @@ pub struct SupplyForm { origin: Option, } +/// Route for supply action. Creates a new Transaction for the specified Item Variant. #[post("/supply", data = "
")] pub async fn supply_route(form: Json, itemdb: &State) -> FallibleApiResponse { println!("{form:?}"); @@ -40,6 +41,7 @@ pub async fn supply_route(form: Json, itemdb: &State) -> Fal Ok(json!({"uuid": transaction_id})) } +/// Returns a list of Transaction UUIDs for the Item Variant #[get("/item///supply")] pub async fn supply_log_route( item_id: &str, @@ -57,6 +59,7 @@ pub async fn supply_log_route( Ok(json!(transactions)) } +/// Returns current active Transactions for Item Variant #[get("/item///inventory")] pub async fn inventory_route( item_id: &str, @@ -77,6 +80,7 @@ pub async fn inventory_route( .collect::>())) } +/// Returns statistics for the Item Variant #[get("/item///stat")] pub async fn variant_stat_route( item_id: &str, diff --git a/src/transaction.rs b/src/transaction.rs index 645063d..5300f7e 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -50,7 +50,7 @@ impl Transaction { variant: variant.to_string(), price, consumed: None, - origin: origin.map(|x| x.to_string()), + origin: origin.map(std::string::ToString::to_string), timestamp: chrono::Utc::now().timestamp(), } } @@ -61,7 +61,7 @@ impl Transaction { "consumed": Consumed{destination: destination.to_string(),price, timestamp: chrono::Utc::now().timestamp() } })) .await - .ok().unwrap() + .ok().unwrap(); } pub fn api_json(&self) -> serde_json::Value { diff --git a/src/variant.rs b/src/variant.rs index 9f7929c..f4028aa 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -3,10 +3,10 @@ use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; -use crate::transaction::{Consumed, Price, Transaction}; +use crate::transaction::{Price, Transaction}; -pub fn sort_by_timestamp() -> Option { - Some(doc! { "timestamp": mongod::Sort::Descending }) +pub fn sort_by_timestamp() -> mongodb::bson::Document { + doc! { "timestamp": mongod::Sort::Descending } } pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) { @@ -39,12 +39,10 @@ pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) { pub struct Variant { /// Associated Item pub item: String, - /// Variant Name + /// Variant ID pub variant: String, - /// Amount of items this variant represents - pub amount: u64, - /// Dependencies for the item variant. - pub depends: Vec, + /// Variant Name + pub name: String, } impl Variant { @@ -53,23 +51,14 @@ impl Variant { Self { item: item.to_string(), variant: variant.to_string(), - amount: json + name: json .as_mapping() .unwrap() - .get("amount") - .map_or(1, |x| x.as_u64().unwrap()), - depends: json - .as_mapping() + .get("name") .unwrap() - .get("depends") - .map(|x| { - x.as_sequence() - .unwrap() - .iter() - .map(|x| x.as_str().unwrap().to_string()) - .collect() - }) - .unwrap_or_default(), + .as_str() + .unwrap() + .to_string(), } } @@ -167,7 +156,7 @@ impl Variant { } }, None, - sort_by_timestamp(), + Some(sort_by_timestamp()), ) .await .unwrap() @@ -205,7 +194,7 @@ impl Variant { filter.insert("origin", origin); } - Transaction::find(filter, Some(1), sort_by_timestamp()) + Transaction::find(filter, Some(1), Some(sort_by_timestamp())) .await .unwrap() .first() @@ -230,8 +219,7 @@ impl Variant { json!({ "item": self.item, "variant": self.variant, - "amount": self.amount as u32, - "depends": self.depends + "name": self.name }) } }