From d9c7c218bc37c3d58954fd26d8b5094ee57035b3 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 31 Oct 2023 20:57:05 +0100 Subject: [PATCH] allow no cox in 1x; require them in >2x --- frontend/main.ts | 42 +++++++++++++++++++++++++++--------------- src/model/logbook.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/frontend/main.ts b/frontend/main.ts index f65bfe0..fa83eb5 100644 --- a/frontend/main.ts +++ b/frontend/main.ts @@ -124,21 +124,33 @@ function setMaxAmountRowers(rowers: number, isShipmasterSteering='false') { } } - let only_steering = document.querySelector('#shipmaster_only_steering'); - if(only_steering) { - if(isShipmasterSteering == 'true') { - only_steering.removeAttribute('disabled'); - only_steering.setAttribute('checked', 'true'); - only_steering.parentElement?.parentElement?.parentElement?.classList.remove('hidden'); - only_steering.parentElement?.parentElement?.parentElement?.classList.remove('md:block'); - only_steering.parentElement?.parentElement?.parentElement?.classList.remove('opacity-50'); - } else { - only_steering.setAttribute('disabled', 'disabled'); - only_steering.removeAttribute('checked'); - only_steering.parentElement?.parentElement?.parentElement?.classList.add('hidden'); - only_steering.parentElement?.parentElement?.parentElement?.classList.add('md:block'); - only_steering.parentElement?.parentElement?.parentElement?.classList.add('opacity-50'); - } + //let only_steering = document.querySelector('#shipmaster_only_steering'); + //if(only_steering) { + // if(isShipmasterSteering == 'true') { + // only_steering.removeAttribute('disabled'); + // only_steering.setAttribute('checked', 'true'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('hidden'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('md:block'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.remove('opacity-50'); + // } else { + // only_steering.setAttribute('disabled', 'disabled'); + // only_steering.removeAttribute('checked'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.add('hidden'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.add('md:block'); + // only_steering.parentElement?.parentElement?.parentElement?.classList.add('opacity-50'); + // } + //} + let shipmaster = document.querySelector('#shipmaster-newrowerjs'); + let steering_person = document.querySelector('#steering_person-newrowerjs'); + if (rowers == 1){ + shipmaster.parentNode.classList.add('hidden'); + steering_person.parentNode.classList.add('hidden'); + }else{ + shipmaster.parentNode.classList.remove('hidden'); + shipmaster.setAttribute('required', 'required'); + + steering_person.parentNode.classList.remove('hidden'); + steering_person.setAttribute('required', 'required'); } } } diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 4289231..412cb8e 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -180,6 +180,48 @@ ORDER BY departure DESC return Err(LogbookCreateError::BoatNotFound); }; + if let Ok(log_to_finalize) = TryInto::::try_into(log.clone()) { + //TODO: fix clone() above + + if !boat.shipmaster_allowed(created_by_user).await { + return Err(LogbookCreateError::UserNotAllowedToUseBoat); + } + + let mut tx = db.begin().await.unwrap(); + + let inserted_row = sqlx::query!( + "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, + log.destination, + log.distance_in_km, + log.comments, + log.logtype + ) + .fetch_one(&mut tx) + .await.unwrap().id; + + let logbook = Logbook::find_by_id(db, inserted_row as i32).await.unwrap(); //ok + + return match logbook + .home_with_transaction(&mut tx, created_by_user, log_to_finalize) + .await + { + Ok(_) => { + tx.commit().await.unwrap(); + Ok(()) + } + Err(a) => Err(a.into()), + }; + } + if log.arrival.is_some() { + return Err(LogbookCreateError::ArrivalSetButNotRemainingTwo); + } + if boat.is_locked(db).await { return Err(LogbookCreateError::BoatLocked); }