2023-07-23 12:17:57 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2023-07-24 13:01:39 +02:00
|
|
|
use serde::Serialize;
|
2023-07-23 12:17:57 +02:00
|
|
|
use sqlx::{FromRow, SqlitePool};
|
|
|
|
|
2023-07-24 13:01:39 +02:00
|
|
|
use super::{boat::Boat, rower::Rower, user::User};
|
2023-07-23 16:49:14 +02:00
|
|
|
|
2023-07-24 13:01:39 +02:00
|
|
|
#[derive(FromRow, Serialize, Clone)]
|
2023-07-23 12:17:57 +02:00
|
|
|
pub struct Logbook {
|
|
|
|
pub id: i64,
|
|
|
|
pub boat_id: i64,
|
|
|
|
pub shipmaster: i64,
|
|
|
|
#[serde(default = "bool::default")]
|
|
|
|
pub shipmaster_only_steering: bool,
|
|
|
|
pub departure: String, //TODO: Switch to chrono::nativedatetime
|
|
|
|
pub arrival: Option<String>, //TODO: Switch to chrono::nativedatetime
|
|
|
|
pub destination: Option<String>,
|
|
|
|
pub distance_in_km: Option<i64>,
|
|
|
|
pub comments: Option<String>,
|
|
|
|
pub logtype: Option<i64>,
|
|
|
|
}
|
|
|
|
|
2023-07-24 13:01:39 +02:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct LogbookWithBoatAndRowers {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub logbook: Logbook,
|
|
|
|
pub boat: Boat,
|
|
|
|
pub shipmaster_user: User,
|
|
|
|
pub rowers: Vec<User>,
|
2023-07-23 12:17:57 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 16:49:14 +02:00
|
|
|
pub enum LogbookUpdateError {
|
|
|
|
NotYourEntry,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum LogbookCreateError {
|
|
|
|
BoatAlreadyOnWater,
|
2023-07-23 19:45:48 +02:00
|
|
|
BoatLocked,
|
2023-07-24 13:01:39 +02:00
|
|
|
BoatNotFound,
|
|
|
|
TooManyRowers(usize, usize),
|
2023-07-23 16:49:14 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 12:17:57 +02:00
|
|
|
impl Logbook {
|
2023-07-23 16:49:14 +02:00
|
|
|
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Self,
|
|
|
|
"
|
|
|
|
SELECT id,boat_id,shipmaster,shipmaster_only_steering,departure,arrival,destination,distance_in_km,comments,logtype
|
|
|
|
FROM logbook
|
|
|
|
WHERE id like ?
|
|
|
|
",
|
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:17:57 +02:00
|
|
|
// pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
|
|
|
|
// sqlx::query_as!(
|
|
|
|
// User,
|
|
|
|
// "
|
|
|
|
//SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access
|
|
|
|
//FROM user
|
|
|
|
//WHERE name like ?
|
|
|
|
// ",
|
|
|
|
// name
|
|
|
|
// )
|
|
|
|
// .fetch_one(db)
|
|
|
|
// .await
|
|
|
|
// .ok()
|
|
|
|
// }
|
|
|
|
//
|
2023-07-24 13:01:39 +02:00
|
|
|
pub async fn on_water(db: &SqlitePool) -> Vec<LogbookWithBoatAndRowers> {
|
|
|
|
let logs = sqlx::query_as!(
|
|
|
|
Logbook,
|
2023-07-23 12:17:57 +02:00
|
|
|
"
|
2023-07-24 13:01:39 +02:00
|
|
|
SELECT id, boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
|
2023-07-23 12:17:57 +02:00
|
|
|
FROM logbook
|
|
|
|
WHERE arrival is null
|
|
|
|
ORDER BY departure DESC
|
|
|
|
"
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
2023-07-24 13:01:39 +02:00
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for log in logs {
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ret
|
2023-07-23 12:17:57 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 13:01:39 +02:00
|
|
|
pub async fn completed(db: &SqlitePool) -> Vec<LogbookWithBoatAndRowers> {
|
|
|
|
let logs = sqlx::query_as!(
|
|
|
|
Logbook,
|
2023-07-23 12:17:57 +02:00
|
|
|
"
|
2023-07-24 13:01:39 +02:00
|
|
|
SELECT id, boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
|
2023-07-23 12:17:57 +02:00
|
|
|
FROM logbook
|
|
|
|
WHERE arrival is not null
|
2023-07-24 13:01:39 +02:00
|
|
|
ORDER BY departure DESC
|
2023-07-23 12:17:57 +02:00
|
|
|
"
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
2023-07-24 13:01:39 +02:00
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for log in logs {
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ret
|
2023-07-23 12:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create(
|
|
|
|
db: &SqlitePool,
|
2023-07-24 13:01:39 +02:00
|
|
|
boat_id: i32,
|
2023-07-23 12:17:57 +02:00
|
|
|
shipmaster: i64,
|
|
|
|
shipmaster_only_steering: bool,
|
|
|
|
departure: NaiveDateTime,
|
|
|
|
arrival: Option<NaiveDateTime>,
|
|
|
|
destination: Option<String>,
|
|
|
|
distance_in_km: Option<i64>,
|
|
|
|
comments: Option<String>,
|
|
|
|
logtype: Option<i64>,
|
2023-07-24 13:01:39 +02:00
|
|
|
rower: Vec<i64>,
|
2023-07-23 16:49:14 +02:00
|
|
|
) -> Result<(), LogbookCreateError> {
|
2023-07-24 13:01:39 +02:00
|
|
|
let boat = match Boat::find_by_id(db, boat_id).await {
|
|
|
|
Some(b) => b,
|
|
|
|
None => {
|
|
|
|
return Err(LogbookCreateError::BoatNotFound);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if boat.is_locked(db).await {
|
|
|
|
return Err(LogbookCreateError::BoatLocked);
|
|
|
|
}
|
|
|
|
|
|
|
|
if boat.on_water(db).await {
|
|
|
|
return Err(LogbookCreateError::BoatAlreadyOnWater);
|
|
|
|
}
|
|
|
|
|
|
|
|
if rower.len() > boat.amount_seats as usize - 1 {
|
|
|
|
return Err(LogbookCreateError::TooManyRowers(
|
|
|
|
boat.amount_seats as usize,
|
|
|
|
rower.len() + 1,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let inserted_row = sqlx::query!(
|
|
|
|
"INSERT INTO logbook(boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype) VALUES (?,?,?,?,?,?,?,?,?) RETURNING id",
|
2023-07-23 12:17:57 +02:00
|
|
|
boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
|
|
|
|
)
|
2023-07-24 13:01:39 +02:00
|
|
|
.fetch_one(db)
|
|
|
|
.await.unwrap();
|
|
|
|
|
|
|
|
for rower in &rower {
|
|
|
|
Rower::create(db, inserted_row.id, *rower).await;
|
|
|
|
}
|
2023-07-23 16:49:14 +02:00
|
|
|
Ok(())
|
2023-07-23 12:17:57 +02:00
|
|
|
}
|
2023-07-23 16:49:14 +02:00
|
|
|
|
|
|
|
pub async fn home(
|
|
|
|
&self,
|
|
|
|
db: &SqlitePool,
|
|
|
|
user: &User,
|
|
|
|
destination: String,
|
|
|
|
distance_in_km: i64,
|
|
|
|
comments: Option<String>,
|
|
|
|
logtype: Option<i64>,
|
|
|
|
) -> Result<(), LogbookUpdateError> {
|
|
|
|
if user.id != self.shipmaster {
|
|
|
|
return Err(LogbookUpdateError::NotYourEntry);
|
|
|
|
}
|
|
|
|
//TODO: check current date
|
|
|
|
|
|
|
|
let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M"));
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE logbook SET destination=?, distance_in_km=?, comments=?, logtype=?, arrival=? WHERE id=?",
|
|
|
|
destination,
|
|
|
|
distance_in_km,
|
|
|
|
comments,
|
|
|
|
logtype,
|
|
|
|
arrival,
|
|
|
|
self.id
|
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await.unwrap(); //TODO: fixme
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:17:57 +02:00
|
|
|
// pub async fn delete(&self, db: &SqlitePool) {
|
|
|
|
// sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
|
|
|
|
// .execute(db)
|
|
|
|
// .await
|
|
|
|
// .unwrap(); //Okay, because we can only create a User of a valid id
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//#[cfg(test)]
|
|
|
|
//mod test {
|
|
|
|
// use crate::{model::boat::Boat, testdb};
|
|
|
|
//
|
|
|
|
// use sqlx::SqlitePool;
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_find_correct_id() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
// let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
|
|
|
// assert_eq!(boat.id, 1);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_find_wrong_id() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
// let boat = Boat::find_by_id(&pool, 1337).await;
|
|
|
|
// assert!(boat.is_none());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_all() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
// let res = Boat::all(&pool).await;
|
|
|
|
// assert!(res.len() > 3);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_succ_create() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
//
|
|
|
|
// assert_eq!(
|
|
|
|
// Boat::create(
|
|
|
|
// &pool,
|
|
|
|
// "new-boat-name".into(),
|
|
|
|
// 42,
|
|
|
|
// None,
|
|
|
|
// "Best Boatbuilder".into(),
|
|
|
|
// true,
|
|
|
|
// true,
|
|
|
|
// false,
|
|
|
|
// Some(1),
|
|
|
|
// None
|
|
|
|
// )
|
|
|
|
// .await,
|
|
|
|
// true
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_duplicate_name_create() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
//
|
|
|
|
// assert_eq!(
|
|
|
|
// Boat::create(
|
|
|
|
// &pool,
|
|
|
|
// "Haichenbach".into(),
|
|
|
|
// 42,
|
|
|
|
// None,
|
|
|
|
// "Best Boatbuilder".into(),
|
|
|
|
// true,
|
|
|
|
// true,
|
|
|
|
// false,
|
|
|
|
// Some(1),
|
|
|
|
// None
|
|
|
|
// )
|
|
|
|
// .await,
|
|
|
|
// false
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
//}
|