only have a single user with details struct #481

Merged
philipp merged 1 commits from simplify-user-structs into main 2024-05-06 13:30:38 +02:00
14 changed files with 49 additions and 85 deletions

View File

@ -47,36 +47,20 @@ pub struct User {
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct UserWithRolesAndNotificationCount { pub struct UserWithDetails {
#[serde(flatten)] #[serde(flatten)]
pub user: User, pub user: User,
pub amount_unread_notifications: i32, pub amount_unread_notifications: i32,
pub roles: Vec<String>,
}
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 on_water: bool,
pub roles: Vec<String>, pub roles: Vec<String>,
} }
impl UserWithWaterStatus { impl UserWithDetails {
pub async fn from_user(user: User, db: &SqlitePool) -> Self { pub async fn from_user(user: User, db: &SqlitePool) -> Self {
Self { Self {
on_water: user.on_water(db).await, on_water: user.on_water(db).await,
roles: user.roles(db).await, roles: user.roles(db).await,
amount_unread_notifications: user.amount_unread_notifications(db).await,
user, user,
} }
} }

View File

@ -2,7 +2,7 @@ use crate::model::{
boat::{Boat, BoatToAdd, BoatToUpdate}, boat::{Boat, BoatToAdd, BoatToUpdate},
location::Location, location::Location,
log::Log, log::Log,
user::{AdminUser, User, UserWithRolesAndNotificationCount}, user::{AdminUser, User, UserWithDetails},
}; };
use rocket::{ use rocket::{
form::Form, form::Form,
@ -33,7 +33,7 @@ async fn index(
context.insert("users", &users); context.insert("users", &users);
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(admin.user, db).await, &UserWithDetails::from_user(admin.user, db).await,
); );
Template::render("admin/boat/index", context.into_json()) Template::render("admin/boat/index", context.into_json())

View File

@ -10,7 +10,7 @@ use crate::model::log::Log;
use crate::model::mail::Mail; use crate::model::mail::Mail;
use crate::model::role::Role; use crate::model::role::Role;
use crate::model::user::AdminUser; use crate::model::user::AdminUser;
use crate::model::user::UserWithRolesAndNotificationCount; use crate::model::user::UserWithDetails;
use crate::tera::Config; use crate::tera::Config;
#[get("/mail")] #[get("/mail")]
@ -27,7 +27,7 @@ async fn index(
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(admin.user, db).await, &UserWithDetails::from_user(admin.user, db).await,
); );
context.insert("roles", &roles); context.insert("roles", &roles);

View File

@ -2,7 +2,7 @@ use crate::model::{
log::Log, log::Log,
notification::Notification, notification::Notification,
role::Role, role::Role,
user::{AdminUser, User, UserWithRolesAndNotificationCount}, user::{AdminUser, User, UserWithDetails},
}; };
use rocket::{ use rocket::{
form::Form, form::Form,
@ -26,7 +26,7 @@ async fn index(
} }
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.user, db).await, &UserWithDetails::from_user(user.user, db).await,
); );
context.insert("roles", &Role::all(db).await); context.insert("roles", &Role::all(db).await);

View File

@ -1,6 +1,6 @@
use crate::model::{ use crate::model::{
role::Role, role::Role,
user::{SchnupperBetreuerUser, User, UserWithRolesAndNotificationCount}, user::{SchnupperBetreuerUser, User, UserWithDetails},
}; };
use futures::future::join_all; use futures::future::join_all;
use rocket::{get, request::FlashMessage, routes, Route, State}; 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) let user_futures: Vec<_> = User::all_with_role(db, &schnupperant)
.await .await
.into_iter() .into_iter()
.map(|u| async move { UserWithRolesAndNotificationCount::from_user(u, db).await }) .map(|u| async move { UserWithDetails::from_user(u, db).await })
.collect(); .collect();
let users: Vec<UserWithRolesAndNotificationCount> = join_all(user_futures).await; let users: Vec<UserWithDetails> = join_all(user_futures).await;
let mut context = Context::new(); let mut context = Context::new();
if let Some(msg) = flash { if let Some(msg) = flash {
@ -29,7 +29,7 @@ async fn index(
context.insert("schnupperanten", &users); context.insert("schnupperanten", &users);
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.into(), db).await, &UserWithDetails::from_user(user.into(), db).await,
); );
Template::render("admin/schnupper/index", context.into_json()) Template::render("admin/schnupper/index", context.into_json())

View File

