From aca5340370438b86cd92120539f85c13d6531525 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 23 Dec 2023 15:26:49 +0100 Subject: [PATCH] show guest km, Fixes #41 --- src/model/stat.rs | 30 ++++++++++++ src/tera/stat.rs | 6 ++- templates/stat.people.html.tera | 87 ++++++++++++++++++++++----------- 3 files changed, 92 insertions(+), 31 deletions(-) diff --git a/src/model/stat.rs b/src/model/stat.rs index 5b39750..b97a09b 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -36,6 +36,36 @@ ORDER BY rowed_km DESC; .collect() } + pub async fn guest(db: &SqlitePool, year: Option) -> Stat { + let year = match year { + Some(year) => year, + None => chrono::Utc::now().year(), + }; + //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) + let rowed_km = sqlx::query(&format!( + " +SELECT SUM((b.amount_seats - COALESCE(m.member_count, 0)) * l.distance_in_km) as total_guest_km +FROM logbook l +JOIN boat b ON l.boat_id = b.id +LEFT JOIN ( + SELECT logbook_id, COUNT(*) as member_count + FROM rower + GROUP BY logbook_id +) m ON l.id = m.logbook_id +WHERE l.distance_in_km IS NOT NULL AND l.arrival LIKE '{year}-%' AND b.name != 'Externes Boot'; +" + )) + .fetch_one(db) + .await + .unwrap() + .get::(0); + + Stat { + name: "Gäste".into(), + rowed_km: rowed_km as i32, + } + } + pub async fn people(db: &SqlitePool, year: Option) -> Vec { let year = match year { Some(year) => year, diff --git a/src/tera/stat.rs b/src/tera/stat.rs index 66bf2f5..873afa2 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -35,23 +35,25 @@ async fn index_boat_kiosk( #[get("/?", rank = 2)] async fn index(db: &State, user: NonGuestUser, year: Option) -> Template { let stat = Stat::people(db, year).await; + let guest_km = Stat::guest(db, year).await; let personal = stat::get_personal(db, &user.user).await; let kiosk = false; Template::render( "stat.people", - context!(loggedin_user: &user.user, stat, personal, kiosk), + context!(loggedin_user: &user.user, stat, personal, kiosk, guest_km), ) } #[get("/?")] async fn index_kiosk(db: &State, _kiosk: KioskCookie, year: Option) -> Template { let stat = Stat::people(db, year).await; + let guest_km = Stat::guest(db, year).await; let kiosk = true; Template::render( "stat.people", - context!(stat, kiosk, show_kiosk_header: true), + context!(stat, kiosk, show_kiosk_header: true, guest_km), ) } diff --git a/templates/stat.people.html.tera b/templates/stat.people.html.tera index e2ce9b4..5ef1eb0 100644 --- a/templates/stat.people.html.tera +++ b/templates/stat.people.html.tera @@ -5,37 +5,66 @@ {% block content %} -
-

Statistik

-
- - +
+

+ Statistik + +

+
+ + +
+ +
+ +
+ {% set_global km = 0 %} {% set_global index = 1 %} {% for s in stat %} +
+ + {% if km != s.rowed_km %} + {{ loop.index }} + {% set_global index = loop.index %} {% else %} + {{ index }} + {% endif %} + + {{ s.name }} + {{ s.rowed_km }} km + + {% set_global km = s.rowed_km %}
+ {% endfor %} +
+ + + {{ guest_km.name }} + {{ guest_km.rowed_km }} km -
- -
- {% set_global km = 0 %} - {% set_global index = 1 %} - {% for s in stat %} -
- - {% if km != s.rowed_km %} - {{loop.index}} - {% set_global index = loop.index %} - {% else %} - {{ index }} - {% endif %} - - {{s.name}} - {{s.rowed_km}} km - - {% set_global km = s.rowed_km %} -
- {% endfor %} -
-
-
+
+
+
+