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, None).await)) } #[get("/item///price_latest?")] pub async fn variant_price_latest_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, Some(1)) .await .first() .unwrap())) } #[get("/items/stat")] pub async fn item_stat_route( itemdb: &State, t: Token, c: &State, ) -> 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; } } } Ok(json!({ "item_count": item_count, "total_transactions": transaction_count, "total_price": total_price })) }