CLEANER CODE

This commit is contained in:
2023-12-19 12:26:32 +02:00
parent 53c79295fe
commit 136b8ea2aa

View File

@@ -28,26 +28,17 @@ async fn show_arrivals(db_pool: Data<PgPool>) -> impl Responder {
.content_type("application/json")
.json(arrivals)
}
#[derive(Deserialize)]
struct ArrivalFilter {
tram_line: i32,
week_day: i32,
}
#[get("specific")]
async fn show_specific(db_pool: Data<PgPool>, t_line: Data<i32>, week_day: Data<i32>) -> impl Responder {
async fn show_specific(db_pool: Data<PgPool>, filter: web::Query<ArrivalFilter> ) -> impl Responder {
// Extract data and match nullables
let tram_line: i32 = match t_line.into() {
Some(num) => **num,
None => 1
};
let day = match week_day.into() {
Some(num) => **num,
None => 1,
};
//Le query
let arrivals = query_as!(Arrival, "SELECT * FROM arrivals WHERE tram_line = $1 AND week_day = $2", tram_line, day)
let arrivals = query_as!(Arrival, "SELECT * FROM arrivals WHERE tram_line = $1 AND week_day = $2", filter.tram_line, filter.week_day)
.fetch_all(db_pool.get_ref())
.await
.expect("Could not fetch arrivals");