allow trip to be deleted if noone has registered yet

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

View File

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

View File

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

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]
}

View File

@ -179,6 +179,7 @@
</div>
{% endif %}
</div>
<div><a href="/cox/remove/trip/{{ trip.id }}">LÖSCHEN</a></div>
</div>
<div class="sidebar-overlay" data-trigger="detailTrip{{ trip.trip_details_id }}"></div>
{% endfor %}
@ -259,4 +260,4 @@
{% endfor %}
</div>
</div>
{% endblock content %}
{% endblock content %}