error if same person selected as cox + rower

This commit is contained in:
philipp 2023-09-24 09:12:27 +02:00
parent 78d169e056
commit e849b6f5f9
2 changed files with 30 additions and 0 deletions

View File

@ -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!();

View File

@ -145,6 +145,7 @@ async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect
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::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")),
}
}