From 4a63e5f6c840b7ccc1ca8280c605ffe58cc1dff1 Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 21 Jul 2024 11:09:38 -0300 Subject: [PATCH] WIP --- rust/src/api/main.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/rust/src/api/main.rs b/rust/src/api/main.rs index e254ba3..8322e95 100644 --- a/rust/src/api/main.rs +++ b/rust/src/api/main.rs @@ -3,6 +3,7 @@ use clap::ValueEnum; use futures::future::join_all; use itertools::Itertools; use preciazo::supermercado::Supermercado; +use serde::Serialize; use sqlx::{ sqlite::{SqliteConnectOptions, SqlitePoolOptions}, SqlitePool, @@ -94,6 +95,37 @@ async fn healthcheck(State(pool): State) -> impl IntoResponse { } } +#[derive(Serialize)] +struct CategoryWithProducts {} + +async fn get_best_selling(State(pool): State) -> impl IntoResponse { + let categories = sqlx::query!( + "SELECT fetched_at, category, eans_json FROM db_best_selling + GROUP BY category + HAVING MAX(fetched_at)", + ) + .fetch_all(&pool) + .await + .unwrap(); + + categories.iter().map(|category| { + let eans = + serde_json::de::from_str::>(&category.eans_json.clone().unwrap()).unwrap(); + let products = sqlx::query!( + "SELECT ean, name, image_url FROM precios + WHERE ean in (?) + GROUP BY ean + HAVING MAX(fetched_at)", + eans, + ) + .fetch_all(&pool) + .await + .unwrap(); + }); + + todo!() +} + #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); @@ -116,9 +148,10 @@ async fn main() { let app = Router::new() .route("/", get(index)) .route("/api/healthcheck", get(healthcheck)) + .route("/api/0/best-selling-products", get(get_best_selling)) .with_state(pool); let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap(); - tracing::debug!("listening on {}", listener.local_addr().unwrap()); + tracing::info!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); }