allow deleting registrations
This commit is contained in:
parent
306ae13467
commit
edf59251d0
@ -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 crate::models::{day, trip, user};
|
||||
@ -42,6 +46,51 @@ async fn register(
|
||||
Redirect::to("/")
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![register]
|
||||
#[derive(FromForm)]
|
||||
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]
|
||||
}
|
||||
|
@ -24,7 +24,19 @@
|
||||
|
||||
<ol>
|
||||
{% 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 %}
|
||||
</ol>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user