send departure as timestamp

This commit is contained in:
philipp 2023-07-30 20:40:15 +02:00
parent 09672c2a6e
commit dee228dce8

View File

@ -1,4 +1,4 @@
use chrono::NaiveDateTime; use chrono::{Local, NaiveDateTime, TimeZone};
use rocket::FromForm; use rocket::FromForm;
use serde::Serialize; use serde::Serialize;
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -50,6 +50,8 @@ pub struct LogbookWithBoatAndRowers {
pub boat: Boat, pub boat: Boat,
pub shipmaster_user: User, pub shipmaster_user: User,
pub rowers: Vec<User>, pub rowers: Vec<User>,
pub departure_timestamp: i64,
pub arrival_timestamp: Option<i64>,
} }
pub enum LogbookUpdateError { pub enum LogbookUpdateError {
@ -98,11 +100,20 @@ impl Logbook {
let mut ret = Vec::new(); let mut ret = Vec::new();
for log in logs { 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 { ret.push(LogbookWithBoatAndRowers {
rowers: Rower::for_log(db, &log).await, rowers: Rower::for_log(db, &log).await,
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), 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(), shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
logbook: log, logbook: log,
arrival_timestamp: None, //TODO: send arrival timestmap
departure_timestamp: date_time.timestamp(),
}); });
} }
ret ret
@ -129,6 +140,8 @@ impl Logbook {
boat: Boat::find_by_id(db, log.boat_id as i32).await.unwrap(), 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(), shipmaster_user: User::find_by_id(db, log.shipmaster as i32).await.unwrap(),
logbook: log, logbook: log,
arrival_timestamp: None,
departure_timestamp: 0,
}); });
} }
ret ret
@ -146,7 +159,10 @@ impl Logbook {
if boat.on_water(db).await { if boat.on_water(db).await {
return Err(LogbookCreateError::BoatAlreadyOnWater); 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); return Err(LogbookCreateError::ShipmasterAlreadyOnWater);
} }
@ -200,13 +216,17 @@ impl Logbook {
.await .await
.unwrap(); .unwrap();
result.into_iter().filter_map(|r| { result
if let (Some(destination), Some(distance_in_km)) = (r.destination, r.distance_in_km) { .into_iter()
.filter_map(|r| {
if let (Some(destination), Some(distance_in_km)) = (r.destination, r.distance_in_km)
{
Some((destination, distance_in_km)) Some((destination, distance_in_km))
} else { } else {
None None
} }
}).collect() })
.collect()
} }
async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) { async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) {