diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index b1d1712..0953594 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -2,6 +2,8 @@ use chrono::NaiveDate; use serde::Serialize; use sqlx::SqlitePool; +use super::tripdetails::TripDetails; + #[derive(Serialize, Clone)] pub struct PlannedEvent { pub id: i64, @@ -109,15 +111,15 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even name: String, planned_amount_cox: i32, allow_guests: bool, - trip_details_id: i64, + trip_details: TripDetails, ) { sqlx::query!( "INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)", - name, planned_amount_cox, allow_guests, trip_details_id + name, planned_amount_cox, allow_guests, trip_details.id ) .execute(db) .await - .unwrap(); //TODO: fixme + .unwrap(); //Okay, as TripDetails can only be created with proper DB backing } pub async fn delete(&self, db: &SqlitePool) { @@ -130,7 +132,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even #[cfg(test)] mod test { - use crate::testdb; + use crate::{model::tripdetails::TripDetails, testdb}; use super::PlannedEvent; use chrono::NaiveDate; @@ -149,7 +151,9 @@ mod test { fn test_create() { let pool = testdb!(); - PlannedEvent::create(&pool, "new-event".into(), 2, false, 1).await; + let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap(); + + 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; diff --git a/src/rest/admin/planned_event.rs b/src/rest/admin/planned_event.rs index 7b2ee52..61cd20f 100644 --- a/src/rest/admin/planned_event.rs +++ b/src/rest/admin/planned_event.rs @@ -36,13 +36,17 @@ async fn create( ) .await; + let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc. we + //just created + //the object + //TODO: fix clone() PlannedEvent::create( db, data.name.clone(), data.planned_amount_cox, data.allow_guests, - trip_details_id, + trip_details, ) .await;