don't allow cox to help if cox is already registered as rower for event

This commit is contained in:
2023-04-05 21:49:48 +02:00
parent e2f5436790
commit 9ab1572b15
2 changed files with 47 additions and 17 deletions

View File

@ -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,
}