add latest price route

This commit is contained in:
JMARyA 2024-09-16 08:10:26 +02:00
parent 0d4ed15460
commit 9c03d38f6e
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 31 additions and 4 deletions

View file

@ -62,6 +62,8 @@ async fn rocket() -> _ {
let config = config::get_config(); let config = config::get_config();
// todo : log warnings on misconfiguration
rocket::build() rocket::build()
.mount( .mount(
"/", "/",
@ -90,7 +92,8 @@ async fn rocket() -> _ {
routes::flow::end_flow_route, routes::flow::end_flow_route,
routes::flow::continue_flow_route, routes::flow::continue_flow_route,
routes::flow::create_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) .manage(itemdb)

View file

@ -28,5 +28,29 @@ pub async fn variant_price_history_by_origin(
.variant(variant_id) .variant(variant_id)
.ok_or_else(variant_does_not_exist_error)?; .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/<item_id>/<variant_id>/price_latest?<origin>")]
pub async fn variant_price_latest_by_origin(
item_id: &str,
variant_id: &str,
itemdb: &State<ItemDB>,
t: Token,
c: &State<Config>,
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()))
} }

View file

@ -225,14 +225,14 @@ impl Variant {
) )
} }
pub async fn price_history_by_origin(&self, origin: &str) -> Vec<Price> { pub async fn price_history_by_origin(&self, origin: &str, limit: Option<i64>) -> Vec<Price> {
Transaction::find( Transaction::find(
doc! { doc! {
"item": &self.item, "item": &self.item,
"variant": &self.variant, "variant": &self.variant,
"origin": origin "origin": origin
}, },
None, limit,
Some(sort_by_timestamp()), Some(sort_by_timestamp()),
) )
.await .await