From dee228dce823bb7d4584b174a9ca6429a210876c Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 30 Jul 2023 20:40:15 +0200 Subject: [PATCH] send departure as timestamp --- src/model/logbook.rs | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 92d5025..7537d43 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDateTime; +use chrono::{Local, NaiveDateTime, TimeZone}; use rocket::FromForm; use serde::Serialize; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; @@ -50,6 +50,8 @@ pub struct LogbookWithBoatAndRowers { pub boat: Boat, pub shipmaster_user: User, pub rowers: Vec, + pub departure_timestamp: i64, + pub arrival_timestamp: Option, } pub enum LogbookUpdateError { @@ -98,11 +100,20 @@ impl Logbook { let mut ret = Vec::new(); for log in logs { + let date_time_naive = + NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%d %H:%M:%S").unwrap(); + let date_time = Local + .from_local_datetime(&date_time_naive) + .single() + .unwrap(); + ret.push(LogbookWithBoatAndRowers { 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(), logbook: log, + arrival_timestamp: None, //TODO: send arrival timestmap + departure_timestamp: date_time.timestamp(), }); } ret @@ -129,6 +140,8 @@ impl Logbook { 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(), logbook: log, + arrival_timestamp: None, + departure_timestamp: 0, }); } ret @@ -146,10 +159,13 @@ impl Logbook { 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 { + if (User::find_by_id(db, log.shipmaster as i32).await.unwrap()) + .on_water(db) + .await + { return Err(LogbookCreateError::ShipmasterAlreadyOnWater); } - + if log.rower.len() > boat.amount_seats as usize - 1 { return Err(LogbookCreateError::TooManyRowers( boat.amount_seats as usize, @@ -194,19 +210,23 @@ impl Logbook { Ok(()) } - pub async fn distances(db: &SqlitePool) -> Vec<(String, i64)>{ + pub async fn distances(db: &SqlitePool) -> Vec<(String, i64)> { let result = sqlx::query!("SELECT destination, distance_in_km FROM logbook WHERE id IN (SELECT MIN(id) FROM logbook GROUP BY destination) AND destination IS NOT NULL AND distance_in_km IS NOT NULL;") .fetch_all(db) .await .unwrap(); - result.into_iter().filter_map(|r| { - if let (Some(destination), Some(distance_in_km)) = (r.destination, r.distance_in_km) { - Some((destination, distance_in_km)) - } else { - None - } - }).collect() + result + .into_iter() + .filter_map(|r| { + if let (Some(destination), Some(distance_in_km)) = (r.destination, r.distance_in_km) + { + Some((destination, distance_in_km)) + } else { + None + } + }) + .collect() } async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) {