From bb142a0969073870676ecf2ee0b936d524713aee Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 2 Oct 2023 12:39:05 +0200 Subject: [PATCH 1/8] clean code with clippy --- src/model/boat.rs | 11 +++++------ src/model/logbook.rs | 4 ++-- src/tera/log.rs | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index 6fbd59a..5a12b0e 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -148,8 +148,7 @@ ORDER BY amount_seats DESC if user.is_admin { return Self::all(db).await; } - let boats; - if user.is_cox { + let boats = if user.is_cox { boats = sqlx::query_as!( Boat, " @@ -162,9 +161,9 @@ ORDER BY amount_seats DESC ) .fetch_all(db) .await - .unwrap(); //TODO: fixme + .unwrap() //TODO: fixme } else { - boats = sqlx::query_as!( + sqlx::query_as!( Boat, " SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external @@ -176,8 +175,8 @@ ORDER BY amount_seats DESC ) .fetch_all(db) .await - .unwrap(); //TODO: fixme - } + .unwrap() //TODO: fixme + }; Self::boats_to_details(db, boats).await } diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 735aecc..adca6f0 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -1,4 +1,4 @@ -use chrono::{Local, NaiveDateTime, TimeZone}; +use chrono::{NaiveDateTime, TimeZone}; use chrono_tz::Europe::Vienna; use rocket::FromForm; use serde::Serialize; @@ -196,7 +196,7 @@ ORDER BY departure DESC if let Some(arrival) = &log.arrival { let dep = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap(); - let arr = NaiveDateTime::parse_from_str(&arrival, "%Y-%m-%dT%H:%M").unwrap(); + let arr = NaiveDateTime::parse_from_str(arrival, "%Y-%m-%dT%H:%M").unwrap(); if arr <= dep { return Err(LogbookCreateError::ArrivalNotAfterDeparture); } diff --git a/src/tera/log.rs b/src/tera/log.rs index 0e4c11d..738a26f 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -148,18 +148,18 @@ async fn create_logbook(db: &SqlitePool, data: Form, user: &User) -> F .await { Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"), - Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Boot schon am Wasser")), - Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")), + Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log"), "Boot schon am Wasser"), + Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), "Schiffsführer schon am Wasser"), Err(LogbookCreateError::RowerAlreadyOnWater(rower)) => Flash::error(Redirect::to("/log"), format!("Ruderer {} schon am Wasser", rower.name)), - Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log"), format!("Boot gesperrt")), - Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), format!("Boot gibt's ned")), + Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log"),"Boot gesperrt"), + Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), "Boot gibt's ned"), Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")), Err(LogbookCreateError::RowerCreateError(rower, e)) => Flash::error(Redirect::to("/log"), format!("Fehler bei Ruderer {rower}: {e}")), - Err(LogbookCreateError::SamePersonShipmasterAndRower) => Flash::error(Redirect::to("/log"), format!("Selbe Person als Schiffsführer und Ruderer ausgewählt")), - Err(LogbookCreateError::ArrivalSetButNoDistance) => Flash::error(Redirect::to("/log"), format!("Distanz notwendig, wenn Ankunftszeit angegeben wurde")), - Err(LogbookCreateError::ArrivalSetButNoDestination) => Flash::error(Redirect::to("/log"), format!("Ziel notwendig, wenn Ankunftszeit angegeben wurde")), - Err(LogbookCreateError::ArrivalNotAfterDeparture) => Flash::error(Redirect::to("/log"), format!("Ankunftszeit kann nicht vor der Abfahrtszeit sein")), - Err(LogbookCreateError::UserNotAllowedToUseBoat) => Flash::error(Redirect::to("/log"), format!("Schiffsführer darf dieses Boot nicht verwenden")), + Err(LogbookCreateError::SamePersonShipmasterAndRower) => Flash::error(Redirect::to("/log"), "Selbe Person als Schiffsführer und Ruderer ausgewählt"), + Err(LogbookCreateError::ArrivalSetButNoDistance) => Flash::error(Redirect::to("/log"), "Distanz notwendig, wenn Ankunftszeit angegeben wurde"), + Err(LogbookCreateError::ArrivalSetButNoDestination) => Flash::error(Redirect::to("/log"), "Ziel notwendig, wenn Ankunftszeit angegeben wurde"), + Err(LogbookCreateError::ArrivalNotAfterDeparture) => Flash::error(Redirect::to("/log"), "Ankunftszeit kann nicht vor der Abfahrtszeit sein"), + Err(LogbookCreateError::UserNotAllowedToUseBoat) => Flash::error(Redirect::to("/log"), "Schiffsführer darf dieses Boot nicht verwenden"), } } @@ -196,10 +196,10 @@ async fn home_logbook( match logbook.home(db, user, data.into_inner()).await { Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"), Err(LogbookUpdateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")), - Err(a) => {println!("{a:?}"); return Flash::error( + Err(_) => Flash::error( Redirect::to("/log"), format!("Logbook with ID {} could not be updated!", logbook_id), - )}, + ), } } From 88833b9f4bc6618e589ff8bc2694bcf44c6f0503 Mon Sep 17 00:00:00 2001 From: philipp Date: Mon, 2 Oct 2023 12:40:20 +0200 Subject: [PATCH 2/8] fix ci --- src/model/boat.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model/boat.rs b/src/model/boat.rs index 5a12b0e..696038e 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -149,7 +149,7 @@ ORDER BY amount_seats DESC return Self::all(db).await; } let boats = if user.is_cox { - boats = sqlx::query_as!( + sqlx::query_as!( Boat, " SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external From e86d84d34c5874ddc61fd9d1843d4d5bdd6672e8 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 4 Oct 2023 13:39:09 +0200 Subject: [PATCH 3/8] upgrade deps --- Cargo.lock | 115 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9607f3b..0f396fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,6 +164,15 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c59bdb34bc650a32731b31bd8f0829cc15d24a708ee31559e0bb34f2bc320cba" +[[package]] +name = "atomic" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d818003e740b63afc82337e3160717f4f63078720a810b7b903e70a5d1d2994" +dependencies = [ + "bytemuck", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -249,6 +258,12 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "bytemuck" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" + [[package]] name = "byteorder" version = "1.4.3" @@ -518,9 +533,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ "errno-dragonfly", "libc", @@ -545,17 +560,17 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "figment" -version = "0.10.10" +version = "0.10.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4547e226f4c9ab860571e070a9034192b3175580ecea38da34fcdb53a018c9a5" +checksum = "a014ac935975a70ad13a3bff2463b1c1b083b35ae4cb6309cfc59476aa7a181f" dependencies = [ - "atomic", + "atomic 0.6.0", "pear", "serde", "toml", @@ -824,9 +839,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" dependencies = [ "ahash 0.8.3", "allocator-api2", @@ -838,7 +853,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" dependencies = [ - "hashbrown 0.14.0", + "hashbrown 0.14.1", ] [[package]] @@ -1022,12 +1037,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.0" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", - "hashbrown 0.14.0", + "hashbrown 0.14.1", ] [[package]] @@ -1160,9 +1175,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" +checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db" [[package]] name = "lock_api" @@ -1206,9 +1221,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.3" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "mime" @@ -1461,9 +1476,9 @@ checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "pest" -version = "2.7.3" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7a4d085fd991ac8d5b05a147b437791b4260b76326baf0fc60cf7c9c27ecd33" +checksum = "c022f1e7b65d6a24c0dbbd5fb344c66881bc01f3e5ae74a1c8100f2f985d98a4" dependencies = [ "memchr", "thiserror", @@ -1472,9 +1487,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.3" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bee7be22ce7918f641a33f08e3f43388c7656772244e2bbb2477f44cc9021a" +checksum = "35513f630d46400a977c4cb58f78e1bfbe01434316e60c37d27b9ad6139c66d8" dependencies = [ "pest", "pest_generator", @@ -1482,9 +1497,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.3" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1511785c5e98d79a05e8a6bc34b4ac2168a0e3e92161862030ad84daa223141" +checksum = "bc9fc1b9e7057baba189b5c626e2d6f40681ae5b6eb064dc7c7834101ec8123a" dependencies = [ "pest", "pest_meta", @@ -1495,9 +1510,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.7.3" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42f0394d3123e33353ca5e1e89092e533d2cc490389f2bd6131c43c634ebc5f" +checksum = "1df74e9e7ec4053ceb980e7c0c8bd3594e977fde1af91daba9c928e8e8c6708d" dependencies = [ "once_cell", "pest", @@ -1705,13 +1720,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.5" +version = "1.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.3.8", + "regex-automata 0.3.9", "regex-syntax 0.7.5", ] @@ -1726,9 +1741,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" dependencies = [ "aho-corasick", "memchr", @@ -1770,7 +1785,7 @@ checksum = "58734f7401ae5cfd129685b48f61182331745b357b96f2367f01aebaf1cc9cc9" dependencies = [ "async-stream", "async-trait", - "atomic", + "atomic 0.5.3", "binascii", "bytes", "either", @@ -1883,9 +1898,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.14" +version = "0.38.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747c788e9ce8e92b12cd485c49ddf90723550b654b32508f979b71a7b1ecda4f" +checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" dependencies = [ "bitflags 2.4.0", "errno", @@ -2000,9 +2015,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.10.7" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if", "cpufeatures", @@ -2011,9 +2026,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.4" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" dependencies = [ "lazy_static", ] @@ -2294,18 +2309,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.48" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", @@ -2432,9 +2447,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.8" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" dependencies = [ "serde", "serde_spanned", @@ -2453,11 +2468,11 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.19.15" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.2", "serde", "serde_spanned", "toml_datetime", @@ -2546,9 +2561,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "ubyte" -version = "0.10.3" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c81f0dae7d286ad0d9366d7679a77934cfc3cf3a8d67e82669794412b2368fe6" +checksum = "f720def6ce1ee2fc44d40ac9ed6d3a59c361c80a75a7aa8e75bb9baed31cf2ea" dependencies = [ "serde", ] @@ -2794,9 +2809,9 @@ dependencies = [ [[package]] name = "webpki" -version = "0.22.1" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0e74f82d49d545ad128049b7e88f6576df2da6b02e9ce565c6f533be576957e" +checksum = "07ecc0cd7cac091bf682ec5efa18b1cff79d617b84181f38b3951dbe135f607f" dependencies = [ "ring", "untrusted", From 8852e1cdd6e4be862817bd9c6083a593dd4d6155 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 4 Oct 2023 13:56:58 +0200 Subject: [PATCH 4/8] 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) }} From 5539f033f16695cccfa065662a5cc52fa992a21d Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 4 Oct 2023 13:59:51 +0200 Subject: [PATCH 5/8] remove todo :-) --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 841f3b6..0a121d7 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ - [] reload page -> don't throw input away! ## Backend -- [] boat_damage -> Add user_select # Nice to have ## Frontend From c47b3bb44724beabeeb9ff68903cd5b14fbde375 Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 5 Oct 2023 08:46:34 +0200 Subject: [PATCH 6/8] log if new kiosk cookie is set --- src/tera/log.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/tera/log.rs b/src/tera/log.rs index 738a26f..bb11581 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -1,3 +1,5 @@ +use std::net::IpAddr; + use rocket::{ form::Form, get, @@ -15,6 +17,7 @@ use tera::Context; use crate::model::{ boat::Boat, + log::Log, logbook::{ LogToAdd, LogToFinalize, Logbook, LogbookCreateError, LogbookDeleteError, LogbookUpdateError, @@ -91,7 +94,17 @@ async fn show_kiosk(db: &State, _kiosk: KioskCookie) -> Template { } #[get("/kiosk/ekrv2019/")] -fn new_kiosk(cookies: &CookieJar<'_>, loc: String) -> Redirect { +async fn new_kiosk( + db: &State, + cookies: &CookieJar<'_>, + loc: String, + ip: IpAddr, +) -> Redirect { + Log::create( + db, + format!("New kiosk cookie set for loc '{loc}' (IP={ip:?})"), + ) + .await; let mut cookie = Cookie::new("kiosk", loc); cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12)); cookies.add_private(cookie); From 1386ba05c272401bcc724904f4003c6c3dec7ccf Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 5 Oct 2023 08:59:13 +0200 Subject: [PATCH 7/8] log important events --- src/model/boatdamage.rs | 23 ++++++++++++++++++++--- src/model/logbook.rs | 6 +++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/model/boatdamage.rs b/src/model/boatdamage.rs index 7dbabfb..b50b754 100644 --- a/src/model/boatdamage.rs +++ b/src/model/boatdamage.rs @@ -4,6 +4,8 @@ use rocket::serde::{Deserialize, Serialize}; use rocket::FromForm; use sqlx::{FromRow, SqlitePool}; +use super::log::Log; + #[derive(FromRow, Debug, Serialize, Deserialize)] pub struct BoatDamage { pub id: i64, @@ -28,6 +30,7 @@ pub struct BoatDamageWithDetails { boat: Boat, } +#[derive(Debug)] pub struct BoatDamageToAdd<'r> { pub boat_id: i64, pub desc: &'r str, @@ -35,13 +38,13 @@ pub struct BoatDamageToAdd<'r> { pub lock_boat: bool, } -#[derive(FromForm)] +#[derive(FromForm, Debug)] pub struct BoatDamageFixed<'r> { pub desc: &'r str, pub user_id_fixed: i32, } -#[derive(FromForm)] +#[derive(FromForm, Debug)] pub struct BoatDamageVerified<'r> { pub desc: &'r str, pub user_id_verified: i32, @@ -107,6 +110,8 @@ ORDER BY created_at DESC } pub async fn create(db: &SqlitePool, boatdamage: BoatDamageToAdd<'_>) -> Result<(), String> { + Log::create(db, format!("New boat damage: {boatdamage:?}")).await; + sqlx::query!( "INSERT INTO boat_damage(boat_id, desc, user_id_created, lock_boat) VALUES (?,?,?, ?)", boatdamage.boat_id, @@ -121,6 +126,8 @@ ORDER BY created_at DESC } pub async fn fixed(&self, db: &SqlitePool, boat: BoatDamageFixed<'_>) -> Result<(), String> { + Log::create(db, format!("Fixed boat damage: {boat:?}")).await; + sqlx::query!( "UPDATE boat_damage SET desc=?, user_id_fixed=?, fixed_at=CURRENT_TIMESTAMP WHERE id=?", boat.desc, @@ -152,7 +159,17 @@ ORDER BY created_at DESC db: &SqlitePool, boat: BoatDamageVerified<'_>, ) -> Result<(), String> { - //TODO: Check if user is allowed to verify + if let Some(verifier) = User::find_by_id(db, boat.user_id_verified).await { + if !verifier.is_tech { + Log::create(db, format!("User {verifier:?} tried to verify boat {boat:?}. The user is no tech. Manually craftted request?")).await; + return Err("You are not allowed to verify the boat!".into()); + } + } else { + Log::create(db, format!("Someone tried to verify the boat {boat:?} with user_id={} which does not exist. Manually craftted request?", boat.user_id_verified)).await; + return Err("Could not find user".into()); + } + + Log::create(db, format!("Verified boat damage: {boat:?}")).await; sqlx::query!( "UPDATE boat_damage SET desc=?, user_id_verified=?, verified_at=CURRENT_TIMESTAMP WHERE id=?", diff --git a/src/model/logbook.rs b/src/model/logbook.rs index adca6f0..4289231 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -4,7 +4,7 @@ use rocket::FromForm; use serde::Serialize; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; -use super::{boat::Boat, rower::Rower, user::User}; +use super::{boat::Boat, log::Log, rower::Rower, user::User}; #[derive(FromRow, Serialize, Clone, Debug)] pub struct Logbook { @@ -232,6 +232,7 @@ ORDER BY departure DESC } //let departure = format!("{}+02:00", &log.departure); + Log::create(db, format!("New trip started: {log:?}")).await; let mut tx = db.begin().await.unwrap(); @@ -339,6 +340,7 @@ ORDER BY departure DESC return Err(LogbookUpdateError::ArrivalNotAfterDeparture); } + Log::create(db, format!("New trip: {log:?}")).await; let mut tx = db.begin().await.unwrap(); sqlx::query!( @@ -367,6 +369,8 @@ ORDER BY departure DESC } pub async fn delete(&self, db: &SqlitePool, user: &User) -> Result<(), LogbookDeleteError> { + Log::create(db, format!("{user:?} deleted trip: {self:?}")).await; + if user.is_admin || user.id == self.shipmaster { sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) .execute(db) From c323b209d04b3b3608a3494edb84fb1d9b9ab8c9 Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 5 Oct 2023 09:01:55 +0200 Subject: [PATCH 8/8] fix ci --- src/tera/log.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tera/log.rs b/src/tera/log.rs index bb11581..de65e73 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -98,7 +98,7 @@ async fn new_kiosk( db: &State, cookies: &CookieJar<'_>, loc: String, - ip: IpAddr, + ip: Option, ) -> Redirect { Log::create( db,