42 lines
1014 B
Rust
42 lines
1014 B
Rust
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
|
use serde::Serialize;
|
|
|
|
use super::{day, trip, user};
|
|
|
|
#[derive(Serialize, Debug)]
|
|
pub struct TripWithUser {
|
|
pub trip: trip::Model,
|
|
pub user: user::Model,
|
|
}
|
|
|
|
impl TripWithUser {
|
|
fn new(trip: trip::Model, user: user::Model) -> Self {
|
|
Self { trip, user }
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Debug)]
|
|
pub struct DayWithTrips {
|
|
pub day: day::Model,
|
|
pub trips: Vec<TripWithUser>,
|
|
}
|
|
|
|
impl DayWithTrips {
|
|
pub async fn new(day: day::Model, db: &DatabaseConnection) -> Self {
|
|
let mut trips = Vec::new();
|
|
|
|
let trips_with_users: Vec<(trip::Model, Option<user::Model>)> = trip::Entity::find()
|
|
.filter(trip::Column::Day.eq(day.day))
|
|
.find_also_related(user::Entity)
|
|
.all(db)
|
|
.await
|
|
.unwrap();
|
|
|
|
for (trip, users) in trips_with_users {
|
|
trips.push(TripWithUser::new(trip, users.unwrap()));
|
|
}
|
|
|
|
Self { day, trips }
|
|
}
|
|
}
|