mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-22 22:26:19 +00:00
Merge 4a63e5f6c8
into 6d32c897ac
This commit is contained in:
commit
4b07791bc1
1 changed files with 34 additions and 1 deletions
|
@ -3,6 +3,7 @@ use clap::ValueEnum;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use preciazo::supermercado::Supermercado;
|
use preciazo::supermercado::Supermercado;
|
||||||
|
use serde::Serialize;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
|
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
|
||||||
SqlitePool,
|
SqlitePool,
|
||||||
|
@ -94,6 +95,37 @@ async fn healthcheck(State(pool): State<SqlitePool>) -> impl IntoResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct CategoryWithProducts {}
|
||||||
|
|
||||||
|
async fn get_best_selling(State(pool): State<SqlitePool>) -> 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::<Vec<String>>(&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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
@ -116,9 +148,10 @@ async fn main() {
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
.route("/api/healthcheck", get(healthcheck))
|
.route("/api/healthcheck", get(healthcheck))
|
||||||
|
.route("/api/0/best-selling-products", get(get_best_selling))
|
||||||
.with_state(pool);
|
.with_state(pool);
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
|
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();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue