rowt/src/model/mod.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2023-04-04 12:19:56 +02:00
use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;
2023-04-04 15:16:21 +02:00
use self::{
2023-04-28 21:19:30 +02:00
planned_event::{PlannedEvent, PlannedEventWithUserAndTriptype},
trip::{Trip, TripWithUserAndType},
2023-04-04 15:16:21 +02:00
};
2023-04-04 12:19:56 +02:00
2023-04-18 12:10:11 +02:00
pub mod log;
2023-04-04 12:19:56 +02:00
pub mod planned_event;
2023-04-04 15:16:21 +02:00
pub mod trip;
2023-04-04 12:19:56 +02:00
pub mod tripdetails;
2023-04-28 21:19:30 +02:00
pub mod triptype;
2023-04-03 16:11:26 +02:00
pub mod user;
2023-04-04 15:16:21 +02:00
pub mod usertrip;
2023-04-04 12:19:56 +02:00
#[derive(Serialize)]
pub struct Day {
day: NaiveDate,
2023-04-28 21:19:30 +02:00
planned_events: Vec<PlannedEventWithUserAndTriptype>,
trips: Vec<TripWithUserAndType>,
2023-06-08 10:57:42 +02:00
is_pinned: bool,
2023-04-04 12:19:56 +02:00
}
impl Day {
2023-06-08 10:57:42 +02:00
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,
};
2023-04-04 12:19:56 +02:00
Self {
day,
2023-06-08 10:57:42 +02:00
planned_events,
2023-04-04 15:16:21 +02:00
trips: Trip::get_for_day(db, day).await,
2023-06-08 10:57:42 +02:00
is_pinned,
2023-04-04 12:19:56 +02:00
}
}
2023-06-08 10:57:42 +02:00
pub async fn new_guest(db: &SqlitePool, day: NaiveDate, is_pinned: bool) -> Self {
let mut day = Self::new(db, day, is_pinned).await;
2023-05-30 14:36:23 +02:00
day.planned_events.retain(|e| e.planned_event.allow_guests);
day.trips.retain(|t| t.trip.allow_guests);
day
}
2023-04-04 12:19:56 +02:00
}