check if shipmaster is already on the water
This commit is contained in:
parent
621058356d
commit
0c906f2bda
@ -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(
|
||||
|
@ -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<Self> {
|
||||
sqlx::query_as!(
|
||||
User,
|
||||
|
@ -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<SqlitePool>, data: Form<LogToAdd>) -> Flash<Red
|
||||
{
|
||||
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)")),
|
||||
|
Loading…
x
Reference in New Issue
Block a user