check if shipmaster is already on the water
This commit is contained in:
		| @@ -62,6 +62,7 @@ pub enum LogbookCreateError { | |||||||
|     BoatLocked, |     BoatLocked, | ||||||
|     BoatNotFound, |     BoatNotFound, | ||||||
|     TooManyRowers(usize, usize), |     TooManyRowers(usize, usize), | ||||||
|  |     ShipmasterAlreadyOnWater, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Logbook { | impl Logbook { | ||||||
| @@ -144,6 +145,10 @@ impl Logbook { | |||||||
|         if boat.on_water(db).await { |         if boat.on_water(db).await { | ||||||
|             return Err(LogbookCreateError::BoatAlreadyOnWater); |             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 { |         if log.rower.len() > boat.amount_seats as usize - 1 { | ||||||
|             return Err(LogbookCreateError::TooManyRowers( |             return Err(LogbookCreateError::TooManyRowers( | ||||||
|   | |||||||
| @@ -93,6 +93,17 @@ WHERE name like ? | |||||||
|         .ok() |         .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<Self> { |     pub async fn all(db: &SqlitePool) -> Vec<Self> { | ||||||
|         sqlx::query_as!( |         sqlx::query_as!( | ||||||
|             User, |             User, | ||||||
|   | |||||||
| @@ -119,9 +119,11 @@ async fn create( | |||||||
|     { |     { | ||||||
|         Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"), |         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::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::BoatLocked) => Flash::error(Redirect::to("/log"), format!("Boot gesperrt")), | ||||||
|         Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), format!("Boot gibt's ned")), |         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)")), |         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<SqlitePool>, data: Form<LogToAdd>) -> Flash<Red | |||||||
|     { |     { | ||||||
|         Ok(_) => Flash::success(Redirect::to("/log/kiosk"), "Ausfahrt erfolgreich hinzugefügt"), |         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::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::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::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)")), |         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)")), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user