diff --git a/README.md b/README.md index 4e76e0d..40ee492 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # Notes / Bugfixes - [ ] Allow cox to edit own trip - - [ ] Nobody has registered yet -> deletable + - [x] Nobody has registered yet -> deletable - [ ] Change max_people (to be settable to 0) - [ ] Change note diff --git a/src/model/trip.rs b/src/model/trip.rs index 9df8756..4b26e75 100644 --- a/src/model/trip.rs +++ b/src/model/trip.rs @@ -50,7 +50,7 @@ WHERE day=? ret } - async fn get_all_rower_for_id(db: &SqlitePool, id: i64) -> Vec { + async fn get_all_rower_for_id(db: &SqlitePool, trip_id: i64) -> Vec { 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, +} diff --git a/src/rest/cox.rs b/src/rest/cox.rs index 4e5d206..68b986b 100644 --- a/src/rest/cox.rs +++ b/src/rest/cox.rs @@ -7,7 +7,7 @@ use rocket::{ use sqlx::SqlitePool; use crate::model::{ - trip::{CoxHelpError, Trip}, + trip::{CoxHelpError, Trip, TripDeleteError}, tripdetails::TripDetails, user::CoxUser, }; @@ -53,15 +53,27 @@ async fn join(db: &State, planned_event_id: i64, cox: CoxUser) -> Fl } } +#[get("/remove/trip/")] +async fn remove_trip(db: &State, trip_id: i64, cox: CoxUser) -> Flash { + match Trip::delete(db, cox.id, trip_id).await { + Ok(_) => Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!"), + Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error( + Redirect::to("/"), + "Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!", + ), + Err(TripDeleteError::NotYourTrip) => { + Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!") + } + } +} + #[get("/remove/")] async fn remove(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { - //TODO: Check if > 2 hrs to event - - Trip::delete(db, cox.id, planned_event_id).await; + Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await; Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") } pub fn routes() -> Vec { - routes![create, join, remove] + routes![create, join, remove, remove_trip] } diff --git a/templates/index.html.tera b/templates/index.html.tera index befde40..de6092f 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -175,6 +175,12 @@ {% endfor %} {% else %} Keine Ruderer angemeldet + {% endif %} {% endif %} @@ -251,4 +257,4 @@ {# --- END Form Add Event --- #} -{% endblock content %} \ No newline at end of file +{% endblock content %}