allow trip to be deleted if noone has registered yet
This commit is contained in:
@ -50,7 +50,7 @@ WHERE day=?
|
||||
ret
|
||||
}
|
||||
|
||||
async fn get_all_rower_for_id(db: &SqlitePool, id: i64) -> Vec<Registration> {
|
||||
async fn get_all_rower_for_id(db: &SqlitePool, trip_id: i64) -> Vec<Registration> {
|
||||
sqlx::query_as!(
|
||||
Registration,
|
||||
"
|
||||
@ -58,7 +58,7 @@ SELECT
|
||||
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE id = ?)",
|
||||
id
|
||||
trip_id
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
@ -112,7 +112,7 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, user_id: i64, planned_event_id: i64) {
|
||||
pub async fn delete_by_planned_event_id(db: &SqlitePool, user_id: i64, planned_event_id: i64) {
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM trip WHERE cox_id = ? AND planned_event_id = ?",
|
||||
user_id,
|
||||
@ -122,9 +122,44 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE i
|
||||
.await
|
||||
.is_ok();
|
||||
}
|
||||
|
||||
pub(crate) async fn delete(
|
||||
db: &SqlitePool,
|
||||
user_id: i64,
|
||||
trip_id: i64,
|
||||
) -> Result<(), TripDeleteError> {
|
||||
let registered_rower = Self::get_all_rower_for_id(db, trip_id).await;
|
||||
if registered_rower.len() > 0 {
|
||||
return Err(TripDeleteError::SomebodyAlreadyRegistered);
|
||||
}
|
||||
|
||||
let trip_cox = sqlx::query!("SELECT cox_id FROM trip WHERE id = ?", trip_id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
if trip_cox.cox_id != user_id {
|
||||
return Err(TripDeleteError::NotYourTrip);
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
"DELETE FROM trip WHERE cox_id = ? AND id = ?",
|
||||
user_id,
|
||||
trip_id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap(); //TODO: fixme
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum CoxHelpError {
|
||||
AlreadyRegisteredAsRower,
|
||||
AlreadyRegisteredAsCox,
|
||||
}
|
||||
|
||||
pub enum TripDeleteError {
|
||||
SomebodyAlreadyRegistered,
|
||||
NotYourTrip,
|
||||
}
|
||||
|
Reference in New Issue
Block a user