From 9c03d38f6e82cb27fe59240185eab111d8dab6bf Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 16 Sep 2024 08:10:26 +0200 Subject: [PATCH] add latest price route --- src/main.rs | 5 ++++- src/routes/item/stat.rs | 26 +++++++++++++++++++++++++- src/variant.rs | 4 ++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 370c984..2ec7839 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,8 @@ async fn rocket() -> _ { let config = config::get_config(); + // todo : log warnings on misconfiguration + rocket::build() .mount( "/", @@ -90,7 +92,8 @@ async fn rocket() -> _ { routes::flow::end_flow_route, routes::flow::continue_flow_route, routes::flow::create_flow_route, - routes::item::move_transaction_route + routes::item::move_transaction_route, + routes::item::variant_price_latest_by_origin ], ) .manage(itemdb) diff --git a/src/routes/item/stat.rs b/src/routes/item/stat.rs index de5c36b..079f813 100644 --- a/src/routes/item/stat.rs +++ b/src/routes/item/stat.rs @@ -28,5 +28,29 @@ pub async fn variant_price_history_by_origin( .variant(variant_id) .ok_or_else(variant_does_not_exist_error)?; - Ok(json!(variant.price_history_by_origin(origin).await)) + 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())) } diff --git a/src/variant.rs b/src/variant.rs index 084814a..05f84c6 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -225,14 +225,14 @@ impl Variant { ) } - pub async fn price_history_by_origin(&self, origin: &str) -> Vec { + pub async fn price_history_by_origin(&self, origin: &str, limit: Option) -> Vec { Transaction::find( doc! { "item": &self.item, "variant": &self.variant, "origin": origin }, - None, + limit, Some(sort_by_timestamp()), ) .await