From 194577f9bdfc6079fca7ad7c5774f6a1bddaaf77 Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 6 Apr 2023 18:48:18 +0200 Subject: [PATCH] check if already cox on planned events --- src/model/usertrip.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/model/usertrip.rs b/src/model/usertrip.rs index 6a8d8a2..060e278 100644 --- a/src/model/usertrip.rs +++ b/src/model/usertrip.rs @@ -14,6 +14,7 @@ impl UserTrip { return Err(UserTripError::EventAlreadyFull); } + //check if cox if own event let is_cox = sqlx::query!( "SELECT count(*) as amount FROM trip @@ -29,6 +30,24 @@ impl UserTrip { return Err(UserTripError::AlreadyRegisteredAsCox); } + //check if cox if planned_event + let is_cox = sqlx::query!( + "SELECT count(*) as amount + FROM trip + WHERE planned_event_id = ( + SELECT id FROM planned_event WHERE trip_details_id = ? + ) + AND cox_id = ?", + trip_details_id, + user_id + ) + .fetch_one(db) + .await + .unwrap(); + if is_cox.amount > 0 { + return Err(UserTripError::AlreadyRegisteredAsCox); + } + match sqlx::query!( "INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)", user_id,