Skip to content

Commit

Permalink
Oops
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbate19 committed Oct 13, 2024
1 parent 7a3953a commit 97543da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
19 changes: 8 additions & 11 deletions src/api/v1/event/car/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct CreateCar {
pub departure_time: DateTime<Utc>,
pub return_time: DateTime<Utc>,
pub comment: String,
pub riders: Vec<UserData>,
pub riders: Vec<String>,
}

fn validate_car(car: &CreateCar, user: &String, other_cars: Vec<CarData>) -> Vec<String> {
Expand All @@ -82,9 +82,6 @@ fn validate_car(car: &CreateCar, user: &String, other_cars: Vec<CarData>) -> Vec
}
if car
.riders
.iter()
.map(|rider| rider.id.clone())
.collect::<Vec<String>>()
.contains(user)
{
out.push("You cannot be a rider in your own car.".to_string());
Expand All @@ -99,14 +96,14 @@ fn validate_car(car: &CreateCar, user: &String, other_cars: Vec<CarData>) -> 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(
Expand Down Expand Up @@ -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
}));
Expand All @@ -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::<Vec<String>>()
&car.riders
).execute(&mut *tx).await.unwrap();
tx.commit().await.unwrap();
HttpResponse::Ok().json(record.id)
Expand Down Expand Up @@ -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
}));
Expand Down Expand Up @@ -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::<Vec<String>>()
&car.riders
).execute(&mut *tx).await.unwrap();
tx.commit().await.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/api/v1/event/car/rider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn create_rider(
let user_id = session.get::<UserInfo>("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();

Expand Down
10 changes: 5 additions & 5 deletions src/api/v1/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ pub struct CreateEvent {

fn validate_event(event: &CreateEvent) -> Vec<String> {
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 {
Expand All @@ -70,7 +70,7 @@ fn validate_event(event: &CreateEvent) -> Vec<String> {
if event.end_time < Utc::now() {
out.push("Event cannot be in the past.".to_string())
}
return out;
out
}

#[utoipa::path(
Expand All @@ -85,7 +85,7 @@ async fn create_event(
event: web::Json<CreateEvent>,
) -> impl Responder {
let validate = validate_event(&event);
if validate.len() != 0 {
if !validate.is_empty() {
return HttpResponse::BadRequest().json(json!(
{
"errors": validate
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 97543da

Please sign in to comment.