don't allow cox to help if cox is already registered as rower for event
This commit is contained in:
		| @@ -65,6 +65,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i | |||||||
|         .unwrap() //TODO: fixme |         .unwrap() //TODO: fixme | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /// Cox decides to create own trip. | ||||||
|     pub async fn new_own(db: &SqlitePool, cox_id: i64, trip_details_id: i64) { |     pub async fn new_own(db: &SqlitePool, cox_id: i64, trip_details_id: i64) { | ||||||
|         sqlx::query!( |         sqlx::query!( | ||||||
|             "INSERT INTO trip (cox_id, trip_details_id) VALUES(?, ?)", |             "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 |         .unwrap(); //TODO: fixme | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Returns true if successfully inserted; false if not (e.g. because user is already |     /// Cox decides to help in a planned event. | ||||||
|     /// participant |     pub async fn new_join( | ||||||
|     pub async fn new_join(db: &SqlitePool, cox_id: i64, planned_event_id: i64) -> bool { |         db: &SqlitePool, | ||||||
|         sqlx::query!( |         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(?, ?)", |             "INSERT INTO trip (cox_id, planned_event_id) VALUES(?, ?)", | ||||||
|             cox_id, |             cox_id, | ||||||
|             planned_event_id |             planned_event_id | ||||||
|         ) |         ) | ||||||
|         .execute(db) |         .execute(db) | ||||||
|         .await |         .await | ||||||
|         .is_ok() |         { | ||||||
|  |             Ok(_) => Ok(()), | ||||||
|  |             Err(_) => Err(CoxHelpError::AlreadyRegisteredAsCox), | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub async fn delete(db: &SqlitePool, user_id: i64, planned_event_id: i64) { |     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 |         .await | ||||||
|         .is_ok(); |         .is_ok(); | ||||||
|     } |     } | ||||||
|  | } | ||||||
|     //pub async fn delete(db: &SqlitePool, id: i64) { |  | ||||||
|     //    sqlx::query!("DELETE FROM planned_event WHERE id = ?", id) | pub enum CoxHelpError { | ||||||
|     //        .execute(db) |     AlreadyRegisteredAsRower, | ||||||
|     //        .await |     AlreadyRegisteredAsCox, | ||||||
|     //        .unwrap(); //TODO: fixme |  | ||||||
|     //} |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,7 +6,11 @@ use rocket::{ | |||||||
| }; | }; | ||||||
| use sqlx::SqlitePool; | 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) | //TODO: add constraints (e.g. planned_amount_cox > 0) | ||||||
| #[derive(FromForm)] | #[derive(FromForm)] | ||||||
| @@ -37,10 +41,15 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) - | |||||||
|  |  | ||||||
| #[get("/join/<planned_event_id>")] | #[get("/join/<planned_event_id>")] | ||||||
| async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> { | async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> { | ||||||
|     if Trip::new_join(db, cox.id, planned_event_id).await { |     match Trip::new_join(db, cox.id, planned_event_id).await { | ||||||
|         Flash::success(Redirect::to("/"), "Danke für's helfen!") |         Ok(_) => Flash::success(Redirect::to("/"), "Danke für's helfen!"), | ||||||
|     } else { |         Err(CoxHelpError::AlreadyRegisteredAsCox) => { | ||||||
|         Flash::error(Redirect::to("/"), "Du nimmst bereits teil!") |             Flash::error(Redirect::to("/"), "Du hilfst bereits aus!") | ||||||
|  |         } | ||||||
|  |         Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error( | ||||||
|  |             Redirect::to("/"), | ||||||
|  |             "Du hast dich bereits als Ruderer angemeldet!", | ||||||
|  |         ), | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user