diff --git a/migrations/0000_init.sql b/migrations/0000_init.sql index f38a7f4..a57e570 100644 --- a/migrations/0000_init.sql +++ b/migrations/0000_init.sql @@ -27,6 +27,6 @@ CREATE TABLE transactions ( "location" TEXT, note TEXT, destination TEXT, - consumed_price NUMERIC(2), + consumed_price DOUBLE PRECISION, consumed_timestamp timestamptz ); diff --git a/src/core/flow.rs b/src/core/flow.rs index 7d49494..8917f2e 100644 --- a/src/core/flow.rs +++ b/src/core/flow.rs @@ -122,7 +122,7 @@ impl Flow { info.location.as_ref().map(|x| x.as_str()), info.note.as_ref().map(|x| x.as_str()), info.quanta, - info.properties.clone() + info.properties.clone(), ) .await; ret.entry(item.item_variant_id().clone()) diff --git a/src/core/transaction.rs b/src/core/transaction.rs index a5c8de9..233cc1a 100644 --- a/src/core/transaction.rs +++ b/src/core/transaction.rs @@ -43,12 +43,12 @@ pub struct Transaction { pub enum Origin { Flow(String), - Custom(String) + Custom(String), } pub enum Destination { Flow(String), - Custom(String) + Custom(String), } impl Transaction { @@ -60,7 +60,7 @@ impl Transaction { location: Option<&str>, note: Option<&str>, quanta: Option, - properties: Option + properties: Option, ) -> Self { sqlx::query_as("INSERT INTO transactions (item, variant, price, origin, location, note, quanta, properties) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *") .bind(item) @@ -201,7 +201,11 @@ impl Transaction { impl ToAPI for Transaction { async fn api(&self) -> serde_json::Value { let location = if let Some(loc) = &self.location { - Some(get_locations!().get(loc).unwrap().api().await) + if !loc.is_empty() { + Some(get_locations!().get(loc).unwrap().api().await) + } else { + None + } } else { None }; diff --git a/src/core/variant.rs b/src/core/variant.rs index c538df9..1204e53 100644 --- a/src/core/variant.rs +++ b/src/core/variant.rs @@ -34,8 +34,10 @@ pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Variant { /// Associated Item + #[serde(default)] pub item: String, /// Variant ID + #[serde(default)] pub variant: String, /// Variant Name pub name: String, @@ -50,13 +52,13 @@ pub struct Variant { /// Custom fields as JSON object schema pub meta: Option, /// Quantifiable - pub unit: Option + pub unit: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Quanta { pub by: String, - pub conversions: HashMap + pub conversions: HashMap, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -67,8 +69,11 @@ pub struct VariantNeedCondition { impl Variant { /// Create variant from itemdb yaml - pub fn from_yml(json: serde_yaml::Value, variant: &str, item: &str) -> Self { - serde_yaml::from_value(json).unwrap() + pub fn from_yml(json: serde_yaml::Value, variant_str: &str, item: &str) -> Self { + let mut variant: Variant = serde_yaml::from_value(json).unwrap(); + variant.item = item.to_string(); + variant.variant = variant_str.to_string(); + variant } /// Get a API id for this Item Variant. @@ -143,9 +148,19 @@ impl Variant { location: Option<&str>, note: Option<&str>, quanta: Option, - properties: Option + properties: Option, ) -> Transaction { - Transaction::new(&self.item, &self.variant, price, origin, location, note, quanta, properties).await + Transaction::new( + &self.item, + &self.variant, + price, + origin, + location, + note, + quanta, + properties, + ) + .await } /// Returns all Transactions of this Item Variant diff --git a/src/main.rs b/src/main.rs index db8cfe9..b97670f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,41 +141,44 @@ async fn rocket() -> _ { integrity::verify_integrity(&config, &flows, &locations, &itemdb).await; rocket::build() - .mount("/", route![ - routes::item::get_items_route, - routes::item::item_route, - routes::item::item_variants_page, - routes::item::supply_log_route, - routes::item::demand_log_route, - routes::item::supply_route, - routes::item::demand_route, - routes::item::transaction_route, - routes::item::inventory_route, - routes::item::inventory_route_variant, - routes::item::variant_stat_route, - routes::item::unique_field_route, - routes::item::location_info, - routes::item::locations_info, - routes::item::locations_list, - routes::item::location_inventory, - routes::flow::flow_info, - routes::flow::flows_list, - routes::item::expired_items_route, - routes::item::min_items_route, - routes::item::variant_price_history_by_origin, - routes::flow::end_flow_route, - routes::flow::continue_flow_route, - routes::flow::create_flow_route, - routes::item::location_condition_warn, - routes::item::move_transaction_route, - routes::item::variant_price_latest_by_origin, - routes::item::item_stat_route, - routes::flow::active_flows_route, - routes::flow::flow_api_route, - routes::flow::create_flow_note_route, - routes::flow::flow_notes_route, - routes::item::item_image_route - ]) + .mount( + "/", + route![ + routes::item::get_items_route, + routes::item::item_route, + routes::item::item_variants_page, + routes::item::supply_log_route, + routes::item::demand_log_route, + routes::item::supply_route, + routes::item::demand_route, + routes::item::transaction_route, + routes::item::inventory_route, + routes::item::inventory_route_variant, + routes::item::variant_stat_route, + routes::item::unique_field_route, + routes::item::location_info, + routes::item::locations_info, + routes::item::locations_list, + routes::item::location_inventory, + routes::flow::flow_info, + routes::flow::flows_list, + routes::item::expired_items_route, + routes::item::min_items_route, + routes::item::variant_price_history_by_origin, + routes::flow::end_flow_route, + routes::flow::continue_flow_route, + routes::flow::create_flow_route, + routes::item::location_condition_warn, + routes::item::move_transaction_route, + routes::item::variant_price_latest_by_origin, + routes::item::item_stat_route, + routes::flow::active_flows_route, + routes::flow::flow_api_route, + routes::flow::create_flow_note_route, + routes::flow::flow_notes_route, + routes::item::item_image_route + ], + ) .manage(itemdb) .manage(locations) .manage(flows) diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index 3090332..151b25b 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -22,7 +22,7 @@ pub struct SupplyForm { pub location: Option, pub note: Option, pub quanta: Option, - pub properties: Option + pub properties: Option, } #[post("/supply", data = "
")] @@ -50,7 +50,7 @@ pub async fn supply_route( form.location.as_deref(), form.note.as_deref(), form.quanta, - form.properties.clone() + form.properties.clone(), ) .await;