From 94971162654c08ce8fece70ffd523a740b08fd84 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 29 Oct 2023 17:31:07 +0100 Subject: [PATCH 1/5] add first draft of notifications --- src/model/notification.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/model/notification.rs diff --git a/src/model/notification.rs b/src/model/notification.rs new file mode 100644 index 0000000..d345f74 --- /dev/null +++ b/src/model/notification.rs @@ -0,0 +1,36 @@ +use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc}; +use serde::{Deserialize, Serialize}; +use sqlx::{FromRow, SqlitePool}; + +#[derive(FromRow, Debug, Serialize, Deserialize)] +pub struct Notification { + pub id: i64, + pub user_id: i64, + pub message: String, + pub read_at: NaiveDateTime, + pub category: String, +} + +impl Notification { + //pub async fn create(db: &SqlitePool, msg: String) -> bool { + // sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,) + // .execute(db) + // .await + // .is_ok() + //} + + async fn for_user(db: &SqlitePool, user: &User) -> Vec { + sqlx::query_as!( + Log, + " +SELECT id, user_id, message, read_at, category +FROM notification +WHERE user_id = {} + ", + user.id + ) + .fetch_all(db) + .await + .unwrap() + } +} From 730f9e5dfff85a593c54e363c0626f2f7965de80 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 16:07:15 +0100 Subject: [PATCH 2/5] 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 From fe5ba9020d83db8601ba36d2892c82bfedf076a0 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 18:22:34 +0100 Subject: [PATCH 3/5] also show in kiosk mode --- templates/base.html.tera | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/base.html.tera b/templates/base.html.tera index af4230b..fa114bb 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -23,7 +23,8 @@
From 8479d0c70b18d432a271ee835833c1b86ade7f34 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 20:25:12 +0100 Subject: [PATCH 4/5] fix link --- templates/base.html.tera | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/base.html.tera b/templates/base.html.tera index fa114bb..1ac1e13 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -24,7 +24,7 @@ Ausfahrt eintragen Logbuch Personen Statistik - Boots Statistik + Boots Statistik Bootsschaden From aed8e7d5c348066776bea803737e60f67f12d744 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 20:29:33 +0100 Subject: [PATCH 5/5] use names that also m enjoys seeing --- templates/base.html.tera | 4 ++-- templates/includes/macros.html.tera | 4 ++-- templates/stat.boats.html.tera | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/base.html.tera b/templates/base.html.tera index 1ac1e13..e7ae735 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -23,8 +23,8 @@ diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 9487352..d1cbe1e 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -28,10 +28,10 @@ Logbuch - Personen-Statistik + Statistik - Boots-Statistik + Bootsauswertung {% if loggedin_user.is_admin %} diff --git a/templates/stat.boats.html.tera b/templates/stat.boats.html.tera index d1aca02..1ea186c 100644 --- a/templates/stat.boats.html.tera +++ b/templates/stat.boats.html.tera @@ -4,7 +4,7 @@ {% block content %}
-

Boots-Statistik

+

Bootsauswertung