update
This commit is contained in:
parent
0be7fff77d
commit
74b07a0ea3
8 changed files with 107 additions and 10 deletions
|
@ -3,6 +3,7 @@ use rocket::{http::Method, launch};
|
|||
|
||||
mod db;
|
||||
mod item;
|
||||
mod process;
|
||||
mod routes;
|
||||
mod transaction;
|
||||
mod variant;
|
||||
|
@ -51,7 +52,8 @@ async fn rocket() -> _ {
|
|||
routes::item::supply_route,
|
||||
routes::item::demand_route,
|
||||
routes::item::transaction_route,
|
||||
routes::item::inventory_route
|
||||
routes::item::inventory_route,
|
||||
routes::item::variant_stat_route
|
||||
],
|
||||
)
|
||||
.manage(itemdb)
|
||||
|
|
8
src/process.rs
Normal file
8
src/process.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
pub struct ProcessInfo {
|
||||
pub name: String,
|
||||
pub depends: HashMap<String, i64>,
|
||||
pub next: Option<String>,
|
||||
pub produces: Option<Vec<String>>,
|
||||
}
|
|
@ -76,3 +76,18 @@ pub async fn inventory_route(
|
|||
.map(|x| x.api_json())
|
||||
.collect::<Vec<_>>()))
|
||||
}
|
||||
|
||||
#[get("/item/<item_id>/<variant_id>/stat")]
|
||||
pub async fn variant_stat_route(
|
||||
item_id: &str,
|
||||
variant_id: &str,
|
||||
itemdb: &State<ItemDB>,
|
||||
) -> FallibleApiResponse {
|
||||
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(variant.stat().await)
|
||||
}
|
||||
|
|
|
@ -128,6 +128,18 @@ impl Variant {
|
|||
t._id
|
||||
}
|
||||
|
||||
pub async fn stat(&self) -> serde_json::Value {
|
||||
let active_transactions = self.inventory().await;
|
||||
|
||||
// fix : ignores currency
|
||||
let total_price: f64 = active_transactions.iter().map(|x| x.price.value).sum();
|
||||
|
||||
json!({
|
||||
"amount": active_transactions.len(),
|
||||
"total_price": total_price
|
||||
})
|
||||
}
|
||||
|
||||
pub fn api_json(&self) -> serde_json::Value {
|
||||
json!({
|
||||
"item": self.item,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue