From 45b51f46986ba1043daff529d415fe096c6a3416 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 6 May 2024 12:17:03 +0200 Subject: [PATCH] only have a single user with details struct --- src/model/user.rs | 22 +++------------------- src/tera/admin/boat.rs | 4 ++-- src/tera/admin/mail.rs | 4 ++-- src/tera/admin/notification.rs | 4 ++-- src/tera/admin/schnupper.rs | 8 ++++---- src/tera/admin/user.rs | 20 +++++++------------- src/tera/board/boathouse.rs | 4 ++-- src/tera/boatdamage.rs | 4 ++-- src/tera/boatreservation.rs | 4 ++-- src/tera/ergo.rs | 9 +++------ src/tera/log.rs | 26 ++++++++++++-------------- src/tera/mod.rs | 12 +++--------- src/tera/planned.rs | 7 ++----- src/tera/stat.rs | 6 +++--- 14 files changed, 49 insertions(+), 85 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index bb540aa..fb3b323 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -47,36 +47,20 @@ pub struct User { } #[derive(Debug, Serialize, Deserialize)] -pub struct UserWithRolesAndNotificationCount { +pub struct UserWithDetails { #[serde(flatten)] pub user: User, pub amount_unread_notifications: i32, - pub roles: Vec, -} - -impl UserWithRolesAndNotificationCount { - pub async fn from_user(user: User, db: &SqlitePool) -> Self { - Self { - roles: user.roles(db).await, - amount_unread_notifications: user.amount_unread_notifications(db).await, - user, - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct UserWithWaterStatus { - #[serde(flatten)] - pub user: User, pub on_water: bool, pub roles: Vec, } -impl UserWithWaterStatus { +impl UserWithDetails { pub async fn from_user(user: User, db: &SqlitePool) -> Self { Self { on_water: user.on_water(db).await, roles: user.roles(db).await, + amount_unread_notifications: user.amount_unread_notifications(db).await, user, } } diff --git a/src/tera/admin/boat.rs b/src/tera/admin/boat.rs index b034a38..11dddf5 100644 --- a/src/tera/admin/boat.rs +++ b/src/tera/admin/boat.rs @@ -2,7 +2,7 @@ use crate::model::{ boat::{Boat, BoatToAdd, BoatToUpdate}, location::Location, log::Log, - user::{AdminUser, User, UserWithRolesAndNotificationCount}, + user::{AdminUser, User, UserWithDetails}, }; use rocket::{ form::Form, @@ -33,7 +33,7 @@ async fn index( context.insert("users", &users); context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(admin.user, db).await, + &UserWithDetails::from_user(admin.user, db).await, ); Template::render("admin/boat/index", context.into_json()) diff --git a/src/tera/admin/mail.rs b/src/tera/admin/mail.rs index 1f6f11c..9abbad8 100644 --- a/src/tera/admin/mail.rs +++ b/src/tera/admin/mail.rs @@ -10,7 +10,7 @@ use crate::model::log::Log; use crate::model::mail::Mail; use crate::model::role::Role; use crate::model::user::AdminUser; -use crate::model::user::UserWithRolesAndNotificationCount; +use crate::model::user::UserWithDetails; use crate::tera::Config; #[get("/mail")] @@ -27,7 +27,7 @@ async fn index( context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(admin.user, db).await, + &UserWithDetails::from_user(admin.user, db).await, ); context.insert("roles", &roles); diff --git a/src/tera/admin/notification.rs b/src/tera/admin/notification.rs index 0e7ab9a..bba3488 100644 --- a/src/tera/admin/notification.rs +++ b/src/tera/admin/notification.rs @@ -2,7 +2,7 @@ use crate::model::{ log::Log, notification::Notification, role::Role, - user::{AdminUser, User, UserWithRolesAndNotificationCount}, + user::{AdminUser, User, UserWithDetails}, }; use rocket::{ form::Form, @@ -26,7 +26,7 @@ async fn index( } context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.user, db).await, + &UserWithDetails::from_user(user.user, db).await, ); context.insert("roles", &Role::all(db).await); diff --git a/src/tera/admin/schnupper.rs b/src/tera/admin/schnupper.rs index d7a704a..8548f7b 100644 --- a/src/tera/admin/schnupper.rs +++ b/src/tera/admin/schnupper.rs @@ -1,6 +1,6 @@ use crate::model::{ role::Role, - user::{SchnupperBetreuerUser, User, UserWithRolesAndNotificationCount}, + user::{SchnupperBetreuerUser, User, UserWithDetails}, }; use futures::future::join_all; use rocket::{get, request::FlashMessage, routes, Route, State}; @@ -18,9 +18,9 @@ async fn index( let user_futures: Vec<_> = User::all_with_role(db, &schnupperant) .await .into_iter() - .map(|u| async move { UserWithRolesAndNotificationCount::from_user(u, db).await }) + .map(|u| async move { UserWithDetails::from_user(u, db).await }) .collect(); - let users: Vec = join_all(user_futures).await; + let users: Vec = join_all(user_futures).await; let mut context = Context::new(); if let Some(msg) = flash { @@ -29,7 +29,7 @@ async fn index( context.insert("schnupperanten", &users); context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, + &UserWithDetails::from_user(user.into(), db).await, ); Template::render("admin/schnupper/index", context.into_json()) diff --git a/src/tera/admin/user.rs b/src/tera/admin/user.rs index e5d9781..919d06f 100644 --- a/src/tera/admin/user.rs +++ b/src/tera/admin/user.rs @@ -6,8 +6,8 @@ use crate::model::{ logbook::Logbook, role::Role, user::{ - AdminUser, User, UserWithMembershipPdf, UserWithRolesAndMembershipPdf, - UserWithRolesAndNotificationCount, VorstandUser, + AdminUser, User, UserWithDetails, UserWithMembershipPdf, UserWithRolesAndMembershipPdf, + VorstandUser, }, }; use futures::future::join_all; @@ -67,10 +67,7 @@ async fn index( context.insert("users", &users); context.insert("roles", &roles); context.insert("families", &families); - context.insert( - "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user, db).await, - ); + context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await); Template::render("admin/user/index", context.into_json()) } @@ -102,10 +99,7 @@ async fn index_admin( context.insert("users", &users); context.insert("roles", &roles); context.insert("families", &families); - context.insert( - "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user, db).await, - ); + context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await); Template::render("admin/user/index", context.into_json()) } @@ -133,7 +127,7 @@ async fn fees( } context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(admin.into(), db).await, + &UserWithDetails::from_user(admin.into(), db).await, ); Template::render("admin/user/fees", context.into_json()) @@ -153,7 +147,7 @@ async fn scheckbuch( for s in scheckbooks { scheckbooks_with_roles.push(( Logbook::completed_with_user(db, &s).await, - UserWithRolesAndNotificationCount::from_user(s, db).await, + UserWithDetails::from_user(s, db).await, )) } @@ -164,7 +158,7 @@ async fn scheckbuch( } context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, + &UserWithDetails::from_user(user.into(), db).await, ); Template::render("admin/user/scheckbuch", context.into_json()) diff --git a/src/tera/board/boathouse.rs b/src/tera/board/boathouse.rs index 54fd729..105e32b 100644 --- a/src/tera/board/boathouse.rs +++ b/src/tera/board/boathouse.rs @@ -1,7 +1,7 @@ use crate::model::{ boat::Boat, boathouse::Boathouse, - user::{AdminUser, UserWithRolesAndNotificationCount, VorstandUser}, + user::{AdminUser, UserWithDetails, VorstandUser}, }; use rocket::{ form::Form, @@ -39,7 +39,7 @@ async fn index( context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(admin.into(), db).await, + &UserWithDetails::from_user(admin.into(), db).await, ); Template::render("board/boathouse", context.into_json()) diff --git a/src/tera/boatdamage.rs b/src/tera/boatdamage.rs index 0fca62b..c718664 100644 --- a/src/tera/boatdamage.rs +++ b/src/tera/boatdamage.rs @@ -13,7 +13,7 @@ use crate::{ model::{ boat::Boat, boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified}, - user::{CoxUser, DonauLinzUser, TechUser, User, UserWithRolesAndNotificationCount}, + user::{CoxUser, DonauLinzUser, TechUser, User, UserWithDetails}, }, tera::log::KioskCookie, }; @@ -59,7 +59,7 @@ async fn index( context.insert("boats", &boats); context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, + &UserWithDetails::from_user(user.into(), db).await, ); Template::render("boatdamages", context.into_json()) diff --git a/src/tera/boatreservation.rs b/src/tera/boatreservation.rs index 3c406bc..a011160 100644 --- a/src/tera/boatreservation.rs +++ b/src/tera/boatreservation.rs @@ -15,7 +15,7 @@ use crate::{ boat::Boat, boatreservation::{BoatReservation, BoatReservationToAdd}, log::Log, - user::{DonauLinzUser, User, UserWithRolesAndNotificationCount}, + user::{DonauLinzUser, User, UserWithDetails}, }, tera::log::KioskCookie, }; @@ -75,7 +75,7 @@ async fn index( context.insert("user", &User::all(db).await); context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, + &UserWithDetails::from_user(user.into(), db).await, ); Template::render("boatreservations", context.into_json()) diff --git a/src/tera/ergo.rs b/src/tera/ergo.rs index c419781..493ae64 100644 --- a/src/tera/ergo.rs +++ b/src/tera/ergo.rs @@ -18,7 +18,7 @@ use tera::Context; use crate::model::{ log::Log, - user::{AdminUser, User, UserWithRolesAndNotificationCount}, + user::{AdminUser, User, UserWithDetails}, }; #[derive(Serialize)] @@ -51,7 +51,7 @@ async fn send(db: &State, _user: AdminUser) -> Template { Template::render( "ergo.final", - context!(loggedin_user: &UserWithRolesAndNotificationCount::from_user(_user.user, db).await, thirty, dozen), + context!(loggedin_user: &UserWithDetails::from_user(_user.user, db).await, thirty, dozen), ) } @@ -120,10 +120,7 @@ async fn index(db: &State, user: User, flash: Option Template { let boats = Boat::for_user(db, &user).await; - let mut coxes: Vec = futures::future::join_all( + let mut coxes: Vec = futures::future::join_all( User::cox(db) .await .into_iter() - .map(|user| UserWithWaterStatus::from_user(user, db)), + .map(|user| UserWithDetails::from_user(user, db)), ) .await; coxes.retain(|u| { u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into()) }); - let mut users: Vec = futures::future::join_all( + let mut users: Vec = futures::future::join_all( User::all(db) .await .into_iter() - .map(|user| UserWithWaterStatus::from_user(user, db)), + .map(|user| UserWithDetails::from_user(user, db)), ) .await; users.retain(|u| { @@ -93,7 +91,7 @@ async fn index( context.insert("logtypes", &logtypes); context.insert( "loggedin_user", - &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, + &UserWithDetails::from_user(user.into(), db).await, ); context.insert("on_water", &on_water); context.insert("distances", &distances); @@ -107,7 +105,7 @@ async fn show(db: &State, user: DonauLinzUser) -> Template { Template::render( "log.completed", - context!(logs, loggedin_user: &UserWithRolesAndNotificationCount::from_user(user.into(), db).await), + context!(logs, loggedin_user: &UserWithDetails::from_user(user.into(), db).await), ) } @@ -117,7 +115,7 @@ async fn show_for_year(db: &State, user: AdminUser, year: i32) -> Te Template::render( "log.completed", - context!(logs, loggedin_user: &UserWithRolesAndNotificationCount::from_user(user.user, db).await), + context!(logs, loggedin_user: &UserWithDetails::from_user(user.user, db).await), ) } @@ -153,11 +151,11 @@ async fn kiosk( _kiosk: KioskCookie, ) -> Template { let boats = Boat::all(db).await; - let mut coxes: Vec = futures::future::join_all( + let mut coxes: Vec = futures::future::join_all( User::cox(db) .await .into_iter() - .map(|user| UserWithWaterStatus::from_user(user, db)), + .map(|user| UserWithDetails::from_user(user, db)), ) .await; @@ -165,11 +163,11 @@ async fn kiosk( u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into()) }); - let mut users: Vec = futures::future::join_all( + let mut users: Vec = futures::future::join_all( User::all(db) .await .into_iter() - .map(|user| UserWithWaterStatus::from_user(user, db)), + .map(|user| UserWithDetails::from_user(user, db)), ) .await; diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 7fc6ead..ab087ce 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -23,7 +23,7 @@ use tera::Context; use crate::model::{ notification::Notification, role::Role, - user::{User, UserWithRolesAndNotificationCount}, + user::{User, UserWithDetails}, }; pub(crate) mod admin; @@ -53,10 +53,7 @@ async fn index(db: &State, user: User, flash: Option, user: User, flash: Option, user: DonauLinzUser) -> Template { Template::render( "stat.boats", - context!(loggedin_user: &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, stat, kiosk), + context!(loggedin_user: &UserWithDetails::from_user(user.into(), db).await, stat, kiosk), ) } @@ -38,7 +38,7 @@ async fn index(db: &State, user: DonauLinzUser, year: Option) - Template::render( "stat.people", - context!(loggedin_user: &UserWithRolesAndNotificationCount::from_user(user.into(), db).await, stat, personal, kiosk, guest_km, club_km), + context!(loggedin_user: &UserWithDetails::from_user(user.into(), db).await, stat, personal, kiosk, guest_km, club_km), ) } -- 2.45.2