diff --git a/src/model/trip.rs b/src/model/trip.rs index da9e0bf..9df8756 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -65,6 +65,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i .unwrap() //TODO: fixme } + /// Cox decides to create own trip. pub async fn new_own(db: &SqlitePool, cox_id: i64, trip_details_id: i64) { sqlx::query!( "INSERT INTO trip (cox_id, trip_details_id) VALUES(?, ?)", @@ -76,17 +77,39 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i .unwrap(); //TODO: fixme } - /// Returns true if successfully inserted; false if not (e.g. because user is already - /// participant - pub async fn new_join(db: &SqlitePool, cox_id: i64, planned_event_id: i64) -> bool { - sqlx::query!( + /// Cox decides to help in a planned event. + pub async fn new_join( + db: &SqlitePool, + cox_id: i64, + planned_event_id: i64, + ) -> Result<(), CoxHelpError> { + let is_rower = sqlx::query!( + "SELECT count(*) as amount + FROM user_trip + WHERE trip_details_id = + (SELECT trip_details_id FROM planned_event WHERE id = ?) + AND user_id = ?", + planned_event_id, + cox_id + ) + .fetch_one(db) + .await + .unwrap(); + if is_rower.amount > 0 { + return Err(CoxHelpError::AlreadyRegisteredAsRower); + } + + match sqlx::query!( "INSERT INTO trip (cox_id, planned_event_id) VALUES(?, ?)", cox_id, planned_event_id ) .execute(db) .await - .is_ok() + { + Ok(_) => Ok(()), + Err(_) => Err(CoxHelpError::AlreadyRegisteredAsCox), + } } pub async fn delete(db: &SqlitePool, user_id: i64, planned_event_id: i64) { @@ -99,11 +122,9 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i .await .is_ok(); } - - //pub async fn delete(db: &SqlitePool, id: i64) { - // sqlx::query!("DELETE FROM planned_event WHERE id = ?", id) - // .execute(db) - // .await - // .unwrap(); //TODO: fixme - //} +} + +pub enum CoxHelpError { + AlreadyRegisteredAsRower, + AlreadyRegisteredAsCox, } diff --git a/src/rest/cox.rs b/src/rest/cox.rs index 8054cd6..4e5d206 100644 --- a/src/rest/cox.rs +++ b/src/rest/cox.rs @@ -6,7 +6,11 @@ use rocket::{ }; use sqlx::SqlitePool; -use crate::model::{trip::Trip, tripdetails::TripDetails, user::CoxUser}; +use crate::model::{ + trip::{CoxHelpError, Trip}, + tripdetails::TripDetails, + user::CoxUser, +}; //TODO: add constraints (e.g. planned_amount_cox > 0) #[derive(FromForm)] @@ -37,10 +41,15 @@ async fn create(db: &State, data: Form, cox: CoxUser) - #[get("/join/")] async fn join(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { - if Trip::new_join(db, cox.id, planned_event_id).await { - Flash::success(Redirect::to("/"), "Danke für's helfen!") - } else { - Flash::error(Redirect::to("/"), "Du nimmst bereits teil!") + match Trip::new_join(db, cox.id, planned_event_id).await { + Ok(_) => Flash::success(Redirect::to("/"), "Danke für's helfen!"), + Err(CoxHelpError::AlreadyRegisteredAsCox) => { + Flash::error(Redirect::to("/"), "Du hilfst bereits aus!") + } + Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error( + Redirect::to("/"), + "Du hast dich bereits als Ruderer angemeldet!", + ), } }