add global item stat route
This commit is contained in:
parent
055395337c
commit
b468aab088
2 changed files with 29 additions and 1 deletions
|
@ -91,7 +91,8 @@ async fn rocket() -> _ {
|
|||
routes::flow::continue_flow_route,
|
||||
routes::flow::create_flow_route,
|
||||
routes::item::move_transaction_route,
|
||||
routes::item::variant_price_latest_by_origin
|
||||
routes::item::variant_price_latest_by_origin,
|
||||
routes::item::item_stat_route
|
||||
],
|
||||
)
|
||||
.manage(itemdb)
|
||||
|
|
|
@ -41,6 +41,33 @@ pub fn get_items_route(itemdb: &State<ItemDB>, t: Token, c: &State<Config>) -> F
|
|||
Ok(json!({"items": items}))
|
||||
}
|
||||
|
||||
#[get("/items/stat")]
|
||||
pub async fn item_stat_route(itemdb: &State<ItemDB>, t: Token, c: &State<Config>) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
let items = itemdb.items();
|
||||
let item_count = items.len();
|
||||
let mut transaction_count = 0;
|
||||
let mut total_price = 0.0;
|
||||
|
||||
for item in items {
|
||||
for var in itemdb.get_item(&item).unwrap().variants.keys() {
|
||||
let item_var = itemdb.get_item(&item).unwrap().variant(var).unwrap();
|
||||
for t in item_var.inventory().await {
|
||||
transaction_count += 1;
|
||||
total_price += t.price.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(json!({
|
||||
"item_count": item_count,
|
||||
"total_transactions": transaction_count,
|
||||
"total_price": total_price
|
||||
}))
|
||||
}
|
||||
|
||||
/// Return an API Response for an `Item`
|
||||
#[get("/item/<item_id>")]
|
||||
pub fn item_route(
|
||||
|
|
Loading…
Reference in a new issue