add is_pinned functionality

This commit is contained in:
2023-06-08 10:57:42 +02:00
parent c264b70e26
commit 87ed710f66
6 changed files with 67 additions and 15 deletions

View File

@ -20,18 +20,24 @@ pub struct Day {
day: NaiveDate,
planned_events: Vec<PlannedEventWithUserAndTriptype>,
trips: Vec<TripWithUserAndType>,
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);

View File

@ -21,6 +21,7 @@ pub struct PlannedEvent {
pub day: String,
notes: Option<String>,
pub allow_guests: bool,
always_show: bool,
trip_type_id: Option<i64>,
}
@ -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<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(
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<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> {
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;