From 8852e1cdd6e4be862817bd9c6083a593dd4d6155 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 4 Oct 2023 13:56:58 +0200 Subject: [PATCH] allow boatdamage add in kiosk mode --- README.md | 1 - src/tera/boatdamage.rs | 75 ++++++++++++++++++++++++++++++--- templates/boatdamages.html.tera | 3 ++ 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f5b7f78..841f3b6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ - [] reload page -> don't throw input away! ## Backend -- [] variable for kiosk mode (needed especially for header) - [] boat_damage -> Add user_select # Nice to have diff --git a/src/tera/boatdamage.rs b/src/tera/boatdamage.rs index be42d96..1fb28b2 100644 --- a/src/tera/boatdamage.rs +++ b/src/tera/boatdamage.rs @@ -9,13 +9,38 @@ use rocket_dyn_templates::Template; use sqlx::SqlitePool; use tera::Context; -use crate::model::{ - boat::Boat, - boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified}, - user::{CoxUser, TechUser, User}, +use crate::{ + model::{ + boat::Boat, + boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified}, + user::{CoxUser, TechUser, User}, + }, + tera::log::KioskCookie, }; #[get("/")] +async fn index_kiosk( + db: &State, + flash: Option>, + _kiosk: KioskCookie, +) -> Template { + let boatdamages = BoatDamage::all(db).await; + let boats = Boat::all(db).await; + let coxes = User::cox(db).await; + + let mut context = Context::new(); + if let Some(msg) = flash { + context.insert("flash", &msg.into_inner()); + } + + context.insert("boatdamages", &boatdamages); + context.insert("boats", &boats); + context.insert("coxes", &coxes); + + Template::render("boatdamages", context.into_json()) +} + +#[get("/", rank = 2)] async fn index(db: &State, flash: Option>, user: User) -> Template { let boatdamages = BoatDamage::all(db).await; let boats = Boat::all(db).await; @@ -39,7 +64,7 @@ pub struct FormBoatDamageToAdd<'r> { pub lock_boat: bool, } -#[post("/", data = "")] +#[post("/", data = "", rank = 2)] async fn create<'r>( db: &State, data: Form>, @@ -54,7 +79,36 @@ async fn create<'r>( match BoatDamage::create(db, boatdamage_to_add).await { Ok(_) => Flash::success( Redirect::to("/boatdamage"), - "Ausfahrt erfolgreich hinzugefügt", + "Bootsschaden erfolgreich hinzugefügt", + ), + Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Fehler: {e}")), + } +} + +#[derive(FromForm)] +pub struct FormBoatDamageToAddKiosk<'r> { + pub boat_id: i64, + pub desc: &'r str, + pub lock_boat: bool, + pub user_id: i32, +} + +#[post("/", data = "")] +async fn create_from_kiosk<'r>( + db: &State, + data: Form>, + _kiosk: KioskCookie, +) -> Flash { + let boatdamage_to_add = BoatDamageToAdd { + boat_id: data.boat_id, + desc: data.desc, + lock_boat: data.lock_boat, + user_id_created: data.user_id, + }; + match BoatDamage::create(db, boatdamage_to_add).await { + Ok(_) => Flash::success( + Redirect::to("/boatdamage"), + "Bootsschaden erfolgreich hinzugefügt", ), Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Fehler: {e}")), } @@ -110,5 +164,12 @@ async fn verified<'r>( } pub fn routes() -> Vec { - routes![index, create, fixed, verified] + routes![ + index, + index_kiosk, + create, + fixed, + verified, + create_from_kiosk + ] } diff --git a/templates/boatdamages.html.tera b/templates/boatdamages.html.tera index c4d5f2c..4fddd12 100644 --- a/templates/boatdamages.html.tera +++ b/templates/boatdamages.html.tera @@ -26,6 +26,9 @@
{{ log::boat_select(only_ones=false, id='boat') }} + {% if not loggedin_user %} + {{ macros::select(label='Gemeldet von', data=coxes, name='user_id') }} + {% endif %} {{ macros::input(label='Beschreibung des Schadens', name='desc', type='text', required=true, wrapper_class='col-span-4') }}
{{ macros::checkbox(label='Boot sperren', name='lock_boat', type='text', required=true) }}