From 0558b0458cd9a15b9073ea928bffab072b773bf9 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 24 Sep 2023 09:48:01 +0200 Subject: [PATCH] verify that arrivaldate > departuredate --- seeds.sql | 6 +++--- src/model/logbook.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/tera/log.rs | 2 ++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/seeds.sql b/seeds.sql index 5fa2847..00652ed 100644 --- a/seeds.sql +++ b/seeds.sql @@ -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 "logbook_type" (name) VALUES ('Wanderfahrt'); 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, 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 (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) 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, '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, '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 "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); diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 7c2ab1a..1480c7c 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -65,6 +65,7 @@ pub enum LogbookUpdateError { NotYourEntry, TooManyRowers(usize, usize), RowerCreateError(i64, String), + ArrivalNotAfterDeparture, } #[derive(Debug, PartialEq)] @@ -77,6 +78,7 @@ pub enum LogbookCreateError { RowerAlreadyOnWater(User), RowerCreateError(i64, String), SamePersonShipmasterAndRower, + ArrivalNotAfterDeparture, } impl Logbook { @@ -181,6 +183,7 @@ ORDER BY departure DESC 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 @@ -188,6 +191,14 @@ ORDER BY departure DESC 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 { return Err(LogbookCreateError::TooManyRowers( 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 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(); @@ -468,6 +483,30 @@ mod test { 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] fn test_create_shipmaster_on_water() { let pool = testdb!(); diff --git a/src/tera/log.rs b/src/tera/log.rs index 57ce8e6..9a1cda3 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -147,6 +147,8 @@ async fn create_logbook(db: &SqlitePool, data: Form) -> Flash 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::ArrivalNotAfterDeparture) => Flash::error(Redirect::to("/log"), format!("Ankunftszeit kann nicht vor der Abfahrtszeit sein")), + } }