FOUND DYNAMICALLY BUILDING SQLX QUERIES TOO LATE

This commit is contained in:
2023-12-18 18:18:52 +02:00
parent 0dad808c75
commit b011f3f972

View File

@@ -15,7 +15,7 @@ pub struct Arrival {
direction: bool,
}
use web::Data;
use sqlx::{PgPool, query_as};
use sqlx::{PgPool, query_as, query};
#[get("all")]
async fn show_arrivals(db_pool: Data<PgPool>) -> impl Responder {
let arrivals = query_as!(Arrival, r#"SELECT * FROM arrivals"#)
@@ -27,3 +27,25 @@ async fn show_arrivals(db_pool: Data<PgPool>) -> impl Responder {
.content_type("application/json")
.json(arrivals)
}
#[get("specific")]
async fn show_specific(db_pool: Data<PgPool>, type_arr: Vec<u8>) -> impl Responder {
// Query Logic
// Idea: Construct a query bit by bit depending on input
let mut dyn_query = String::from("SELECT * FROM arrivals WHERE ");
if type_arr.len() == 1 {
dyn_query.push_str(&format!("tram_line = {}", type_arr[0]));
}
// Check: https://stackoverflow.com/questions/74956100/how-to-build-safe-dynamic-query-with-sqlx-in-rust
// lappy ded ;-;
let arrivals = String::from("AIDS");
dbg!(&arrivals);
HttpResponse::Ok()
.content_type("application/json")
.json(arrivals)
}