add is_pinned functionality
This commit is contained in:
		| @@ -20,18 +20,24 @@ pub struct Day { | |||||||
|     day: NaiveDate, |     day: NaiveDate, | ||||||
|     planned_events: Vec<PlannedEventWithUserAndTriptype>, |     planned_events: Vec<PlannedEventWithUserAndTriptype>, | ||||||
|     trips: Vec<TripWithUserAndType>, |     trips: Vec<TripWithUserAndType>, | ||||||
|  |     is_pinned: bool, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Day { | 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 { |         Self { | ||||||
|             day, |             day, | ||||||
|             planned_events: PlannedEvent::get_for_day(db, day).await, |             planned_events, | ||||||
|             trips: Trip::get_for_day(db, day).await, |             trips: Trip::get_for_day(db, day).await, | ||||||
|  |             is_pinned, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     pub async fn new_guest(db: &SqlitePool, day: NaiveDate) -> Self { |     pub async fn new_guest(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self { | ||||||
|         let mut day = Self::new(db, day).await; |         let mut day = Self::new(db, day, is_pinned).await; | ||||||
|  |  | ||||||
|         day.planned_events.retain(|e| e.planned_event.allow_guests); |         day.planned_events.retain(|e| e.planned_event.allow_guests); | ||||||
|         day.trips.retain(|t| t.trip.allow_guests); |         day.trips.retain(|t| t.trip.allow_guests); | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ pub struct PlannedEvent { | |||||||
|     pub day: String, |     pub day: String, | ||||||
|     notes: Option<String>, |     notes: Option<String>, | ||||||
|     pub allow_guests: bool, |     pub allow_guests: bool, | ||||||
|  |     always_show: bool, | ||||||
|     trip_type_id: Option<i64>, |     trip_type_id: Option<i64>, | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -48,7 +49,7 @@ impl PlannedEvent { | |||||||
|             Self, |             Self, | ||||||
|             " |             " | ||||||
| SELECT | 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  | FROM planned_event  | ||||||
| INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id | INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id | ||||||
| WHERE planned_event.id like ? | WHERE planned_event.id like ? | ||||||
| @@ -60,6 +61,15 @@ WHERE planned_event.id like ? | |||||||
|         .ok() |         .ok() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     pub async fn get_pinned_for_day( | ||||||
|  |         db: &SqlitePool, | ||||||
|  |         day: NaiveDate, | ||||||
|  |     ) -> Vec<PlannedEventWithUserAndTriptype> { | ||||||
|  |         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( |     pub async fn get_for_day( | ||||||
|         db: &SqlitePool, |         db: &SqlitePool, | ||||||
|         day: NaiveDate, |         day: NaiveDate, | ||||||
| @@ -67,7 +77,7 @@ WHERE planned_event.id like ? | |||||||
|         let day = format!("{day}"); |         let day = format!("{day}"); | ||||||
|         let events = sqlx::query_as!( |         let events = sqlx::query_as!( | ||||||
|             PlannedEvent, |             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 | FROM planned_event | ||||||
| INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id | INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id | ||||||
| WHERE day=?", | WHERE day=?", | ||||||
| @@ -95,10 +105,25 @@ WHERE day=?", | |||||||
|         ret |         ret | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     pub async fn pinned_days(db: &SqlitePool, amount_days_to_skip: i64) -> Vec<NaiveDate> { | ||||||
|  |         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<String> = 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<PlannedEvent> { |     pub async fn all(db: &SqlitePool) -> Vec<PlannedEvent> { | ||||||
|         sqlx::query_as!( |         sqlx::query_as!( | ||||||
|             PlannedEvent, |             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 | FROM planned_event | ||||||
| INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id", | 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, |         db: &SqlitePool, | ||||||
|         name: &str, |         name: &str, | ||||||
|         planned_amount_cox: i32, |         planned_amount_cox: i32, | ||||||
|  |         always_show: bool, | ||||||
|         trip_details: TripDetails, |         trip_details: TripDetails, | ||||||
|     ) { |     ) { | ||||||
|         sqlx::query!( |         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, |             name, | ||||||
|             planned_amount_cox, |             planned_amount_cox, | ||||||
|             trip_details.id |             trip_details.id, | ||||||
|  |             always_show | ||||||
|         ) |         ) | ||||||
|         .execute(db) |         .execute(db) | ||||||
|         .await |         .await | ||||||
| @@ -255,7 +282,7 @@ mod test { | |||||||
|  |  | ||||||
|         let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap(); |         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 = |         let res = | ||||||
|             PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await; |             PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await; | ||||||
|   | |||||||
| @@ -17,6 +17,7 @@ struct AddPlannedEventForm<'r> { | |||||||
|     allow_guests: bool, |     allow_guests: bool, | ||||||
|     planned_starting_time: &'r str, |     planned_starting_time: &'r str, | ||||||
|     max_people: i32, |     max_people: i32, | ||||||
|  |     always_show: bool, | ||||||
|     notes: Option<&'r str>, |     notes: Option<&'r str>, | ||||||
|     trip_type: Option<i64>, |     trip_type: Option<i64>, | ||||||
| } | } | ||||||
| @@ -42,7 +43,14 @@ async fn create( | |||||||
|                                                                                     //just created |                                                                                     //just created | ||||||
|                                                                                     //the object |                                                                                     //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") |     Flash::success(Redirect::to("/"), "Successfully planned the event") | ||||||
| } | } | ||||||
|   | |||||||
| @@ -14,6 +14,7 @@ use sqlx::SqlitePool; | |||||||
|  |  | ||||||
| use crate::model::{ | use crate::model::{ | ||||||
|     log::Log, |     log::Log, | ||||||
|  |     planned_event::PlannedEvent, | ||||||
|     tripdetails::TripDetails, |     tripdetails::TripDetails, | ||||||
|     triptype::TripType, |     triptype::TripType, | ||||||
|     user::User, |     user::User, | ||||||
| @@ -57,9 +58,18 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_ | |||||||
|         let date = (Local::now() + Duration::days(i)).date_naive(); |         let date = (Local::now() + Duration::days(i)).date_naive(); | ||||||
|  |  | ||||||
|         if user.is_guest { |         if user.is_guest { | ||||||
|             days.push(Day::new_guest(db, date).await); |             days.push(Day::new_guest(db, date, false).await); | ||||||
|         } else { |         } else { | ||||||
|             days.push(Day::new(db, date).await); |             days.push(Day::new(db, date, false).await); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     for date in PlannedEvent::pinned_days(db, show_next_n_days).await { | ||||||
|  |         //TODO: extract the 2 if's (this block + prev. one) after the 2 for's | ||||||
|  |         if user.is_guest { | ||||||
|  |             days.push(Day::new_guest(db, date, true).await); | ||||||
|  |         } else { | ||||||
|  |             days.push(Day::new(db, date, true).await); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| {% block content %} | {% block content %} | ||||||
|   <div class="w-full max-w-md space-y-8"> |   <div class="w-full max-w-md space-y-8"> | ||||||
|     <div> |     <div> | ||||||
|       <img class="mx-auto h-16 w-auto" src="https://rudernlinz.at/wp-content/uploads/2021/02/cropped-logo.png" alt="Your Company"> |       <img class="mx-auto h-16 w-auto" src="https://rudernlinz.at/wp-content/uploads/2021/02/cropped-logo.png" alt="Logo Ruderassistent"> | ||||||
|       <h1 class="mt-6 h1">Ruderassistent</h1> |       <h1 class="mt-6 h1">Ruderassistent</h1> | ||||||
|     </div> |     </div> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ | |||||||
|     {{ macros::input(label='Anzahl Steuerleute', name='planned_amount_cox', type='number', required=true, min='0') }} |     {{ 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::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='Gäste erlauben', name='allow_guests') }} | ||||||
|  |     {{ macros::checkbox(label='Immer anzeigen', name='always_show') }} | ||||||
|     {{ macros::input(label='Anmerkungen', name='notes', type='input') }} |     {{ macros::input(label='Anmerkungen', name='notes', type='input') }} | ||||||
|     {{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }} |     {{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user