This commit is contained in:
JMARyA 2024-08-27 23:33:09 +02:00
parent 0be7fff77d
commit 74b07a0ea3
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
8 changed files with 107 additions and 10 deletions

View file

@ -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
View 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>>,
}

View file

@ -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)
}

View file

@ -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,