From 730f9e5dfff85a593c54e363c0626f2f7965de80 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 16:07:15 +0100 Subject: [PATCH] add boat stats Fixes #50 --- src/model/stat.rs | 23 ++++++++++- src/tera/stat.rs | 32 ++++++++++++--- templates/includes/macros.html.tera | 5 ++- templates/stat.boats.html.tera | 40 +++++++++++++++++++ .../{stat.html.tera => stat.people.html.tera} | 0 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 templates/stat.boats.html.tera rename templates/{stat.html.tera => stat.people.html.tera} (100%) diff --git a/src/model/stat.rs b/src/model/stat.rs index d60816a..b2f2a0e 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -9,7 +9,28 @@ pub struct Stat { } impl Stat { - pub async fn get_rowed_km(db: &SqlitePool) -> Vec { + pub async fn boats(db: &SqlitePool) -> Vec { + //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) + sqlx::query( + " +SELECT (SELECT name FROM boat WHERE id=logbook.boat_id) as name, CAST(SUM(distance_in_km) AS INTEGER) AS rowed_km +FROM logbook +GROUP BY boat_id +ORDER BY rowed_km DESC; +", + ) + .fetch_all(db) + .await + .unwrap() + .into_iter() + .map(|row| Stat { + name: row.get("name"), + rowed_km: row.get("rowed_km"), + }) + .collect() + } + + pub async fn people(db: &SqlitePool) -> Vec { //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) sqlx::query( " diff --git a/src/tera/stat.rs b/src/tera/stat.rs index e2d23ba..834870d 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -9,28 +9,50 @@ use crate::model::{ use super::log::KioskCookie; +#[get("/boats", rank = 2)] +async fn index_boat(db: &State, user: NonGuestUser) -> Template { + let stat = Stat::boats(db).await; + let kiosk = false; + + Template::render( + "stat.boats", + context!(loggedin_user: &user.user, stat, kiosk), + ) +} + +#[get("/boats")] +async fn index_boat_kiosk(db: &State, _kiosk: KioskCookie) -> Template { + let stat = Stat::boats(db).await; + let kiosk = true; + + Template::render("stat.boats", context!(stat, kiosk, show_kiosk_header: true)) +} + #[get("/", rank = 2)] async fn index(db: &State, user: NonGuestUser) -> Template { - let stat = Stat::get_rowed_km(db).await; + let stat = Stat::people(db).await; let personal = stat::get_personal(db, &user.user).await; let kiosk = false; Template::render( - "stat", + "stat.people", context!(loggedin_user: &user.user, stat, personal, kiosk), ) } #[get("/")] async fn index_kiosk(db: &State, _kiosk: KioskCookie) -> Template { - let stat = Stat::get_rowed_km(db).await; + let stat = Stat::people(db).await; let kiosk = true; - Template::render("stat", context!(stat, kiosk, show_kiosk_header: true)) + Template::render( + "stat.people", + context!(stat, kiosk, show_kiosk_header: true), + ) } pub fn routes() -> Vec { - routes![index, index_kiosk] + routes![index, index_kiosk, index_boat, index_boat_kiosk] } #[cfg(test)] diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 3d9cca8..9487352 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -28,7 +28,10 @@ Logbuch - Statistik + Personen-Statistik + + + Boots-Statistik {% if loggedin_user.is_admin %} diff --git a/templates/stat.boats.html.tera b/templates/stat.boats.html.tera new file mode 100644 index 0000000..d1aca02 --- /dev/null +++ b/templates/stat.boats.html.tera @@ -0,0 +1,40 @@ +{% import "includes/macros" as macros %} + +{% extends "base" %} + +{% block content %} +
+

Boots-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 %} +
+
+
+ + +{% endblock content%} diff --git a/templates/stat.html.tera b/templates/stat.people.html.tera similarity index 100% rename from templates/stat.html.tera rename to templates/stat.people.html.tera