allow trip to be deleted if noone has registered yet

This commit is contained in:
2023-04-07 11:16:39 +02:00
parent 5e4df13289
commit 3f9d5bb4c7
4 changed files with 58 additions and 10 deletions

View File

@ -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<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Fl
}
}
#[get("/remove/trip/<trip_id>")]
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
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/<planned_event_id>")]
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
//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<Route> {
routes![create, join, remove]
routes![create, join, remove, remove_trip]
}