@ -6,8 +6,8 @@ use crate::model::{
logbook::Logbook, logbook::Logbook,
role::Role, role::Role,
user::{ user::{
AdminUser, User, UserWithMembershipPdf, UserWithRolesAndMembershipPdf, AdminUser, User, UserWithDetails, UserWithMembershipPdf, UserWithRolesAndMembershipPdf,
UserWithRolesAndNotificationCount, VorstandUser, VorstandUser,
}, },
}; };
use futures::future::join_all; use futures::future::join_all;
@ -67,10 +67,7 @@ async fn index(
context.insert("users", &users); context.insert("users", &users);
context.insert("roles", &roles); context.insert("roles", &roles);
context.insert("families", &families); context.insert("families", &families);
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
Template::render("admin/user/index", context.into_json()) Template::render("admin/user/index", context.into_json())
} }
@ -102,10 +99,7 @@ async fn index_admin(
context.insert("users", &users); context.insert("users", &users);
context.insert("roles", &roles); context.insert("roles", &roles);
context.insert("families", &families); context.insert("families", &families);
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
Template::render("admin/user/index", context.into_json()) Template::render("admin/user/index", context.into_json())
} }
@ -133,7 +127,7 @@ async fn fees(
} }
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(admin.into(), db).await, &UserWithDetails::from_user(admin.into(), db).await,
); );
Template::render("admin/user/fees", context.into_json()) Template::render("admin/user/fees", context.into_json())
@ -153,7 +147,7 @@ async fn scheckbuch(
for s in scheckbooks { for s in scheckbooks {
scheckbooks_with_roles.push(( scheckbooks_with_roles.push((
Logbook::completed_with_user(db, &s).await, 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( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.into(), db).await, &UserWithDetails::from_user(user.into(), db).await,
); );
Template::render("admin/user/scheckbuch", context.into_json()) Template::render("admin/user/scheckbuch", context.into_json())

View File

@ -1,7 +1,7 @@
use crate::model::{ use crate::model::{
boat::Boat, boat::Boat,
boathouse::Boathouse, boathouse::Boathouse,
user::{AdminUser, UserWithRolesAndNotificationCount, VorstandUser}, user::{AdminUser, UserWithDetails, VorstandUser},
}; };
use rocket::{ use rocket::{
form::Form, form::Form,
@ -39,7 +39,7 @@ async fn index(
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(admin.into(), db).await, &UserWithDetails::from_user(admin.into(), db).await,
); );
Template::render("board/boathouse", context.into_json()) Template::render("board/boathouse", context.into_json())

View File

@ -13,7 +13,7 @@ use crate::{
model::{ model::{
boat::Boat, boat::Boat,
boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified}, boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified},
user::{CoxUser, DonauLinzUser, TechUser, User, UserWithRolesAndNotificationCount}, user::{CoxUser, DonauLinzUser, TechUser, User, UserWithDetails},
}, },
tera::log::KioskCookie, tera::log::KioskCookie,
}; };
@ -59,7 +59,7 @@ async fn index(
context.insert("boats", &boats); context.insert("boats", &boats);
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.into(), db).await, &UserWithDetails::from_user(user.into(), db).await,
); );
Template::render("boatdamages", context.into_json()) Template::render("boatdamages", context.into_json())

View File

@ -15,7 +15,7 @@ use crate::{
boat::Boat, boat::Boat,
boatreservation::{BoatReservation, BoatReservationToAdd}, boatreservation::{BoatReservation, BoatReservationToAdd},
log::Log, log::Log,
user::{DonauLinzUser, User, UserWithRolesAndNotificationCount}, user::{DonauLinzUser, User, UserWithDetails},
}, },
tera::log::KioskCookie, tera::log::KioskCookie,
}; };
@ -75,7 +75,7 @@ async fn index(
context.insert("user", &User::all(db).await); context.insert("user", &User::all(db).await);
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.into(), db).await, &UserWithDetails::from_user(user.into(), db).await,
); );
Template::render("boatreservations", context.into_json()) Template::render("boatreservations", context.into_json())

View File

