diff --git a/frontend/main.ts b/frontend/main.ts index 5a4d8ff..640f60a 100644 --- a/frontend/main.ts +++ b/frontend/main.ts @@ -129,15 +129,18 @@ function setMaxAmountRowers(name: string, rowers: number) { // only_steering.parentElement?.parentElement?.parentElement?.classList.add('opacity-50'); // } //} - let shipmaster = document.querySelector('#shipmaster-newrowerjs'); - let steering_person = document.querySelector('#steering_person-newrowerjs'); + let shipmaster = document.querySelector('#shipmaster-'+name+'js'); + let steering_person = document.querySelector('#steering_person-'+name+'js'); if (rowers == 1){ if (shipmaster.parentNode) { (shipmaster.parentNode).classList.add('hidden'); } + shipmaster.removeAttribute('required'); + if (steering_person.parentNode){ (steering_person.parentNode).classList.add('hidden'); } + steering_person.removeAttribute('required'); }else{ if (shipmaster.parentNode){ (shipmaster.parentNode).classList.remove('hidden'); @@ -196,6 +199,31 @@ function initNewChoice(select: HTMLInputElement) { if (select.dataset && select.dataset.seats) { seats = +select.dataset.seats; } + console.log(seats); + + let shipmaster = document.querySelector('#shipmaster-'+select.id+'js'); + let steering_person = document.querySelector('#steering_person-'+select.id+'js'); + if (seats == 1){ + if (shipmaster.parentNode) { + (shipmaster.parentNode).classList.add('hidden'); + } + shipmaster.removeAttribute('required'); + + if (steering_person.parentNode){ + (steering_person.parentNode).classList.add('hidden'); + } + steering_person.removeAttribute('required'); + }else{ + if (shipmaster.parentNode){ + (shipmaster.parentNode).classList.remove('hidden'); + } + shipmaster.setAttribute('required', 'required'); + + if (steering_person.parentNode){ + (steering_person.parentNode).classList.remove('hidden'); + } + steering_person.setAttribute('required', 'required'); + } const choice = new Choices(select, { removeItemButton: true, loadingText: 'Wird geladen...', diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 6230ad4..85e4007 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -30,8 +30,8 @@ impl PartialEq for Logbook { #[derive(FromForm, Debug, Clone)] pub struct LogToAdd { pub boat_id: i32, - pub shipmaster: i64, - pub steering_person: i64, + pub shipmaster: Option, + pub steering_person: Option, pub shipmaster_only_steering: bool, pub departure: String, pub arrival: Option, @@ -44,8 +44,8 @@ pub struct LogToAdd { #[derive(FromForm, Debug)] pub struct LogToFinalize { - pub shipmaster: i64, - pub steering_person: i64, + pub shipmaster: Option, + pub steering_person: Option, pub shipmaster_only_steering: bool, pub departure: String, pub arrival: String, @@ -236,13 +236,18 @@ ORDER BY departure DESC pub async fn create( db: &SqlitePool, - log: LogToAdd, + mut log: LogToAdd, created_by_user: &User, ) -> Result<(), LogbookCreateError> { let Some(boat) = Boat::find_by_id(db, log.boat_id).await else { return Err(LogbookCreateError::BoatNotFound); }; + if boat.amount_seats == 1 { + log.shipmaster = Some(log.rowers[0]); + log.steering_person = Some(log.rowers[0]); + } + if let Ok(log_to_finalize) = TryInto::::try_into(log.clone()) { //TODO: fix clone() above @@ -293,10 +298,10 @@ ORDER BY departure DESC return Err(LogbookCreateError::BoatAlreadyOnWater); } - if !log.rowers.contains(&log.shipmaster) { + if !log.rowers.contains(&log.shipmaster.unwrap()) { return Err(LogbookCreateError::ShipmasterNotInRowers); } - if !log.rowers.contains(&log.steering_person) { + if !log.rowers.contains(&log.steering_person.unwrap()) { return Err(LogbookCreateError::SteeringPersonNotInRowers); } @@ -403,21 +408,26 @@ ORDER BY departure DESC &self, db: &mut Transaction<'_, Sqlite>, user: &User, - log: LogToFinalize, + mut log: LogToFinalize, ) -> Result<(), LogbookUpdateError> { //TODO: extract common tests with `create()` if user.id != self.shipmaster { return Err(LogbookUpdateError::NotYourEntry); } - if !log.rowers.contains(&log.shipmaster) { - return Err(LogbookUpdateError::ShipmasterNotInRowers); - } - if !log.rowers.contains(&log.steering_person) { - return Err(LogbookUpdateError::SteeringPersonNotInRowers); + let boat = Boat::find_by_id_tx(db, self.boat_id as i32).await.unwrap(); //ok + + if boat.amount_seats == 1 { + log.shipmaster = Some(log.rowers[0]); + log.steering_person = Some(log.rowers[0]); } - let boat = Boat::find_by_id_tx(db, self.boat_id as i32).await.unwrap(); //ok + if !log.rowers.contains(&log.shipmaster.unwrap()) { + return Err(LogbookUpdateError::ShipmasterNotInRowers); + } + if !log.rowers.contains(&log.steering_person.unwrap()) { + return Err(LogbookUpdateError::SteeringPersonNotInRowers); + } if !boat.shipmaster_allowed(&user).await && self.shipmaster != user.id { //second part: @@ -546,8 +556,8 @@ mod test { &pool, LogToAdd { boat_id: 3, - shipmaster: 4, - steering_person: 4, + shipmaster: Some(4), + steering_person: Some(4), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -571,8 +581,8 @@ mod test { &pool, LogToAdd { boat_id: 999, - shipmaster: 5, - steering_person: 5, + shipmaster: Some(5), + steering_person: Some(5), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -597,8 +607,8 @@ mod test { &pool, LogToAdd { boat_id: 5, - shipmaster: 5, - steering_person: 5, + shipmaster: Some(5), + steering_person: Some(5), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -623,8 +633,8 @@ mod test { &pool, LogToAdd { boat_id: 2, - shipmaster: 5, - steering_person: 5, + shipmaster: Some(5), + steering_person: Some(5), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -649,8 +659,8 @@ mod test { &pool, LogToAdd { boat_id: 3, - shipmaster: 5, - steering_person: 5, + shipmaster: Some(5), + steering_person: Some(5), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: Some("2128-05-20T11:00".into()), @@ -675,8 +685,8 @@ mod test { &pool, LogToAdd { boat_id: 3, - shipmaster: 2, - steering_person: 2, + shipmaster: Some(2), + steering_person: Some(2), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -701,8 +711,8 @@ mod test { &pool, LogToAdd { boat_id: 3, - shipmaster: 5, - steering_person: 1, + shipmaster: Some(5), + steering_person: Some(1), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -727,8 +737,8 @@ mod test { &pool, LogToAdd { boat_id: 1, - shipmaster: 5, - steering_person: 5, + shipmaster: Some(5), + steering_person: Some(5), shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, @@ -777,8 +787,8 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![2], - shipmaster: 2, - steering_person: 2, + shipmaster: Some(2), + steering_person: Some(2), shipmaster_only_steering: false, departure: "1990-01-01T10:00".into(), arrival: "1990-01-01T12:00".into(), @@ -805,8 +815,8 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![1], - shipmaster: 1, - steering_person: 1, + shipmaster: Some(1), + steering_person: Some(1), shipmaster_only_steering: false, departure: "1990-01-01T10:00".into(), arrival: "1990-01-01T12:00".into(), @@ -834,8 +844,8 @@ mod test { comments: Some("Perfect water".into()), logtype: None, rowers: vec![1, 2], - shipmaster: 2, - steering_person: 2, + shipmaster: Some(2), + steering_person: Some(2), shipmaster_only_steering: false, departure: "1990-01-01T10:00".into(), arrival: "1990-01-01T12:00".into(), diff --git a/src/tera/log.rs b/src/tera/log.rs index 1e2bdbb..c2626a5 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -210,7 +210,11 @@ async fn create_kiosk( data: Form, _kiosk: KioskCookie, ) -> Flash { - let creator = User::find_by_id(db, data.shipmaster as i32).await.unwrap(); + let creator = if let Some(shipmaster) = data.shipmaster { + User::find_by_id(db, shipmaster as i32).await.unwrap() + } else { + User::find_by_id(db, data.rowers[0] as i32).await.unwrap() + }; Log::create( db, format!(