From 3abff08f616524e7aec5001ecaa3122b10dca85f Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 23 Apr 2024 20:40:06 +0200 Subject: [PATCH] [TASK] group reservations in log to avoid near-duplicates --- src/model/boatreservation.rs | 31 +++++++++++++++++++++++++++-- src/model/user.rs | 2 +- src/tera/log.rs | 10 ++++++++-- templates/includes/macros.html.tera | 8 ++++++-- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/model/boatreservation.rs b/src/model/boatreservation.rs index 311f966..12db94e 100644 --- a/src/model/boatreservation.rs +++ b/src/model/boatreservation.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use crate::model::{boat::Boat, user::User}; use chrono::NaiveDate; use chrono::NaiveDateTime; @@ -24,7 +26,7 @@ pub struct BoatReservation { #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct BoatReservationWithDetails { #[serde(flatten)] - boat_reservation: BoatReservation, + reservation: BoatReservation, boat: Boat, user_applicant: User, user_confirmation: Option, @@ -84,7 +86,7 @@ WHERE end_date >= CURRENT_DATE ORDER BY end_date .unwrap(); res.push(BoatReservationWithDetails { - boat_reservation: reservation, + reservation, boat, user_applicant, user_confirmation, @@ -92,6 +94,31 @@ WHERE end_date >= CURRENT_DATE ORDER BY end_date } res } + pub async fn all_future_with_groups( + db: &SqlitePool, + ) -> HashMap> { + let mut grouped_reservations: HashMap> = + HashMap::new(); + + let reservations = Self::all_future(db).await; + for reservation in reservations { + let key = format!( + "{}-{}-{}-{}-{}", + reservation.reservation.start_date, + reservation.reservation.end_date, + reservation.reservation.time_desc, + reservation.reservation.usage, + reservation.user_applicant.name + ); + + grouped_reservations + .entry(key) + .or_insert_with(Vec::new) + .push(reservation); + } + + grouped_reservations + } pub async fn create( db: &SqlitePool, diff --git a/src/model/user.rs b/src/model/user.rs index 86e311a..aab1a35 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -26,7 +26,7 @@ const REGULAR: i32 = 22000; const UNTERSTUETZEND: i32 = 2500; const FOERDERND: i32 = 8500; -#[derive(FromRow, Serialize, Deserialize, Clone, Debug)] +#[derive(FromRow, Serialize, Deserialize, Clone, Debug, Eq, Hash)] pub struct User { pub id: i64, pub name: String, diff --git a/src/tera/log.rs b/src/tera/log.rs index 011aff7..d5a4d6e 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -78,7 +78,10 @@ async fn index( } context.insert("boats", &boats); - context.insert("reservations", &BoatReservation::all_future(db).await); + context.insert( + "reservations", + &BoatReservation::all_future_with_groups(db).await, + ); context.insert("coxes", &coxes); context.insert("users", &users); context.insert("logtypes", &logtypes); @@ -169,7 +172,10 @@ async fn kiosk( } context.insert("boats", &boats); - context.insert("reservations", &BoatReservation::all_future(db).await); + context.insert( + "reservations", + &BoatReservation::all_future_with_groups(db).await, + ); context.insert("coxes", &coxes); context.insert("users", &users); context.insert("logtypes", &logtypes); diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index e591143..da63170 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -2,7 +2,8 @@

Reservierungen ({{ reservations | length }})

- {% for reservation in reservations %} + {% for _, reservations_for_event in reservations %} + {% set reservation = reservations_for_event[0] %}
{{ reservation.start_date | date(format="%d.%m.%Y") }} @@ -13,7 +14,10 @@ ({{ reservation.time_desc }}) - {{ reservation.boat.name }} + {% for reservation in reservations_for_event %} + {{ reservation.boat.name }} + {% if not loop.last %}+{% endif %} + {% endfor %} ({{ reservation.user_applicant.name }}) {{ reservation.usage }} -- 2.45.2