From edf59251d02b52822f65a7f089cc3eda8327a2ad Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 16 Feb 2023 13:47:51 +0100 Subject: [PATCH] allow deleting registrations --- db.sqlite | Bin 57344 -> 57344 bytes src/rest/restreg.rs | 55 +++++++++++++++++++++++++++++++++++--- templates/index.html.tera | 14 +++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/db.sqlite b/db.sqlite index 3f773596d694802be950458dbc00cf49cbbb968e..39cc64db479e6dea36e3ce9eb77d0cc1a0166cc1 100644 GIT binary patch delta 93 zcmZoTz}#?vd4e?Kx`{H*jO#Wgtf^<>@82w_(88~z%E`cBY-C_$tZQJTYiNdGDHs}A i8JSp_T29{5AHl?bf3sk~5q@3)MuZCH&G+XkIsgFRxfz!L delta 42 ycmZoTz}#?vd4e?Knu#*bjB7R~tf}8D7_fqW@|OMxAmcLs Vec { - routes![register] +#[derive(FromForm)] +struct DeleteForm { + day: NaiveDateForm, + user: i32, +} + +#[delete("/", data = "")] +async fn delete( + db: &State, + delete: Form, + user: user::Model, +) -> Flash { + 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 { + routes![register, delete] } diff --git a/templates/index.html.tera b/templates/index.html.tera index 779e533..f75779f 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -24,7 +24,19 @@
    {% for r in rowers %} -
  1. {{ r.user.name }} (angemeldet seit {{ r.trip.created }})
  2. +
  3. + {{ r.user.name }} (angemeldet seit {{ r.trip.created }}) + {% if r.user.name == user.name %} +
    + + + + + + +
    + {% endif %} +
  4. {% endfor %}