diff --git a/src/model/stat.rs b/src/model/stat.rs index 77922f9..0344de3 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -50,18 +50,36 @@ pub struct PersonalStat { } pub async fn get_personal(db: &SqlitePool, user: &User) -> Vec { - vec![ - PersonalStat { - date: String::from("2023-01-01"), - km: 5, - }, - PersonalStat { - date: String::from("2023-02-01"), - km: 24, - }, - PersonalStat { - date: String::from("2023-08-30"), - km: 1340, - }, - ] + sqlx::query(&format!( + " +SELECT + departure_date as date, + SUM(total_distance) OVER (ORDER BY departure_date) as km +FROM ( + SELECT + date(l.departure) as departure_date, + COALESCE(SUM(l.distance_in_km),0) as total_distance + FROM + logbook l + LEFT JOIN + rower r ON l.id = r.logbook_id + WHERE + l.shipmaster = {0} OR r.rower_id = {0} + GROUP BY + departure_date +) as subquery +ORDER BY + departure_date; +", + user.id + )) + .fetch_all(db) + .await + .unwrap() + .into_iter() + .map(|row| PersonalStat { + date: row.get("date"), + km: row.get("km"), + }) + .collect() } diff --git a/src/tera/stat.rs b/src/tera/stat.rs index 1e39aa9..5dac834 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -12,6 +12,7 @@ async fn index(db: &State, user: User) -> Template { let stat = Stat::get_rowed_km(db).await; let personal = stat::get_personal(db, &user).await; + println!("{personal:?}"); Template::render("stat", context!(loggedin_user: &user, stat, personal)) }