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 serde::Serialize;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use super::tripdetails::TripDetails;
#[derive(Serialize, Clone)] #[derive(Serialize, Clone)]
pub struct PlannedEvent { pub struct PlannedEvent {
pub id: i64, pub id: i64,
@ -109,15 +111,15 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
name: String, name: String,
planned_amount_cox: i32, planned_amount_cox: i32,
allow_guests: bool, allow_guests: bool,
trip_details_id: i64, trip_details: TripDetails,
) { ) {
sqlx::query!( sqlx::query!(
"INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)", "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) .execute(db)
.await .await
.unwrap(); //TODO: fixme .unwrap(); //Okay, as TripDetails can only be created with proper DB backing
} }
pub async fn delete(&self, db: &SqlitePool) { 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)] #[cfg(test)]
mod test { mod test {
use crate::testdb; use crate::{model::tripdetails::TripDetails, testdb};
use super::PlannedEvent; use super::PlannedEvent;
use chrono::NaiveDate; use chrono::NaiveDate;
@ -149,7 +151,9 @@ mod test {
fn test_create() { fn test_create() {
let pool = testdb!(); 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 = let res =
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await; PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;

View File

@ -36,13 +36,17 @@ async fn create(
) )
.await; .await;
let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc. we
//just created
//the object
//TODO: fix clone() //TODO: fix clone()
PlannedEvent::create( PlannedEvent::create(
db, db,
data.name.clone(), data.name.clone(),
data.planned_amount_cox, data.planned_amount_cox,
data.allow_guests, data.allow_guests,
trip_details_id, trip_details,
) )
.await; .await;