From 36d84e8157bb54c3cc215ca588a46e3d3a4cf0df Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 23 Sep 2023 22:13:48 +0200 Subject: [PATCH] disable rowers in logbook if they are on water --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 1 + src/model/user.rs | 16 ++++++++++++++++ src/tera/log.rs | 19 ++++++++++++++++--- templates/includes/forms/log.html.tera | 4 ++-- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 971777e..5b52f8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -625,6 +625,7 @@ checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -675,6 +676,17 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + [[package]] name = "futures-sink" version = "0.3.28" @@ -696,6 +708,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -1850,6 +1863,7 @@ dependencies = [ "argon2", "chrono", "env_logger", + "futures", "ics", "log", "rocket", diff --git a/Cargo.toml b/Cargo.toml index ac37dbe..02f1b49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,4 @@ serde_json = "1.0" chrono = { version = "0.4", features = ["serde"]} tera = { version = "1.18", features = ["date-locale"], optional = true} ics = "0.5" +futures = "0.3" diff --git a/src/model/user.rs b/src/model/user.rs index 4de124b..068fc05 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -28,6 +28,22 @@ pub struct User { pub last_access: Option, } +#[derive(Debug, Serialize, Deserialize)] +pub struct UserWithWaterStatus { + #[serde(flatten)] + pub user: User, + pub on_water: bool, +} + +impl UserWithWaterStatus { + pub async fn from_user(user: User, db: &SqlitePool) -> Self { + Self { + on_water: user.on_water(db).await, + user, + } + } +} + impl PartialEq for User { fn eq(&self, other: &Self) -> bool { self.id == other.id diff --git a/src/tera/log.rs b/src/tera/log.rs index 8709ac9..a5cb652 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -17,7 +17,7 @@ use crate::model::{ boat::Boat, logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError, LogbookUpdateError}, logtype::LogType, - user::{AdminUser, User}, + user::{AdminUser, User, UserWithWaterStatus}, }; pub struct KioskCookie(String); @@ -41,8 +41,21 @@ async fn index( adminuser: AdminUser, ) -> Template { let boats = Boat::all(db).await; - let coxes = User::cox(db).await; - let users = User::all(db).await; + + let coxes: Vec = futures::future::join_all( + User::cox(db) + .await + .into_iter() + .map(|user| UserWithWaterStatus::from_user(user, db)), + ) + .await; + let users: Vec = futures::future::join_all( + User::all(db) + .await + .into_iter() + .map(|user| UserWithWaterStatus::from_user(user, db)), + ) + .await; let logtypes = LogType::all(db).await; let distances = Logbook::distances(db).await; diff --git a/templates/includes/forms/log.html.tera b/templates/includes/forms/log.html.tera index e60b5bb..84cfd21 100644 --- a/templates/includes/forms/log.html.tera +++ b/templates/includes/forms/log.html.tera @@ -35,12 +35,12 @@