From b5fd4018ff34f1128699ac86983780951fbabd2f Mon Sep 17 00:00:00 2001 From: philipp Date: Thu, 27 Jul 2023 15:24:29 +0200 Subject: [PATCH] extract common functionality to functoin --- src/model/logbook.rs | 11 +++- src/tera/log.rs | 103 +++++++++++++++----------------------- templates/kiosk.html.tera | 4 ++ templates/log.html.tera | 4 ++ 4 files changed, 58 insertions(+), 64 deletions(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 449ad30..92d5025 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -63,6 +63,7 @@ pub enum LogbookCreateError { BoatNotFound, TooManyRowers(usize, usize), ShipmasterAlreadyOnWater, + RowerAlreadyOnWater(User), } impl Logbook { @@ -146,10 +147,9 @@ impl Logbook { return Err(LogbookCreateError::BoatAlreadyOnWater); } if (User::find_by_id(db, log.shipmaster as i32).await.unwrap()).on_water(db).await { - return Err(LogbookCreateError::BoatAlreadyOnWater); + return Err(LogbookCreateError::ShipmasterAlreadyOnWater); } - if log.rower.len() > boat.amount_seats as usize - 1 { return Err(LogbookCreateError::TooManyRowers( boat.amount_seats as usize, @@ -157,6 +157,13 @@ impl Logbook { )); } + for rower in &log.rower { + let user = User::find_by_id(db, *rower as i32).await.unwrap(); + if user.on_water(db).await { + return Err(LogbookCreateError::RowerAlreadyOnWater(user)); + } + } + let mut tx = db.begin().await.unwrap(); let departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap(); diff --git a/src/tera/log.rs b/src/tera/log.rs index 58df629..e5bfaf9 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -15,7 +15,7 @@ use tera::Context; use crate::model::{ boat::Boat, - logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError}, + logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError, LogbookUpdateError}, logtype::LogType, user::{AdminUser, User}, }; @@ -34,7 +34,7 @@ impl<'r> FromRequest<'r> for KioskCookie { } } -#[get("/")] +#[get("/", rank=2)] async fn index( db: &State, flash: Option>, @@ -71,10 +71,10 @@ fn new_kiosk(cookies: &CookieJar<'_>) -> Redirect { let mut cookie = Cookie::new("kiosk", format!("yes")); cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12)); cookies.add_private(cookie); - Redirect::to("/log/kiosk") + Redirect::to("/log") } -#[get("/kiosk")] +#[get("/")] async fn kiosk( db: &State, flash: Option>, @@ -105,12 +105,7 @@ async fn kiosk( Template::render("kiosk", context.into_json()) } -#[post("/", data = "", rank = 2)] -async fn create( - db: &State, - data: Form, - _adminuser: AdminUser, -) -> Flash { +async fn create_logbook(db: &SqlitePool, data: Form) -> Flash{ match Logbook::create( db, data.into_inner() @@ -120,28 +115,47 @@ async fn create( 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::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::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")), } + +} + +#[post("/", data = "", rank = 2)] +async fn create( + db: &State, + data: Form, + _adminuser: AdminUser, +) -> Flash { + create_logbook(db, data).await } #[post("/", data = "")] -async fn create_kiosk(db: &State, data: Form) -> Flash { - match Logbook::create( - db, - data.into_inner() - ) - .await - { - Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Ausfahrt erfolgreich hinzugefügt"), - Err(LogbookCreateError::BoatAlreadyOnWater) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot schon am Wasser")), - Err(LogbookCreateError::ShipmasterAlreadyOnWater) => Flash::error(Redirect::to("/log"), format!("Schiffsführer schon am Wasser")), - Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot gesperrt")), - Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log/kiosk"), format!("Boot gibt's ned")), - Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log/kiosk"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")), +async fn create_kiosk(db: &State, data: Form, _kiosk: KioskCookie) -> Flash { + create_logbook(db, data).await +} + +async fn home_logbook(db: &SqlitePool, data: Form, logbook_id: i32, user: &User) -> Flash{ + let logbook: Option = Logbook::find_by_id(db, logbook_id).await; + let Some(logbook) = logbook else { + return Flash::error( + Redirect::to("/admin/log"), + format!("Log with ID {} does not exist!", logbook_id), + ) + }; + + 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(_) => Flash::error( + Redirect::to("/log"), + format!("Logbook with ID {} could not be updated!", logbook_id), + ), } + } #[post("/", data = "")] @@ -149,31 +163,10 @@ async fn home_kiosk( db: &State, data: Form, logbook_id: i32, + _kiosk: KioskCookie ) -> Flash { - let logbook = Logbook::find_by_id(db, logbook_id).await; - let Some(logbook) = logbook else { - return Flash::error( - Redirect::to("/log/kiosk"), - format!("Log with ID {} does not exist!", logbook_id), - ) - }; - - match logbook - .home( - db, - &User::find_by_id(db, logbook.shipmaster as i32) - .await - .unwrap(), - data.into_inner(), - ) - .await - { - Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Successfully updated log"), - Err(_) => Flash::error( - Redirect::to("/log/kiosk"), - format!("Logbook with ID {} could not be updated!", logbook_id), - ), - } + let logbook = Logbook::find_by_id(db, logbook_id).await.unwrap(); //TODO: fixme + home_logbook(db, data, logbook_id, &User::find_by_id(db, logbook.shipmaster as i32).await.unwrap()).await } #[post("/", data = "", rank = 2)] @@ -183,21 +176,7 @@ async fn home( logbook_id: i32, adminuser: AdminUser, ) -> Flash { - let logbook = Logbook::find_by_id(db, logbook_id).await; - let Some(logbook) = logbook else { - return Flash::error( - Redirect::to("/admin/log"), - format!("Log with ID {} does not exist!", logbook_id), - ) - }; - - match logbook.home(db, &adminuser.user, data.into_inner()).await { - Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"), - Err(_) => Flash::error( - Redirect::to("/log"), - format!("Logbook with ID {} could not be updated!", logbook_id), - ), - } + home_logbook(db, data, logbook_id, &adminuser.user).await } pub fn routes() -> Vec { diff --git a/templates/kiosk.html.tera b/templates/kiosk.html.tera index ec212d3..e3be4b6 100644 --- a/templates/kiosk.html.tera +++ b/templates/kiosk.html.tera @@ -4,6 +4,10 @@ {% extends "base" %} {% block content %} + +{% if flash %} + {{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }} + {% endif %}

Logbuch

Neue Ausfahrt starten

diff --git a/templates/log.html.tera b/templates/log.html.tera index 3e8a0ac..9772009 100644 --- a/templates/log.html.tera +++ b/templates/log.html.tera @@ -4,6 +4,10 @@ {% extends "base" %} {% block content %} +{% if flash %} + {{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }} +{% endif %} +

Logbuch

Neue Ausfahrt starten