allow deleting registrations

This commit is contained in:
philipp 2023-02-16 13:47:51 +01:00
parent 306ae13467
commit edf59251d0
3 changed files with 65 additions and 4 deletions

BIN
db.sqlite

Binary file not shown.

View File

@ -1,4 +1,8 @@
use rocket::{form::Form, response::Redirect, Route, State}; use rocket::{
form::Form,
response::{Flash, Redirect},
Route, State,
};
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set}; use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
use crate::models::{day, trip, user}; use crate::models::{day, trip, user};
@ -42,6 +46,51 @@ async fn register(
Redirect::to("/") Redirect::to("/")
} }
pub fn routes() -> Vec<Route> { #[derive(FromForm)]
routes![register] struct DeleteForm {
day: NaiveDateForm,
user: i32,
}
#[delete("/", data = "<delete>")]
async fn delete(
db: &State<DatabaseConnection>,
delete: Form<DeleteForm>,
user: user::Model,
) -> Flash<Redirect> {
let day = day::Entity::find_by_id(*delete.day)
.one(db.inner())
.await
.unwrap()
.expect("There's no trip on this date (yet)");
if delete.user != user.id {
return Flash::error(
Redirect::to("/"),
"Du kannst nur deine eigenen Anmeldungen löschen!",
);
}
let trip = trip::Entity::find_by_id((format!("{}", day.day.format("%Y-%m-%d")), user.id))
.one(db.inner())
.await
.unwrap();
match trip {
None => return Flash::error(Redirect::to("/"), "Du bist gar nicht angemeldet!"),
Some(trip) => {
trip::Entity::delete(trip::ActiveModel {
day: Set(trip.day),
user_id: Set(trip.user_id),
..Default::default()
})
.exec(db.inner())
.await
.unwrap();
}
}
Flash::success(Redirect::to("/"), "Anmeldung erfolgreich gelöscht")
}
pub fn routes() -> Vec<Route> {
routes![register, delete]
} }

View File

@ -24,7 +24,19 @@
<ol> <ol>
{% for r in rowers %} {% for r in rowers %}
<li>{{ r.user.name }} (angemeldet seit {{ r.trip.created }})</li> <li>
{{ r.user.name }} (angemeldet seit {{ r.trip.created }})
{% if r.user.name == user.name %}
<form method="post" action="/register">
<input type="hidden" name="_method" value="delete" />
<input type="hidden" name="day" value="{{ day.day}}" />
<input type="hidden" name="user" value="{{ r.user.id}}" />
<input type="submit" value="LÖSCHEN" />
</form>
{% endif %}
</li>
{% endfor %} {% endfor %}
</ol> </ol>