clean function

This commit is contained in:
philipp 2023-04-26 17:02:47 +02:00
parent 346a9b1d91
commit ef921314d2
2 changed files with 14 additions and 6 deletions

View File

@ -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;

View File

@ -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;