diff --git a/Cargo.lock b/Cargo.lock index 15c2d5f..30c7155 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1655,9 +1655,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.26" +version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "polyval" diff --git a/README.md b/README.md index b2be18a..d40bc4c 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - [] Allow sign-outs only >2h before event - [] add `always_show` to `planned_trips` (e.g. for wanderfahrten) - [] `planned_events` -> ICS feed to be [integrated](https://icscalendar.com/shortcode-overview/) in homepage calendar +- [] Notification system (-> signal msgs?) e.g. if new event; somebody unregistered # Nice to have ## Frontend diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index a776cd9..3364a34 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -2,7 +2,7 @@ use chrono::NaiveDate; use serde::Serialize; use sqlx::{FromRow, SqlitePool}; -use super::{tripdetails::TripDetails, triptype::TripType}; +use super::{tripdetails::TripDetails, triptype::TripType, user::User}; #[derive(Serialize, Clone, FromRow)] pub struct PlannedEvent { @@ -90,6 +90,7 @@ WHERE day=?", } async fn get_all_cox(&self, db: &SqlitePool) -> Vec { + //TODO: switch to join sqlx::query_as!( Registration, " @@ -107,6 +108,7 @@ FROM trip WHERE planned_event_id = ? } async fn get_all_rower(&self, db: &SqlitePool) -> Vec { + //TODO: switch to join sqlx::query_as!( Registration, " @@ -123,6 +125,23 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even .unwrap() //Okay, as PlannedEvent can only be created with proper DB backing } + //TODO: add tests + pub async fn is_rower_registered(&self, db: &SqlitePool, user: &User) -> bool { + let is_rower = sqlx::query!( + "SELECT count(*) as amount + FROM user_trip + WHERE trip_details_id = + (SELECT trip_details_id FROM planned_event WHERE id = ?) + AND user_id = ?", + self.id, + user.id + ) + .fetch_one(db) + .await + .unwrap(); //Okay, bc planned_event can only be created with proper DB backing + is_rower.amount > 0 + } + pub async fn create( db: &SqlitePool, name: String, diff --git a/src/model/trip.rs b/src/model/trip.rs index 7d1f8fa..6314e7a 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -67,19 +67,7 @@ WHERE trip.id=? cox: &CoxUser, planned_event: &PlannedEvent, ) -> Result<(), CoxHelpError> { - let is_rower = sqlx::query!( - "SELECT count(*) as amount - FROM user_trip - WHERE trip_details_id = - (SELECT trip_details_id FROM planned_event WHERE id = ?) - AND user_id = ?", - planned_event.id, - cox.id - ) - .fetch_one(db) - .await - .unwrap(); //Okay, bc planned_event can only be created with proper DB backing - if is_rower.amount > 0 { + if planned_event.is_rower_registered(db, &cox).await { return Err(CoxHelpError::AlreadyRegisteredAsRower); } @@ -112,6 +100,7 @@ WHERE day=? .fetch_all(db) .await .unwrap(); //TODO: fixme + let mut ret = Vec::new(); for trip in trips { let mut trip_type = None; diff --git a/src/model/tripdetails.rs b/src/model/tripdetails.rs index a834f90..0026b2b 100644 --- a/src/model/tripdetails.rs +++ b/src/model/tripdetails.rs @@ -63,14 +63,7 @@ WHERE id like ? .unwrap(); //TODO: fixme let amount_currently_registered = i64::from(amount_currently_registered.count); - let amount_allowed_to_register = - sqlx::query!("SELECT max_people FROM trip_details WHERE id = ?", self.id) - .fetch_one(db) - .await - .unwrap(); //Okay, TripDetails can only be created if self.id exists - let amount_allowed_to_register = amount_allowed_to_register.max_people; - - amount_currently_registered >= amount_allowed_to_register + amount_currently_registered >= self.max_people } } diff --git a/src/model/usertrip.rs b/src/model/usertrip.rs index b8c4f47..a12cab7 100644 --- a/src/model/usertrip.rs +++ b/src/model/usertrip.rs @@ -34,6 +34,7 @@ impl UserTrip { return Err(UserTripError::CantRegisterAtOwnEvent); } + //TODO: can probably move to trip.rs? //check if cox if planned_event let is_cox = sqlx::query!( "SELECT count(*) as amount diff --git a/src/rest/mod.rs b/src/rest/mod.rs index d19eb3d..78eb77d 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -23,26 +23,30 @@ mod admin; mod auth; mod cox; +fn amount_days_to_show(is_cox: bool) -> i64 { + if is_cox { + let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); + end_of_year + .signed_duration_since(Local::now().date_naive()) + .num_days() + + 1 + } else { + 6 + } +} + #[get("/")] async fn index(db: &State, user: User, flash: Option>) -> Template { let mut days = Vec::new(); let mut context = Context::new(); - let mut show_next_n_days = 6; - if user.is_cox { - let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); - show_next_n_days = end_of_year - .signed_duration_since(Local::now().date_naive()) - .num_days() - + 1; - } - if user.is_cox || user.is_admin { let triptypes = TripType::all(db).await; context.insert("trip_types", &triptypes); } + let show_next_n_days = amount_days_to_show(user.is_cox); for i in 0..show_next_n_days { let date = (Local::now() + Duration::days(i)).date_naive();