proper error message if already registered

This commit is contained in:
philipp 2023-03-04 12:10:44 +01:00
parent ff78b64dfd
commit 6ea1585d7c

View File

@ -21,7 +21,7 @@ async fn register(
db: &State<DatabaseConnection>,
register: Form<RegisterForm>,
user: user::Model,
) -> Redirect {
) -> Flash<Redirect> {
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(&register.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)]