diff --git a/db.sqlite b/db.sqlite index 3f77359..39cc64d 100644 Binary files a/db.sqlite and b/db.sqlite differ diff --git a/src/rest/restreg.rs b/src/rest/restreg.rs index 434c23d..b8ed1c8 100644 --- a/src/rest/restreg.rs +++ b/src/rest/restreg.rs @@ -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 { - 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 %}