diff --git a/src/model/trip.rs b/src/model/trip.rs index b7d3d5e..b4c5437 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -3,10 +3,11 @@ use serde::Serialize; use sqlx::SqlitePool; use super::{ + notification::Notification, planned_event::{PlannedEvent, Registration}, tripdetails::TripDetails, triptype::TripType, - user::CoxUser, + user::{CoxUser, User}, }; #[derive(Serialize, Clone, Debug)] @@ -43,6 +44,34 @@ impl Trip { ) .execute(db) .await; + + let same_starting_datetime = TripDetails::find_by_startingdatetime( + db, + trip_details.day, + trip_details.planned_starting_time, + ) + .await; + if same_starting_datetime.len() > 1 { + for notify in same_starting_datetime { + if notify.id != trip_details.id { + // notify everyone except oneself + if let Some(trip) = Trip::find_by_trip_details(db, notify.id).await { + let user = User::find_by_id(db, trip.cox_id as i32).await.unwrap(); + Notification::create( + db, + &user, + &format!( + "{} hat eine Ausfahrt zur selben Zeit ({} um {}) wie du erstellt", + user.name, trip.day, trip.planned_starting_time + ), + "Neue Ausfahrt zur selben Zeit".into(), + None, + ) + .await; + } + } + } + } } pub async fn find_by_trip_details(db: &SqlitePool, tripdetails_id: i64) -> Option { diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index aeec004..485e66f 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -46,6 +46,24 @@ WHERE id like ? .ok() } + pub async fn find_by_startingdatetime( + db: &SqlitePool, + day: String, + planned_starting_time: String, + ) -> Vec { + sqlx::query_as!( + Self, + " +SELECT id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show, is_locked +FROM trip_details +WHERE day = ? AND planned_starting_time = ? + " + , day, planned_starting_time + ) + .fetch_all(db) + .await.unwrap() + } + /// Creates a new entry in `trip_details` and returns its id. pub async fn create(db: &SqlitePool, tripdetails: TripDetailsToAdd<'_>) -> i64 { let query = sqlx::query!(