diff --git a/src/item.rs b/src/item.rs index a2b68a8..de77385 100644 --- a/src/item.rs +++ b/src/item.rs @@ -121,6 +121,27 @@ impl Item { Transaction::find(filter, None, None).await.unwrap() } + pub async fn inventory_by_origin(&self, origin: &str) -> Vec { + let filter = doc! { + "item": &self._id, + "consumed": { "$not": { "$type": "object" } }, + "origin": origin + }; + + Transaction::find(filter, None, None).await.unwrap() + } + + pub async fn consumed_by_destination(&self, destination: &str) -> Vec { + let filter = doc! { + "item": &self._id, + "consumed": { + "destination": destination + } + }; + + Transaction::find(filter, None, None).await.unwrap() + } + pub fn api_json(&self) -> serde_json::Value { let variants: HashMap = self .variants diff --git a/src/main.rs b/src/main.rs index 38510de..2f4bfa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,7 +85,8 @@ async fn rocket() -> _ { routes::flow::flow_info, routes::flow::flows_list, routes::item::expired_items_route, - routes::item::min_items_route + routes::item::min_items_route, + routes::item::variant_price_history_by_origin ], ) .manage(itemdb) diff --git a/src/routes/item/demand.rs b/src/routes/item/demand.rs index 79741b3..af3e168 100644 --- a/src/routes/item/demand.rs +++ b/src/routes/item/demand.rs @@ -69,14 +69,15 @@ pub async fn demand_route(f: Json, t: Token, c: &State) -> F Ok(json!({"ok": 1})) } -/// Returns all used transactions for Item Variant -#[get("/item///demand")] +/// Returns all consumed transactions for Item Variant +#[get("/item///demand?")] pub async fn demand_log_route( item_id: &str, variant_id: &str, itemdb: &State, t: Token, c: &State, + destination: Option<&str>, ) -> FallibleApiResponse { check_auth!(t, c); @@ -86,7 +87,7 @@ pub async fn demand_log_route( .variant(variant_id) .ok_or_else(variant_does_not_exist_error)?; - let transactions = variant.demand_log().await; + let transactions = variant.demand_log(destination).await; Ok(json!(transactions)) } diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index a05ddea..4b0c479 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -1,6 +1,7 @@ mod demand; mod error; mod location; +mod stat; mod supply; pub use demand::*; @@ -8,6 +9,7 @@ pub use error::*; pub use location::*; use mongod::Model; use mongod::ToAPI; +pub use stat::*; pub use supply::*; use rocket::get; diff --git a/src/routes/item/stat.rs b/src/routes/item/stat.rs new file mode 100644 index 0000000..de5c36b --- /dev/null +++ b/src/routes/item/stat.rs @@ -0,0 +1,32 @@ +use rocket::{get, State}; +use serde_json::json; + +use crate::check_auth; +use crate::config::Config; +use crate::routes::Token; +use crate::{ + db::ItemDB, + routes::{api_error, FallibleApiResponse}, +}; + +use super::{item_does_not_exist_error, variant_does_not_exist_error}; + +#[get("/item///price_history?")] +pub async fn variant_price_history_by_origin( + item_id: &str, + variant_id: &str, + itemdb: &State, + t: Token, + c: &State, + origin: &str, +) -> FallibleApiResponse { + check_auth!(t, c); + + let variant = itemdb + .get_item(item_id) + .ok_or_else(item_does_not_exist_error)? + .variant(variant_id) + .ok_or_else(variant_does_not_exist_error)?; + + Ok(json!(variant.price_history_by_origin(origin).await)) +} diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index 13073c8..acb02c3 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -82,20 +82,25 @@ pub async fn supply_log_route( } /// Returns current active Transactions for Item -#[get("/item//inventory")] +#[get("/item//inventory?")] pub async fn inventory_route( item_id: &str, itemdb: &State, t: Token, c: &State, + origin: Option<&str>, ) -> FallibleApiResponse { check_auth!(t, c); - let variant = itemdb + let item = itemdb .get_item(item_id) .ok_or_else(item_does_not_exist_error)?; - let transactions = variant.inventory().await; + let transactions = if let Some(origin) = origin { + item.inventory_by_origin(origin).await + } else { + item.inventory().await + }; Ok(json!(mongod::vec_to_api(&transactions).await)) } diff --git a/src/variant.rs b/src/variant.rs index b5c7dbc..e19c154 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -1,4 +1,4 @@ -use mongod::Model; +use mongod::{Model, Sort}; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -76,6 +76,7 @@ impl Variant { } } + /// Returns the IDs of Transactions from this Item Variant. pub async fn supply_log(&self) -> Vec { let filter = doc! { "item": &self.item, @@ -95,6 +96,7 @@ impl Variant { ret } + /// Returns the active Transaction of this Item Variant which are not yet consumed. pub async fn inventory(&self) -> Vec { let filter = doc! { "item": &self.item, @@ -105,11 +107,20 @@ impl Variant { Transaction::find(filter, None, None).await.unwrap() } - pub async fn demand_log(&self) -> Vec { - let filter = doc! { - "item": &self.item, - "variant": &self.variant, - "consumed": { "$type": "object" } + /// Returns the IDs of the Transactions from this Item Variant which are consumed. + pub async fn demand_log(&self, destination: Option<&str>) -> Vec { + let filter = if let Some(dest) = destination { + doc! { + "item": &self.item, + "variant": &self.variant, + "consumed": { "destination": dest } + } + } else { + doc! { + "item": &self.item, + "variant": &self.variant, + "consumed": { "$type": "object" } + } }; let result = Transaction::find_partial(filter, json!({}), None, None) @@ -155,13 +166,16 @@ impl Variant { t } + /// Returns all Transactions of this Item Variant pub async fn get_all_transactions(&self) -> Vec { let filter = doc! { "item": &self.item, "variant": &self.variant }; - Transaction::find(filter, None, None).await.unwrap() + Transaction::find(filter, None, Some(doc! { "timestamp": Sort::Descending })) + .await + .unwrap() } pub async fn get_transaction_timeslice(&self, year: i32, month: u32) -> Vec {