add functionality to finish logbook trips
This commit is contained in:
@ -2,6 +2,8 @@ use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, SqlitePool};
|
||||
|
||||
use super::user::User;
|
||||
|
||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||
pub struct Logbook {
|
||||
pub id: i64,
|
||||
@ -34,22 +36,31 @@ pub struct LogbookWithBoatAndUsers {
|
||||
pub shipmaster_name: String,
|
||||
}
|
||||
|
||||
pub enum LogbookUpdateError {
|
||||
NotYourEntry,
|
||||
}
|
||||
|
||||
pub enum LogbookCreateError {
|
||||
BoatAlreadyOnWater,
|
||||
BoatLocked
|
||||
}
|
||||
|
||||
impl Logbook {
|
||||
//pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||||
// sqlx::query_as!(
|
||||
// Self,
|
||||
// "
|
||||
//SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external
|
||||
//FROM boat
|
||||
//WHERE id like ?
|
||||
// ",
|
||||
// id
|
||||
// )
|
||||
// .fetch_one(db)
|
||||
// .await
|
||||
// .ok()
|
||||
//}
|
||||
//
|
||||
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()
|
||||
}
|
||||
|
||||
// pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
|
||||
// sqlx::query_as!(
|
||||
// User,
|
||||
@ -110,46 +121,48 @@ impl Logbook {
|
||||
distance_in_km: Option<i64>,
|
||||
comments: Option<String>,
|
||||
logtype: Option<i64>,
|
||||
) -> bool {
|
||||
) -> Result<(), LogbookCreateError> {
|
||||
//Check if boat is not locked
|
||||
//Check if boat is already on water
|
||||
sqlx::query!(
|
||||
"INSERT INTO logbook(boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype) VALUES (?,?,?,?,?,?,?,?,?)",
|
||||
boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
|
||||
)
|
||||
.execute(db)
|
||||
.await.is_ok()
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
//
|
||||
// pub async fn update(
|
||||
// &self,
|
||||
// db: &SqlitePool,
|
||||
// name: &str,
|
||||
// amount_seats: i64,
|
||||
// year_built: Option<i64>,
|
||||
// boatbuilder: Option<&str>,
|
||||
// default_shipmaster_only_steering: bool,
|
||||
// skull: bool,
|
||||
// external: bool,
|
||||
// location_id: Option<i64>,
|
||||
// owner: Option<i64>,
|
||||
// ) -> bool {
|
||||
// sqlx::query!(
|
||||
// "UPDATE boat SET name=?, amount_seats=?, year_built=?, boatbuilder=?, default_shipmaster_only_steering=?, skull=?, external=?, location_id=?, owner=? WHERE id=?",
|
||||
// name,
|
||||
// amount_seats,
|
||||
// year_built,
|
||||
// boatbuilder,
|
||||
// default_shipmaster_only_steering,
|
||||
// skull,
|
||||
// external,
|
||||
// location_id,
|
||||
// owner,
|
||||
// self.id
|
||||
// )
|
||||
// .execute(db)
|
||||
// .await
|
||||
// .is_ok()
|
||||
// }
|
||||
//
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
// pub async fn delete(&self, db: &SqlitePool) {
|
||||
// sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
|
||||
// .execute(db)
|
||||
|
Reference in New Issue
Block a user