diff --git a/src/model/logbook.rs b/src/model/logbook.rs index dad29e5..449ad30 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -62,6 +62,7 @@ pub enum LogbookCreateError { BoatLocked, BoatNotFound, TooManyRowers(usize, usize), + ShipmasterAlreadyOnWater, } impl Logbook { @@ -144,6 +145,10 @@ impl Logbook { if boat.on_water(db).await { return Err(LogbookCreateError::BoatAlreadyOnWater); } + if (User::find_by_id(db, log.shipmaster as i32).await.unwrap()).on_water(db).await { + return Err(LogbookCreateError::BoatAlreadyOnWater); + } + if log.rower.len() > boat.amount_seats as usize - 1 { return Err(LogbookCreateError::TooManyRowers( diff --git a/src/model/user.rs b/src/model/user.rs index 7bc25ff..0079d50 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -93,6 +93,17 @@ WHERE name like ? .ok() } + pub async fn on_water(&self, db: &SqlitePool) -> bool { + sqlx::query!( + "SELECT * FROM logbook WHERE shipmaster=? AND arrival is null", + self.id + ) + .fetch_optional(db) + .await + .unwrap() + .is_some() + } + pub async fn all(db: &SqlitePool) -> Vec { sqlx::query_as!( User, diff --git a/src/tera/log.rs b/src/tera/log.rs index 71130e3..58df629 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -119,9 +119,11 @@ 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::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)")), + } } @@ -135,6 +137,7 @@ async fn create_kiosk(db: &State, data: Form) -> Flash 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)")),