verify that arrivaldate > departuredate

This commit is contained in:
philipp 2023-09-24 09:48:01 +02:00
parent 03880af865
commit 0558b0458c
3 changed files with 44 additions and 3 deletions

View File

@ -25,9 +25,9 @@ INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Sehr kaputtes Boot
INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Ottensheim Boot', 7, 2); INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Ottensheim Boot', 7, 2);
INSERT INTO "logbook_type" (name) VALUES ('Wanderfahrt'); INSERT INTO "logbook_type" (name) VALUES ('Wanderfahrt');
INSERT INTO "logbook_type" (name) VALUES ('Regatta'); INSERT INTO "logbook_type" (name) VALUES ('Regatta');
INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure) VALUES (2, 2, false, '2142-12-24 10:00'); INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure) VALUES (2, 2, false, '1142-12-24 10:00');
INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (1, 4, false, '2141-12-24 10:00', '2141-12-24 15:00', 'Ottensheim', 25); INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (1, 4, false, '1141-12-24 10:00', '2141-12-24 15:00', 'Ottensheim', 25);
INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (3, 4, false, '2142-12-24 10:00', '2142-12-24 11:30', 'Ottensheim + Regattastrecke', 29); INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (3, 4, false, '1142-12-24 10:00', '2142-12-24 11:30', 'Ottensheim + Regattastrecke', 29);
INSERT INTO "rower" (logbook_id, rower_id) VALUES(3,3); INSERT INTO "rower" (logbook_id, rower_id) VALUES(3,3);
INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at) VALUES(4,'Dolle bei Position 2 fehlt', 5, '2142-12-24 15:02'); INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at) VALUES(4,'Dolle bei Position 2 fehlt', 5, '2142-12-24 15:02');
INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at, lock_boat) VALUES(5, 'TOHT', 5, '2142-12-24 15:02', 1); INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at, lock_boat) VALUES(5, 'TOHT', 5, '2142-12-24 15:02', 1);

View File

@ -65,6 +65,7 @@ pub enum LogbookUpdateError {
NotYourEntry, NotYourEntry,
TooManyRowers(usize, usize), TooManyRowers(usize, usize),
RowerCreateError(i64, String), RowerCreateError(i64, String),
ArrivalNotAfterDeparture,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -77,6 +78,7 @@ pub enum LogbookCreateError {
RowerAlreadyOnWater(User), RowerAlreadyOnWater(User),
RowerCreateError(i64, String), RowerCreateError(i64, String),
SamePersonShipmasterAndRower, SamePersonShipmasterAndRower,
ArrivalNotAfterDeparture,
} }
impl Logbook { impl Logbook {
@ -181,6 +183,7 @@ ORDER BY departure DESC
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()) if (User::find_by_id(db, log.shipmaster as i32).await.unwrap())
.on_water(db) .on_water(db)
.await .await
@ -188,6 +191,14 @@ ORDER BY departure DESC
return Err(LogbookCreateError::ShipmasterAlreadyOnWater); return Err(LogbookCreateError::ShipmasterAlreadyOnWater);
} }
if let Some(arrival) = &log.arrival {
let dep = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%d %H:%M").unwrap();
let arr = NaiveDateTime::parse_from_str(&arrival, "%Y-%m-%d %H:%M").unwrap();
if arr <= dep {
return Err(LogbookCreateError::ArrivalNotAfterDeparture);
}
}
if log.rowers.len() > boat.amount_seats as usize - 1 { if log.rowers.len() > boat.amount_seats as usize - 1 {
return Err(LogbookCreateError::TooManyRowers( return Err(LogbookCreateError::TooManyRowers(
boat.amount_seats as usize, boat.amount_seats as usize,
@ -284,6 +295,10 @@ ORDER BY departure DESC
} }
let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M")); let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M"));
let chrono_arrival = NaiveDateTime::parse_from_str(&arrival, "%Y-%m-%d %H:%M").unwrap();
if chrono_arrival <= self.departure {
return Err(LogbookUpdateError::ArrivalNotAfterDeparture);
}
let mut tx = db.begin().await.unwrap(); let mut tx = db.begin().await.unwrap();
@ -468,6 +483,30 @@ mod test {
assert_eq!(res, Err(LogbookCreateError::BoatAlreadyOnWater)); assert_eq!(res, Err(LogbookCreateError::BoatAlreadyOnWater));
} }
#[sqlx::test]
fn test_create_boat_on_water_wrong_arrival() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 3,
shipmaster: 5,
shipmaster_only_steering: false,
departure: "2128-05-20 12:00".into(),
arrival: Some("2128-05-20 11:00".into()),
destination: None,
distance_in_km: None,
comments: None,
logtype: None,
rowers: Vec::new(),
},
)
.await;
assert_eq!(res, Err(LogbookCreateError::ArrivalNotAfterDeparture));
}
#[sqlx::test] #[sqlx::test]
fn test_create_shipmaster_on_water() { fn test_create_shipmaster_on_water() {
let pool = testdb!(); let pool = testdb!();

View File

@ -147,6 +147,8 @@ async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect
Err(LogbookCreateError::RowerCreateError(rower, e)) => Flash::error(Redirect::to("/log"), format!("Fehler bei Ruderer {rower}: {e}")), 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")), Err(LogbookCreateError::SamePersonShipmasterAndRower) => Flash::error(Redirect::to("/log"), format!("Selbe Person als Schiffsführer und Ruderer ausgewählt")),
Err(LogbookCreateError::ArrivalNotAfterDeparture) => Flash::error(Redirect::to("/log"), format!("Ankunftszeit kann nicht vor der Abfahrtszeit sein")),
} }
} }