rowt/src/model/mod.rs

88 lines
2.3 KiB
Rust
Raw Normal View History

2024-09-03 20:35:43 +02:00
use chrono::{Local, NaiveDate};
2023-04-04 12:19:56 +02:00
use serde::Serialize;
use sqlx::SqlitePool;
use waterlevel::WaterlevelDay;
2023-04-04 12:19:56 +02:00
2024-09-03 20:35:43 +02:00
use crate::AMOUNT_DAYS_TO_SHOW_TRIPS_AHEAD;
2023-04-04 15:16:21 +02:00
use self::{
2024-05-28 09:08:48 +02:00
event::{Event, EventWithUserAndTriptype},
2023-04-28 21:19:30 +02:00
trip::{Trip, TripWithUserAndType},
2024-04-30 11:59:33 +02:00
waterlevel::Waterlevel,
2024-05-16 14:41:15 +02:00
weather::Weather,
2023-04-04 15:16:21 +02:00
};
2023-04-04 12:19:56 +02:00
pub mod boat;
2023-08-02 14:29:19 +02:00
pub mod boatdamage;
2024-03-08 13:13:20 +01:00
pub mod boathouse;
2024-03-30 01:36:37 +01:00
pub mod boatreservation;
pub mod distance;
2024-05-28 09:08:48 +02:00
pub mod event;
2024-01-18 16:37:54 +01:00
pub mod family;
2023-07-22 15:34:42 +02:00
pub mod location;
2023-04-18 12:10:11 +02:00
pub mod log;
2023-07-23 12:17:57 +02:00
pub mod logbook;
pub mod logtype;
2024-01-01 15:50:06 +01:00
pub mod mail;
pub mod notification;
pub mod role;
2023-07-24 13:01:39 +02:00
pub mod rower;
2023-07-24 20:56:46 +02:00
pub mod stat;
pub mod trailer;
pub mod trailerreservation;
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;
2024-04-30 11:59:33 +02:00
pub mod waterlevel;
2024-05-16 14:41:15 +02:00
pub mod weather;
2023-04-04 12:19:56 +02:00
#[derive(Serialize, Debug)]
2023-04-04 12:19:56 +02:00
pub struct Day {
day: NaiveDate,
2024-05-28 09:08:48 +02:00
events: Vec<EventWithUserAndTriptype>,
2023-04-28 21:19:30 +02:00
trips: Vec<TripWithUserAndType>,
2023-06-08 10:57:42 +02:00
is_pinned: bool,
2024-09-03 20:35:43 +02:00
regular_sees_this_day: bool,
max_waterlevel: Option<WaterlevelDay>,
2024-05-16 14:41:15 +02:00
weather: Option<Weather>,
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 {
2024-09-03 20:35:43 +02:00
let today = Local::now().date_naive();
let day_diff = (day - today).num_days() + 1;
let regular_sees_this_day = day_diff <= AMOUNT_DAYS_TO_SHOW_TRIPS_AHEAD;
2023-07-25 13:32:20 +02:00
if is_pinned {
Self {
day,
2024-05-28 09:08:48 +02:00
events: Event::get_pinned_for_day(db, day).await,
2023-07-25 13:32:20 +02:00
trips: Trip::get_pinned_for_day(db, day).await,
is_pinned,
2024-09-03 20:35:43 +02:00
regular_sees_this_day,
2024-04-30 11:59:33 +02:00
max_waterlevel: Waterlevel::max_waterlevel_for_day(db, day).await,
2024-05-16 14:41:15 +02:00
weather: Weather::find_by_day(db, day).await,
2023-07-25 13:32:20 +02:00
}
} else {
Self {
day,
2024-05-28 09:08:48 +02:00
events: Event::get_for_day(db, day).await,
2023-07-25 13:32:20 +02:00
trips: Trip::get_for_day(db, day).await,
is_pinned,
2024-09-03 20:35:43 +02:00
regular_sees_this_day,
2024-04-30 11:59:33 +02:00
max_waterlevel: Waterlevel::max_waterlevel_for_day(db, day).await,
2024-05-16 14:41:15 +02:00
weather: Weather::find_by_day(db, day).await,
2023-07-25 13:32:20 +02:00
}
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;
2024-05-28 09:08:48 +02:00
day.events.retain(|e| e.event.allow_guests);
2023-05-30 14:36:23 +02:00
day.trips.retain(|t| t.trip.allow_guests);
day
}
2023-04-04 12:19:56 +02:00
}