From 87ed710f660d9979af70bcfe1ff12ce737d0434b Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 8 Jun 2023 10:57:42 +0200 Subject: [PATCH] add is_pinned functionality --- src/model/mod.rs | 14 ++++++++---- src/model/planned_event.rs | 39 ++++++++++++++++++++++++++++----- src/rest/admin/planned_event.rs | 10 ++++++++- src/rest/mod.rs | 14 ++++++++++-- templates/auth/login.html.tera | 4 ++-- templates/forms/event.html.tera | 1 + 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/model/mod.rs b/src/model/mod.rs index 4c428c9..76e92fb 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -20,18 +20,24 @@ pub struct Day { day: NaiveDate, planned_events: Vec, trips: Vec, + is_pinned: bool, } impl Day { - pub async fn new(db: &SqlitePool, day: NaiveDate) -> Self { + pub async fn new(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self { + let planned_events = match is_pinned { + true => PlannedEvent::get_pinned_for_day(db, day).await, + false => PlannedEvent::get_for_day(db, day).await, + }; Self { day, - planned_events: PlannedEvent::get_for_day(db, day).await, + planned_events, trips: Trip::get_for_day(db, day).await, + is_pinned, } } - pub async fn new_guest(db: &SqlitePool, day: NaiveDate) -> Self { - let mut day = Self::new(db, day).await; + pub async fn new_guest(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self { + let mut day = Self::new(db, day, is_pinned).await; day.planned_events.retain(|e| e.planned_event.allow_guests); day.trips.retain(|t| t.trip.allow_guests); diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index 046ea4a..0bfd7ea 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -21,6 +21,7 @@ pub struct PlannedEvent { pub day: String, notes: Option, pub allow_guests: bool, + always_show: bool, trip_type_id: Option, } @@ -48,7 +49,7 @@ impl PlannedEvent { Self, " SELECT - planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id + planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id, always_show FROM planned_event INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id WHERE planned_event.id like ? @@ -60,6 +61,15 @@ WHERE planned_event.id like ? .ok() } + pub async fn get_pinned_for_day( + db: &SqlitePool, + day: NaiveDate, + ) -> Vec { + let mut events = Self::get_for_day(db, day).await; + events.retain(|e| e.planned_event.always_show); + events + } + pub async fn get_for_day( db: &SqlitePool, day: NaiveDate, @@ -67,7 +77,7 @@ WHERE planned_event.id like ? let day = format!("{day}"); let events = sqlx::query_as!( PlannedEvent, - "SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id + "SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id FROM planned_event INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id WHERE day=?", @@ -95,10 +105,25 @@ WHERE day=?", ret } + pub async fn pinned_days(db: &SqlitePool, amount_days_to_skip: i64) -> Vec { + let query = format!( + "SELECT DISTINCT day +FROM planned_event +INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id +WHERE always_show=true AND day > datetime('now' , '+{} days') +ORDER BY day;", + amount_days_to_skip + ); + let days: Vec = sqlx::query_scalar(&query).fetch_all(db).await.unwrap(); + days.into_iter() + .map(|a| NaiveDate::parse_from_str(&a, "%Y-%m-%d").unwrap()) + .collect() + } + pub async fn all(db: &SqlitePool) -> Vec { sqlx::query_as!( PlannedEvent, - "SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id + "SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id FROM planned_event INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id", ) @@ -164,13 +189,15 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even db: &SqlitePool, name: &str, planned_amount_cox: i32, + always_show: bool, trip_details: TripDetails, ) { sqlx::query!( - "INSERT INTO planned_event(name, planned_amount_cox, trip_details_id) VALUES(?, ?, ?)", + "INSERT INTO planned_event(name, planned_amount_cox, trip_details_id, always_show) VALUES(?, ?, ?, ?)", name, planned_amount_cox, - trip_details.id + trip_details.id, + always_show ) .execute(db) .await @@ -255,7 +282,7 @@ mod test { let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap(); - PlannedEvent::create(&pool, "new-event".into(), 2, trip_details).await; + PlannedEvent::create(&pool, "new-event".into(), 2, false, trip_details).await; let res = PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await; diff --git a/src/rest/admin/planned_event.rs b/src/rest/admin/planned_event.rs index 67297a9..40837fb 100644 --- a/src/rest/admin/planned_event.rs +++ b/src/rest/admin/planned_event.rs @@ -17,6 +17,7 @@ struct AddPlannedEventForm<'r> { allow_guests: bool, planned_starting_time: &'r str, max_people: i32, + always_show: bool, notes: Option<&'r str>, trip_type: Option, } @@ -42,7 +43,14 @@ async fn create( //just created //the object - PlannedEvent::create(db, data.name, data.planned_amount_cox, trip_details).await; + PlannedEvent::create( + db, + data.name, + data.planned_amount_cox, + data.always_show, + trip_details, + ) + .await; Flash::success(Redirect::to("/"), "Successfully planned the event") } diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 3e8850c..d61b780 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -14,6 +14,7 @@ use sqlx::SqlitePool; use crate::model::{ log::Log, + planned_event::PlannedEvent, tripdetails::TripDetails, triptype::TripType, user::User, @@ -57,9 +58,18 @@ async fn index(db: &State, user: User, flash: Option
- Your Company + Logo Ruderassistent

Ruderassistent

@@ -31,4 +31,4 @@ -{% endblock content %} \ No newline at end of file +{% endblock content %} diff --git a/templates/forms/event.html.tera b/templates/forms/event.html.tera index 6832918..495dc39 100644 --- a/templates/forms/event.html.tera +++ b/templates/forms/event.html.tera @@ -8,6 +8,7 @@ {{ macros::input(label='Anzahl Steuerleute', name='planned_amount_cox', type='number', required=true, min='0') }} {{ macros::input(label='Anzahl Ruderer (ohne Steuerperson)', name='max_people', type='number', required=true, min='0') }} {{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }} + {{ macros::checkbox(label='Immer anzeigen', name='always_show') }} {{ macros::input(label='Anmerkungen', name='notes', type='input') }} {{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }}