send triptype to frontend

This commit is contained in:
2023-04-28 21:19:51 +02:00
parent bd5a28b342
commit 3d5ad30904
8 changed files with 109 additions and 21 deletions

View File

@ -1,10 +1,10 @@
use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;
use sqlx::{FromRow, SqlitePool};
use super::tripdetails::TripDetails;
use super::{tripdetails::TripDetails, triptype::TripType};
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, FromRow)]
pub struct PlannedEvent {
pub id: i64,
name: String,
@ -15,12 +15,14 @@ pub struct PlannedEvent {
max_people: i64,
day: String,
notes: Option<String>,
trip_type_id: Option<i64>,
}
#[derive(Serialize)]
pub struct PlannedEventWithUser {
pub struct PlannedEventWithUserAndTriptype {
#[serde(flatten)]
planned_event: PlannedEvent,
trip_type: Option<TripType>,
cox_needed: bool,
cox: Vec<Registration>,
rower: Vec<Registration>,
@ -39,7 +41,8 @@ impl PlannedEvent {
sqlx::query_as!(
Self,
"
SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
SELECT
planned_event.id, planned_event.name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE planned_event.id like ?
@ -51,11 +54,14 @@ WHERE planned_event.id like ?
.ok()
}
pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec<PlannedEventWithUser> {
pub async fn get_for_day(
db: &SqlitePool,
day: NaiveDate,
) -> Vec<PlannedEventWithUserAndTriptype> {
let day = format!("{day}");
let events = sqlx::query_as!(
PlannedEvent,
"SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
"SELECT planned_event.id, planned_event.name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes, trip_type_id
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE day=?",
@ -68,11 +74,16 @@ WHERE day=?",
let mut ret = Vec::new();
for event in events {
let cox = event.get_all_cox(db).await;
ret.push(PlannedEventWithUser {
let mut trip_type = None;
if let Some(trip_type_id) = event.trip_type_id {
trip_type = TripType::find_by_id(db, trip_type_id).await;
}
ret.push(PlannedEventWithUserAndTriptype {
planned_event: event.clone(),
cox_needed: event.planned_amount_cox > cox.len() as i64,
cox,
rower: event.get_all_rower(db).await,
trip_type,
});
}
ret