From 57471181f3031755a486b69ac609ba7b5640de3c Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 29 Oct 2022 17:13:12 +0200 Subject: [PATCH] Adjust routes --- src/main.rs | 1 + src/routes.rs | 35 ++++++++++++++--------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0d09e66..51d0c76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod captcha_solutions; mod config; +mod context; mod forms; mod mailer; mod routes; diff --git a/src/routes.rs b/src/routes.rs index 24b15f9..7d95c4e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -2,28 +2,18 @@ use rocket::form::{Form, Strict}; use rocket::response::status::BadRequest; use rocket::response::Redirect; use rocket::{get, post, uri, State}; -use rocket_dyn_templates::Template; -use serde::Serialize; +use rocket_dyn_templates::{context, Template}; use std::mem; use crate::captcha_solutions; use crate::config; +use crate::context; use crate::forms; use crate::mailer; -#[derive(Serialize)] -struct FormContext<'a> { - path_prefix: &'a str, - id: u16, - name: Option<&'a str>, - email: Option<&'a str>, - telefon: Option<&'a str>, - message: Option<&'a str>, - captcha: &'a str, -} - -#[get("/?&&&")] +#[get("/?&&&&")] pub fn index( + was_validated: Option, name: Option<&str>, email: Option<&str>, telefon: Option<&str>, @@ -39,17 +29,18 @@ pub fn index( let id = captcha_solutions.store_solution(&captcha.chars_as_string()); - let form_context = FormContext { - path_prefix: &config.path_prefix, + let form_context = context::FormContext::new( + &config.path_prefix, + was_validated, id, name, email, telefon, message, - captcha: &captcha_base64, - }; + &captcha_base64, + ); - Ok(Template::render("form", &form_context)) + Ok(Template::render("form", form_context)) } #[post("/submit", data = "
")] @@ -70,6 +61,7 @@ pub fn submit( return Redirect::to( path_prefix + &uri!(index( + Some(true), Some(&name), Some(&email), Some(&telefon), @@ -85,6 +77,7 @@ pub fn submit( return Redirect::to( path_prefix + &uri!(index( + Some(true), Some(&name), Some(&email), Some(&telefon), @@ -99,6 +92,6 @@ pub fn submit( } #[get("/success")] -pub fn success() -> &'static str { - "Ihre Anfrage wurde übermittelt! Wir melden uns bald bei Ihnen zurück." +pub fn success() -> Template { + Template::render("success", context! {}) }