From 826402cd413f6e1a0d12f26778cfc2d04fc846d2 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 1 Oct 2023 18:14:05 +0200 Subject: [PATCH] give proper results for stats --- src/model/stat.rs | 46 ++++++++++++++++++++++++++++++++-------------- src/tera/stat.rs | 1 + 2 files changed, 33 insertions(+), 14 deletions(-) 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)) }