From e849b6f5f97b00c33089b1bb26d35e6ea17d885b Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 24 Sep 2023 09:12:27 +0200 Subject: [PATCH] error if same person selected as cox + rower --- src/model/logbook.rs | 29 +++++++++++++++++++++++++++++ src/tera/log.rs | 1 + 2 files changed, 30 insertions(+) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 5ded709..7c2ab1a 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -76,6 +76,7 @@ pub enum LogbookCreateError { ShipmasterAlreadyOnWater, RowerAlreadyOnWater(User), RowerCreateError(i64, String), + SamePersonShipmasterAndRower, } impl Logbook { @@ -196,6 +197,10 @@ ORDER BY departure DESC for rower in &log.rowers { let user = User::find_by_id(db, *rower as i32).await.unwrap(); + if *rower == log.shipmaster { + return Err(LogbookCreateError::SamePersonShipmasterAndRower); + } + if user.on_water(db).await { return Err(LogbookCreateError::RowerAlreadyOnWater(user)); } @@ -487,6 +492,30 @@ mod test { assert_eq!(res, Err(LogbookCreateError::ShipmasterAlreadyOnWater)); } + #[sqlx::test] + fn test_create_same_person_cox_and_rower() { + let pool = testdb!(); + + let res = Logbook::create( + &pool, + LogToAdd { + boat_id: 3, + shipmaster: 5, + shipmaster_only_steering: false, + departure: "2128-05-20T12:00".into(), + arrival: None, + destination: None, + distance_in_km: None, + comments: None, + logtype: None, + rowers: vec![5], + }, + ) + .await; + + assert_eq!(res, Err(LogbookCreateError::SamePersonShipmasterAndRower)); + } + #[sqlx::test] fn test_create_too_many_rowers() { let pool = testdb!(); diff --git a/src/tera/log.rs b/src/tera/log.rs index a5cb652..57ce8e6 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -145,6 +145,7 @@ async fn create_logbook(db: &SqlitePool, data: Form) -> Flash 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)")), 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")), } }