From 6ea1585d7c8b1b82850e4514c12e9245933d5275 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 4 Mar 2023 12:10:44 +0100 Subject: [PATCH] proper error message if already registered --- src/rest/restreg.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/rest/restreg.rs b/src/rest/restreg.rs index d186026..ee36aa2 100644 --- a/src/rest/restreg.rs +++ b/src/rest/restreg.rs @@ -21,7 +21,7 @@ async fn register( db: &State, register: Form, user: user::Model, -) -> Redirect { +) -> Flash { let day = day::Entity::find_by_id(*register.day) .one(db.inner()) .await @@ -30,22 +30,35 @@ async fn register( if !(day.open_registration || user.is_cox) { log::error!("{} tried to register, even though the user it should not be possible to do so via UI -> manually crafted request?", user.name); - return Redirect::to("/"); + return Flash::error( + Redirect::to("/"), + "Don't (try to ;)) abuse this system! Incident has been reported...", + ); } let user = user::Model::find_or_create_user(®ister.name, db.inner()).await; let day = format!("{}", day.day.format("%Y-%m-%d")); let trip = trip::ActiveModel { - day: Set(day), + day: Set(day.clone()), user_id: Set(user.id), ..Default::default() }; - log::info!("{} registered for {:?}", user.name, trip); - trip.insert(db.inner()).await.unwrap(); - - Redirect::to("/") + match trip.insert(db.inner()).await { + Ok(_) => { + log::info!("{} registered for {:?}", user.name, day); + Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!") + } + Err(_) => { + log::warn!( + "{} tried to register for {:?}, but is already registered", + user.name, + day + ); + Flash::error(Redirect::to("/"), "Du bist bereits angemeldet") + } + } } #[derive(FromForm)]