@ -18,7 +18,7 @@ use tera::Context;
use crate::model::{ use crate::model::{
log::Log, log::Log,
user::{AdminUser, User, UserWithRolesAndNotificationCount}, user::{AdminUser, User, UserWithDetails},
}; };
#[derive(Serialize)] #[derive(Serialize)]
@ -51,7 +51,7 @@ async fn send(db: &State<SqlitePool>, _user: AdminUser) -> Template {
Template::render( Template::render(
"ergo.final", "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<SqlitePool>, user: User, flash: Option<FlashMessage<'_
if let Some(msg) = flash { if let Some(msg) = flash {
context.insert("flash", &msg.into_inner()); context.insert("flash", &msg.into_inner());
} }
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
context.insert("users", &users); context.insert("users", &users);
context.insert("thirty", &thirty); context.insert("thirty", &thirty);
context.insert("dozen", &dozen); context.insert("dozen", &dozen);

View File

@ -24,9 +24,7 @@ use crate::model::{
LogbookUpdateError, LogbookUpdateError,
}, },
logtype::LogType, logtype::LogType,
user::{ user::{AdminUser, DonauLinzUser, User, UserWithDetails},
AdminUser, DonauLinzUser, User, UserWithRolesAndNotificationCount, UserWithWaterStatus,
},
}; };
pub struct KioskCookie(String); pub struct KioskCookie(String);
@ -51,22 +49,22 @@ async fn index(
) -> Template { ) -> Template {
let boats = Boat::for_user(db, &user).await; let boats = Boat::for_user(db, &user).await;
let mut coxes: Vec<UserWithWaterStatus> = futures::future::join_all( let mut coxes: Vec<UserWithDetails> = futures::future::join_all(
User::cox(db) User::cox(db)
.await .await
.into_iter() .into_iter()
.map(|user| UserWithWaterStatus::from_user(user, db)), .map(|user| UserWithDetails::from_user(user, db)),
) )
.await; .await;
coxes.retain(|u| { coxes.retain(|u| {
u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into()) u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into())
}); });
let mut users: Vec<UserWithWaterStatus> = futures::future::join_all( let mut users: Vec<UserWithDetails> = futures::future::join_all(
User::all(db) User::all(db)
.await .await
.into_iter() .into_iter()
.map(|user| UserWithWaterStatus::from_user(user, db)), .map(|user| UserWithDetails::from_user(user, db)),
) )
.await; .await;
users.retain(|u| { users.retain(|u| {
@ -93,7 +91,7 @@ async fn index(
context.insert("logtypes", &logtypes); context.insert("logtypes", &logtypes);
context.insert( context.insert(
"loggedin_user", "loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user.into(), db).await, &UserWithDetails::from_user(user.into(), db).await,
); );
context.insert("on_water", &on_water); context.insert("on_water", &on_water);
context.insert("distances", &distances); context.insert("distances", &distances);
@ -107,7 +105,7 @@ async fn show(db: &State<SqlitePool>, user: DonauLinzUser) -> Template {
Template::render( Template::render(
"log.completed", "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<SqlitePool>, user: AdminUser, year: i32) -> Te
Template::render( Template::render(
"log.completed", "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, _kiosk: KioskCookie,
) -> Template { ) -> Template {
let boats = Boat::all(db).await; let boats = Boat::all(db).await;
let mut coxes: Vec<UserWithWaterStatus> = futures::future::join_all( let mut coxes: Vec<UserWithDetails> = futures::future::join_all(
User::cox(db) User::cox(db)
.await .await
.into_iter() .into_iter()
.map(|user| UserWithWaterStatus::from_user(user, db)), .map(|user| UserWithDetails::from_user(user, db)),
) )
.await; .await;
@ -165,11 +163,11 @@ async fn kiosk(
u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into()) u.roles.contains(&"Donau Linz".into()) || u.roles.contains(&"scheckbuch".into())
}); });
let mut users: Vec<UserWithWaterStatus> = futures::future::join_all( let mut users: Vec<UserWithDetails> = futures::future::join_all(
User::all(db) User::all(db)
.await .await
.into_iter() .into_iter()
.map(|user| UserWithWaterStatus::from_user(user, db)), .map(|user| UserWithDetails::from_user(user, db)),
) )
.await; .await;

View File

@ -23,7 +23,7 @@ use tera::Context;
use crate::model::{ use crate::model::{
notification::Notification, notification::Notification,
role::Role, role::Role,
user::{User, UserWithRolesAndNotificationCount}, user::{User, UserWithDetails},
}; };
pub(crate) mod admin; pub(crate) mod admin;
@ -53,10 +53,7 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
} }
context.insert("notifications", &Notification::for_user(db, &user).await); context.insert("notifications", &Notification::for_user(db, &user).await);
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
Template::render("index", context.into_json()) Template::render("index", context.into_json())
} }
@ -78,10 +75,7 @@ async fn steering(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage
context.insert("coxes", &coxes); context.insert("coxes", &coxes);
context.insert("bootskundige", &bootskundige); context.insert("bootskundige", &bootskundige);
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
Template::render("steering", context.into_json()) Template::render("steering", context.into_json())
} }

View File

@ -13,7 +13,7 @@ use crate::model::{
logbook::Logbook, logbook::Logbook,
tripdetails::TripDetails, tripdetails::TripDetails,
triptype::TripType, triptype::TripType,
user::{AllowedForPlannedTripsUser, User, UserWithRolesAndNotificationCount}, user::{AllowedForPlannedTripsUser, User, UserWithDetails},
usertrip::{UserTrip, UserTripDeleteError, UserTripError}, usertrip::{UserTrip, UserTripDeleteError, UserTripError},
}; };
@ -44,10 +44,7 @@ async fn index(
} }
context.insert("fee", &user.fee(db).await); context.insert("fee", &user.fee(db).await);
context.insert( context.insert("loggedin_user", &UserWithDetails::from_user(user, db).await);
"loggedin_user",
&UserWithRolesAndNotificationCount::from_user(user, db).await,
);
context.insert("days", &days); context.insert("days", &days);
Template::render("planned", context.into_json()) Template::render("planned", context.into_json())
} }

View File

@ -4,7 +4,7 @@ use sqlx::SqlitePool;
use crate::model::{ use crate::model::{
stat::{self, BoatStat, Stat}, stat::{self, BoatStat, Stat},
user::{DonauLinzUser, UserWithRolesAndNotificationCount}, user::{DonauLinzUser, UserWithDetails},
}; };
use super::log::KioskCookie; use super::log::KioskCookie;
@ -16,7 +16,7 @@ async fn index_boat(db: &State<SqlitePool>, user: DonauLinzUser) -> Template {
Template::render( Template::render(
"stat.boats", "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<SqlitePool>, user: DonauLinzUser, year: Option<i32>) -
Template::render( Template::render(
"stat.people", "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),
) )
} }