send departure as timestamp
This commit is contained in:
parent
09672c2a6e
commit
dee228dce8
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,19 +210,23 @@ impl Logbook {
|
|||||||
Ok(())
|
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;")
|
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)
|
.fetch_all(db)
|
||||||
.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>) {
|
||||||
|
Loading…
Reference in New Issue
Block a user