From 09075989e305fb5eea93ba35be75fd88b3d0f181 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 29 Oct 2023 18:42:12 +0100 Subject: [PATCH] first draft --- migration.sql | 1 + seeds.sql | 6 +++--- src/model/logbook.rs | 47 ++++++++++++++++++++++++++++++++++++++++---- src/tera/log.rs | 34 ++++++++++++++++---------------- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/migration.sql b/migration.sql index bff1516..f81938d 100644 --- a/migration.sql +++ b/migration.sql @@ -87,6 +87,7 @@ CREATE TABLE IF NOT EXISTS "logbook" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "boat_id" INTEGER NOT NULL REFERENCES boat(id), "shipmaster" INTEGER NOT NULL REFERENCES user(id), + "steering_person" INTEGER NOT NULL REFERENCES user(id), "shipmaster_only_steering" boolean not null, "departure" datetime not null, "arrival" datetime, -- None -> ship is on water diff --git a/seeds.sql b/seeds.sql index e825338..888f552 100644 --- a/seeds.sql +++ b/seeds.sql @@ -26,9 +26,9 @@ INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Ottensheim Boot', INSERT INTO "boat" (name, amount_seats, location_id, owner) VALUES ('second_private_boat_from_rower', 1, 1, 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, '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 "logbook" (boat_id, shipmaster,steering_person, shipmaster_only_steering, departure) VALUES (2, 2, 2, false, '1142-12-24 10:00'); +INSERT INTO "logbook" (boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (1, 4, 4, false, '1141-12-24 10:00', '2141-12-24 15:00', 'Ottensheim', 25); +INSERT INTO "logbook" (boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (3, 4, 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 4289231..5d2e763 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -11,6 +11,7 @@ pub struct Logbook { pub id: i64, pub boat_id: i64, pub shipmaster: i64, + pub steering_person: i64, #[serde(default = "bool::default")] pub shipmaster_only_steering: bool, pub departure: NaiveDateTime, @@ -31,6 +32,7 @@ impl PartialEq for Logbook { pub struct LogToAdd { pub boat_id: i32, pub shipmaster: i64, + pub steering_person: i64, pub shipmaster_only_steering: bool, pub departure: String, pub arrival: Option, @@ -43,6 +45,11 @@ pub struct LogToAdd { #[derive(FromForm, Debug)] pub struct LogToFinalize { + pub shipmaster: i64, + pub steering_person: i64, + pub shipmaster_only_steering: bool, + pub departure: String, + pub arrival: String, pub destination: String, pub distance_in_km: i64, pub comments: Option, @@ -56,6 +63,7 @@ pub struct LogbookWithBoatAndRowers { pub logbook: Logbook, pub boat: Boat, pub shipmaster_user: User, + pub steering_user: User, pub rowers: Vec, } @@ -93,7 +101,7 @@ impl Logbook { sqlx::query_as!( Self, " - SELECT id,boat_id,shipmaster,shipmaster_only_steering,departure,arrival,destination,distance_in_km,comments,logtype + SELECT id,boat_id,shipmaster,steering_person,shipmaster_only_steering,departure,arrival,destination,distance_in_km,comments,logtype FROM logbook WHERE id like ? ", @@ -107,7 +115,7 @@ impl Logbook { pub async fn on_water(db: &SqlitePool) -> Vec { let rows = sqlx::query!( " -SELECT id, boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype +SELECT id, boat_id, shipmaster,steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype FROM logbook WHERE arrival is null ORDER BY departure DESC @@ -123,6 +131,7 @@ ORDER BY departure DESC id: row.id, boat_id: row.boat_id, shipmaster: row.shipmaster, + steering_person: row.steering_person, shipmaster_only_steering: row.shipmaster_only_steering, departure: row.departure, arrival: row.arrival, @@ -139,6 +148,9 @@ ORDER BY departure DESC rowers: Rower::for_log(db, &log).await, boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), + steering_user: User::find_by_id(db, log.steering_person as i32) + .await + .unwrap(), logbook: log, }); } @@ -149,7 +161,7 @@ ORDER BY departure DESC let logs = sqlx::query_as!( Logbook, " - SELECT id, boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype + SELECT id, boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype FROM logbook WHERE arrival is not null ORDER BY departure DESC @@ -165,6 +177,9 @@ ORDER BY departure DESC rowers: Rower::for_log(db, &log).await, boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(), + steering_user: User::find_by_id(db, log.steering_person as i32) + .await + .unwrap(), logbook: log, }); } @@ -252,9 +267,10 @@ ORDER BY departure DESC //}); //let arrival = log.arrival.map(|a| format!("{}+02:00", a)); let inserted_row = sqlx::query!( - "INSERT INTO logbook(boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype) VALUES (?,?,?,?,?,?,?,?,?) RETURNING id", + "INSERT INTO logbook(boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype) VALUES (?,?,?,?,?,?,?,?,?,?) RETURNING id", log.boat_id, log.shipmaster, + log.steering_person, log.shipmaster_only_steering, log.departure, log.arrival, @@ -444,6 +460,7 @@ mod test { LogToAdd { boat_id: 3, shipmaster: 4, + steering_person: 4, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -468,6 +485,7 @@ mod test { LogToAdd { boat_id: 999, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -493,6 +511,7 @@ mod test { LogToAdd { boat_id: 5, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -518,6 +537,7 @@ mod test { LogToAdd { boat_id: 2, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -543,6 +563,7 @@ mod test { LogToAdd { boat_id: 3, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: Some("2128-05-20T11:00".into()), @@ -568,6 +589,7 @@ mod test { LogToAdd { boat_id: 3, shipmaster: 2, + steering_person: 2, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -593,6 +615,7 @@ mod test { LogToAdd { boat_id: 3, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -618,6 +641,7 @@ mod test { LogToAdd { boat_id: 1, shipmaster: 5, + steering_person: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -666,6 +690,11 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![], + shipmaster: 2, + steering_person: 2, + shipmaster_only_steering: false, + departure: "1990-01-01T10:00".into(), + arrival: "1990-01-01T12:00".into(), }, ) .await @@ -689,6 +718,11 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![], + shipmaster: 1, + steering_person: 1, + shipmaster_only_steering: false, + departure: "1990-01-01T10:00".into(), + arrival: "1990-01-01T12:00".into(), }, ) .await; @@ -713,6 +747,11 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![1], + shipmaster: 2, + steering_person: 2, + shipmaster_only_steering: false, + departure: "1990-01-01T10:00".into(), + arrival: "1990-01-01T12:00".into(), }, ) .await; diff --git a/src/tera/log.rs b/src/tera/log.rs index fc4acba..b3787dc 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -162,15 +162,6 @@ async fn create_logbook( data: Form, user: &NonGuestUser, ) -> Flash { - Log::create( - db, - format!( - "User {} tries to create log entry={:?}", - user.user.name, data - ), - ) - .await; - match Logbook::create( db, data.into_inner(), @@ -201,6 +192,15 @@ async fn create( data: Form, user: NonGuestUser, ) -> Flash { + Log::create( + db, + format!( + "User {} tries to create log entry={:?}", + user.user.name, data + ), + ) + .await; + create_logbook(db, data, &user).await } @@ -515,7 +515,7 @@ mod test { let req = client .post("/log") .header(ContentType::Form) - .body("boat_id=1&shipmaster=4&departure=2199-12-31T10:00"); + .body("boat_id=1&shipmaster=4&departure=2199-12-31T10:00&steering_person=4"); let response = req.dispatch().await; assert_eq!(response.status(), Status::SeeOther); @@ -546,7 +546,7 @@ mod test { let req = client .post("/log/1") .header(ContentType::Form) - .body("destination=Ottensheim&distance_in_km=25"); + .body("destination=Ottensheim&distance_in_km=25&shipmaster=1&steering_person=1&departure=1990-01-01T10:00&arrival=1990-01-01T12:00"); let response = req.dispatch().await; assert_eq!(response.status(), Status::SeeOther); @@ -655,7 +655,7 @@ mod test { let shipmaster_id = User::find_by_name(&db, "rower2".into()).await.unwrap().id; let req = client.post("/log").header(ContentType::Form).body(format!( - "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00" + "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00&steering_person={shipmaster_id}" )); let response = req.dispatch().await; @@ -684,7 +684,7 @@ mod test { let req = client .post(format!("/log/{log_id}")) .header(ContentType::Form) - .body("destination=Ottensheim&distance_in_km=25"); + .body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure=1990-01-01T10:00&arrival=1990-01-01T12:00")); let response = req.dispatch().await; assert_eq!(response.status(), Status::SeeOther); @@ -875,7 +875,7 @@ mod test { let req = client .post("/log/1") .header(ContentType::Form) - .body("destination=Ottensheim&distance_in_km=25"); + .body("destination=Ottensheim&distance_in_km=25&shipmaster=1&steering_person=1&departure=1199-12-12T10:00&arrival=1199-12-12T12:00"); let response = req.dispatch().await; assert_eq!(response.status(), Status::SeeOther); @@ -902,7 +902,7 @@ mod test { let shipmaster_id = User::find_by_name(db, &shipmaster_name).await.unwrap().id; let req = client.post("/log").header(ContentType::Form).body(format!( - "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00" + "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=1199-12-31T10:00&steering_person={shipmaster_id}" )); let response = req.dispatch().await; @@ -924,7 +924,7 @@ mod test { let req = client .post(format!("/log/{log_id}")) .header(ContentType::Form) - .body("destination=Ottensheim&distance_in_km=25"); + .body(format!("destination=Ottensheim&distance_in_km=25&shipmaster={shipmaster_id}&steering_person={shipmaster_id}&departure=1199-12-31T10:00&arrival=1199-12-31T12:00")); let response = req.dispatch().await; assert_eq!(response.status(), Status::SeeOther); @@ -952,7 +952,7 @@ mod test { let shipmaster_id = User::find_by_name(db, &shipmaster_name).await.unwrap().id; let req = client.post("/log").header(ContentType::Form).body(format!( - "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=2199-12-31T10:00" + "boat_id={boat_id}&shipmaster={shipmaster_id}&departure=2199-12-31T10:00&steering_person={shipmaster_id}" )); let response = req.dispatch().await;