week: Implement get_last_week

Previously, we only needed to get the last week number from the index, to
properly redirect to it.
However, we will need it in the future in other places.

Implement a function that centralizes this database operation.
This commit is contained in:
trotFunky 2024-07-30 23:25:41 +01:00
parent f7efe0b66c
commit 198be70a99
2 changed files with 14 additions and 10 deletions

View file

@ -5,7 +5,7 @@ use rocket::response::Redirect;
use rocket_dyn_templates::Template;
use rocket_db_pools::{sqlx, sqlx::Row, Connection};
use rocket_db_pools::Connection;
mod auth;
@ -20,14 +20,7 @@ use database::Db;
#[get("/")]
async fn index(mut db: Connection<Db>) -> Redirect {
let current_week: u8 = match sqlx::query("SELECT number FROM Weeks WHERE is_last_week == 1;")
.fetch_one(&mut **db).await {
Ok(v) => v.try_get(0).ok().unwrap_or_else(|| 1), // If error, go back to 1
Err(error) => {
error!("Error while getting current week : {error:?}");
1
}
};
let current_week: u8 = week::get_last_week(&mut db).await;
Redirect::to(uri!(week::week(week_number = if current_week == 0 {1} else {current_week})))
}