From 97543dae8b0ff8d5cccafc59fbd956e98d141181 Mon Sep 17 00:00:00 2001 From: Joe Abbate Date: Sun, 13 Oct 2024 17:38:59 -0400 Subject: [PATCH] Oops --- src/api/v1/event/car/mod.rs | 19 ++++++++----------- src/api/v1/event/car/rider/mod.rs | 2 +- src/api/v1/event/mod.rs | 10 +++++----- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/api/v1/event/car/mod.rs b/src/api/v1/event/car/mod.rs index 5eb2ee4..e23d92a 100644 --- a/src/api/v1/event/car/mod.rs +++ b/src/api/v1/event/car/mod.rs @@ -63,7 +63,7 @@ pub struct CreateCar { pub departure_time: DateTime, pub return_time: DateTime, pub comment: String, - pub riders: Vec, + pub riders: Vec, } fn validate_car(car: &CreateCar, user: &String, other_cars: Vec) -> Vec { @@ -82,9 +82,6 @@ fn validate_car(car: &CreateCar, user: &String, other_cars: Vec) -> Vec } if car .riders - .iter() - .map(|rider| rider.id.clone()) - .collect::>() .contains(user) { out.push("You cannot be a rider in your own car.".to_string()); @@ -99,14 +96,14 @@ fn validate_car(car: &CreateCar, user: &String, other_cars: Vec) -> Vec .map(|user| user.id) .collect(); for rider in car.riders.iter() { - if other_car_members.contains(&rider.id) { + if other_car_members.contains(&rider) { out.push(format!( "{} is already in another car or is a driver.", - rider.name + rider )) } } - return out; + out } #[utoipa::path( @@ -141,7 +138,7 @@ async fn create_car( .fetch_all(&mut *tx) .await.unwrap(); let validate = validate_car(&car, &user_id, other_cars); - if validate.len() != 0 { + if !validate.is_empty() { return HttpResponse::BadRequest().json(json!({ "errors": validate })); @@ -161,7 +158,7 @@ async fn create_car( INSERT INTO rider (car_id, rider) SELECT $1, * FROM UNNEST($2::VARCHAR[]) "#, record.id, - &car.riders.iter().map(|rider| rider.id.clone()).collect::>() + &car.riders ).execute(&mut *tx).await.unwrap(); tx.commit().await.unwrap(); HttpResponse::Ok().json(record.id) @@ -267,7 +264,7 @@ async fn update_car( .fetch_all(&mut *tx) .await.unwrap(); let validate = validate_car(&car, &user_id, other_cars); - if validate.len() != 0 { + if !validate.is_empty() { return HttpResponse::BadRequest().json(json!({ "errors": validate })); @@ -309,7 +306,7 @@ async fn update_car( INSERT INTO rider (car_id, rider) SELECT $1, * FROM UNNEST($2::VARCHAR[]) "#, car_id, - &car.riders.iter().map(|rider| rider.id.clone()).collect::>() + &car.riders ).execute(&mut *tx).await.unwrap(); tx.commit().await.unwrap(); diff --git a/src/api/v1/event/car/rider/mod.rs b/src/api/v1/event/car/rider/mod.rs index dfd1802..bd7e708 100644 --- a/src/api/v1/event/car/rider/mod.rs +++ b/src/api/v1/event/car/rider/mod.rs @@ -34,7 +34,7 @@ async fn create_rider( let user_id = session.get::("userinfo").unwrap().unwrap().id; let check = query!( - r#"SELECT COUNT(*) as count FROM (SELECT id FROM car WHERE event_id = $1 AND driver = $2 UNION SELECT rider.car_id FROM rider JOIN car ON rider.car_id = car.id WHERE car.event_id = $1 AND rider.rider = $2)"#, + r#"SELECT COUNT(*) as count FROM (SELECT id FROM car WHERE event_id = $1 AND driver = $2 UNION SELECT rider.car_id FROM rider JOIN car ON rider.car_id = car.id WHERE car.event_id = $1 AND rider.rider = $2) AS data"#, event_id, user_id ).fetch_one(&data.db).await.unwrap(); diff --git a/src/api/v1/event/mod.rs b/src/api/v1/event/mod.rs index 14e7fc5..d1a57c4 100644 --- a/src/api/v1/event/mod.rs +++ b/src/api/v1/event/mod.rs @@ -58,10 +58,10 @@ pub struct CreateEvent { fn validate_event(event: &CreateEvent) -> Vec { let mut out = Vec::new(); - if event.name.len() == 0 { + if event.name.is_empty() { out.push("Missing Name.".to_string()); } - if event.location.len() == 0 { + if event.location.is_empty() { out.push("Missing Location.".to_string()); } if event.start_time < event.end_time { @@ -70,7 +70,7 @@ fn validate_event(event: &CreateEvent) -> Vec { if event.end_time < Utc::now() { out.push("Event cannot be in the past.".to_string()) } - return out; + out } #[utoipa::path( @@ -85,7 +85,7 @@ async fn create_event( event: web::Json, ) -> impl Responder { let validate = validate_event(&event); - if validate.len() != 0 { + if !validate.is_empty() { return HttpResponse::BadRequest().json(json!( { "errors": validate @@ -175,7 +175,7 @@ async fn update_event( let event_id = path.into_inner(); let validate = validate_event(&event); - if validate.len() != 0 { + if !validate.is_empty() { return HttpResponse::BadRequest().json(json!( { "errors": validate