From 3f9d5bb4c719a9e10683dc12acac38758223f0f7 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 7 Apr 2023 11:16:39 +0200 Subject: [PATCH 1/2] allow trip to be deleted if noone has registered yet --- README.md | 2 +- src/model/trip.rs | 41 ++++++++++++++++++++++++++++++++++++--- src/rest/cox.rs | 22 ++++++++++++++++----- templates/index.html.tera | 3 ++- 4 files changed, 58 insertions(+), 10 deletions(-) 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 a3211e7..4d3eaef 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -179,6 +179,7 @@ {% endif %} + {% endfor %} @@ -259,4 +260,4 @@ {% endfor %} -{% endblock content %} \ No newline at end of file +{% endblock content %} From eeb7af4fc10b76f6d606c6290f86c6971a189ae6 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 7 Apr 2023 11:17:25 +0200 Subject: [PATCH 2/2] only show delete_trip button when no one is registered --- templates/index.html.tera | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/index.html.tera b/templates/index.html.tera index 4d3eaef..98800e4 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -175,11 +175,11 @@ {% endfor %} {% else %} Keine Ruderer angemeldet + LÖSCHEN {% endif %} {% endif %} - {% endfor %}