use chrono::{Local, NaiveDateTime, TimeZone}; use rocket::FromForm; use serde::Serialize; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use super::{boat::Boat, rower::Rower, user::User}; #[derive(FromRow, Serialize, Clone, Debug)] pub struct Logbook { pub id: i64, pub boat_id: i64, pub shipmaster: i64, #[serde(default = "bool::default")] pub shipmaster_only_steering: bool, pub departure: NaiveDateTime, pub arrival: Option, pub destination: Option, pub distance_in_km: Option, pub comments: Option, pub logtype: Option, } impl PartialEq for Logbook { fn eq(&self, other: &Self) -> bool { self.id == other.id } } #[derive(FromForm, Debug)] pub struct LogToAdd { pub boat_id: i32, pub shipmaster: i64, pub shipmaster_only_steering: bool, pub departure: String, pub arrival: Option, pub destination: Option, pub distance_in_km: Option, pub comments: Option, pub logtype: Option, pub rowers: Vec, } #[derive(FromForm)] pub struct LogToFinalize { pub destination: String, pub distance_in_km: i64, pub comments: Option, pub logtype: Option, pub rowers: Vec, } #[derive(Serialize, Debug)] pub struct LogbookWithBoatAndRowers { #[serde(flatten)] pub logbook: Logbook, pub boat: Boat, pub shipmaster_user: User, pub rowers: Vec, pub departure_timestamp: i64, pub arrival_timestamp: Option, } #[derive(Debug, PartialEq)] pub enum LogbookUpdateError { NotYourEntry, TooManyRowers(usize, usize), RowerCreateError(i64, String), } #[derive(Debug, PartialEq)] pub enum LogbookCreateError { BoatAlreadyOnWater, BoatLocked, BoatNotFound, TooManyRowers(usize, usize), ShipmasterAlreadyOnWater, RowerAlreadyOnWater(User), RowerCreateError(i64, String), SamePersonShipmasterAndRower, } impl Logbook { pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option { 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 on_water(db: &SqlitePool) -> Vec { let rows = sqlx::query!( " SELECT id, boat_id, shipmaster, shipmaster_only_steering, strftime('%Y-%m-%d %H:%M', departure) as departure, arrival, destination, distance_in_km, comments, logtype FROM logbook WHERE arrival is null ORDER BY departure DESC " ) .fetch_all(db) .await .unwrap(); //TODO: fixme let logs: Vec = rows .into_iter() .map(|row| Logbook { id: row.id, boat_id: row.boat_id, shipmaster: row.shipmaster, shipmaster_only_steering: row.shipmaster_only_steering, departure: NaiveDateTime::parse_from_str(&row.departure.unwrap(), "%Y-%m-%d %H:%M") .unwrap(), arrival: row.arrival, destination: row.destination, distance_in_km: row.distance_in_km, comments: row.comments, logtype: row.logtype, }) .collect(); let mut ret = Vec::new(); for log in logs { let date_time = Local.from_local_datetime(&log.departure).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 } pub async fn completed(db: &SqlitePool) -> Vec { let logs = sqlx::query_as!( Logbook, " SELECT id, boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype FROM logbook WHERE arrival is not null ORDER BY departure DESC " ) .fetch_all(db) .await .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, arrival_timestamp: None, departure_timestamp: 0, }); } ret } pub async fn create(db: &SqlitePool, log: LogToAdd) -> Result<(), LogbookCreateError> { let Some(boat) = Boat::find_by_id(db, log.boat_id).await else { 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 (User::find_by_id(db, log.shipmaster as i32).await.unwrap()) .on_water(db) .await { return Err(LogbookCreateError::ShipmasterAlreadyOnWater); } if log.rowers.len() > boat.amount_seats as usize - 1 { return Err(LogbookCreateError::TooManyRowers( boat.amount_seats as usize, log.rowers.len() + 1, )); } for rower in &log.rowers { let user = User::find_by_id(db, *rower as i32).await.unwrap(); if *rower == log.shipmaster { return Err(LogbookCreateError::SamePersonShipmasterAndRower); } if user.on_water(db).await { return Err(LogbookCreateError::RowerAlreadyOnWater(user)); } } let mut tx = db.begin().await.unwrap(); let departure = NaiveDateTime::parse_from_str(&log.departure, "%Y-%m-%dT%H:%M").unwrap(); let arrival = log .arrival .map(|a| NaiveDateTime::parse_from_str(&a, "%Y-%m-%dT%H:%M").unwrap()); 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", log.boat_id, log.shipmaster, log.shipmaster_only_steering, departure, arrival, log.destination, log.distance_in_km, log.comments, log.logtype ) .fetch_one(&mut tx) .await.unwrap(); for rower in &log.rowers { Rower::create(&mut tx, inserted_row.id, *rower) .await .map_err(|e| LogbookCreateError::RowerCreateError(*rower, e.to_string()))?; } tx.commit().await.unwrap(); Ok(()) } 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() } async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) { sqlx::query!("DELETE FROM rower WHERE logbook_id=?", self.id) .execute(db) .await .unwrap(); } pub async fn home( &self, db: &SqlitePool, user: &User, log: LogToFinalize, ) -> Result<(), LogbookUpdateError> { if user.id != self.shipmaster { return Err(LogbookUpdateError::NotYourEntry); } let boat = Boat::find_by_id(db, self.boat_id as i32).await.unwrap(); //ok if log.rowers.len() > boat.amount_seats as usize - 1 { return Err(LogbookUpdateError::TooManyRowers( boat.amount_seats as usize, log.rowers.len() + 1, )); } let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M")); let mut tx = db.begin().await.unwrap(); sqlx::query!( "UPDATE logbook SET destination=?, distance_in_km=?, comments=?, logtype=?, arrival=? WHERE id=?", log.destination, log.distance_in_km, log.comments, log.logtype, arrival, self.id ) .execute(&mut tx) .await.unwrap(); //TODO: fixme self.remove_rowers(&mut tx).await; for rower in &log.rowers { Rower::create(&mut tx, self.id, *rower) .await .map_err(|e| LogbookUpdateError::RowerCreateError(*rower, e.to_string()))?; } tx.commit().await.unwrap(); Ok(()) } pub async fn delete(&self, db: &SqlitePool) { sqlx::query!("DELETE FROM logbook WHERE id=?", self.id) .execute(db) .await .unwrap(); //Okay, because we can only create a Logbook of a valid id } } #[cfg(test)] mod test { use super::{LogToAdd, Logbook, LogbookCreateError, LogbookUpdateError}; use crate::model::user::User; use crate::testdb; use sqlx::SqlitePool; #[sqlx::test] fn test_find_correct_id() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); assert_eq!(logbook.id, 1); } #[sqlx::test] fn test_find_wrong_id() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1337).await; assert_eq!(logbook, None); } #[sqlx::test] fn test_on_water() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); let logbook_with_details = Logbook::on_water(&pool).await; assert_eq!(logbook_with_details[0].logbook, logbook); } #[sqlx::test] fn test_completed() { let pool = testdb!(); let completed = Logbook::completed(&pool).await; assert_eq!( completed[0].logbook, Logbook::find_by_id(&pool, 3).await.unwrap() ); assert_eq!( completed[1].logbook, Logbook::find_by_id(&pool, 2).await.unwrap() ); } //#[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!(); Logbook::create( &pool, LogToAdd { boat_id: 3, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: Vec::new(), }, ) .await .unwrap() } #[sqlx::test] fn test_create_boat_not_found() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 999, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: Vec::new(), }, ) .await; assert_eq!(res, Err(LogbookCreateError::BoatNotFound)); } #[sqlx::test] fn test_create_boat_locked() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 5, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: Vec::new(), }, ) .await; assert_eq!(res, Err(LogbookCreateError::BoatLocked)); } #[sqlx::test] fn test_create_boat_on_water() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 2, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: Vec::new(), }, ) .await; assert_eq!(res, Err(LogbookCreateError::BoatAlreadyOnWater)); } #[sqlx::test] fn test_create_shipmaster_on_water() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 3, shipmaster: 2, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: Vec::new(), }, ) .await; assert_eq!(res, Err(LogbookCreateError::ShipmasterAlreadyOnWater)); } #[sqlx::test] fn test_create_same_person_cox_and_rower() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 3, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: vec![5], }, ) .await; assert_eq!(res, Err(LogbookCreateError::SamePersonShipmasterAndRower)); } #[sqlx::test] fn test_create_too_many_rowers() { let pool = testdb!(); let res = Logbook::create( &pool, LogToAdd { boat_id: 1, shipmaster: 5, shipmaster_only_steering: false, departure: "2128-05-20T12:00".into(), arrival: None, destination: None, distance_in_km: None, comments: None, logtype: None, rowers: vec![1], }, ) .await; assert_eq!(res, Err(LogbookCreateError::TooManyRowers(1, 2))); } #[sqlx::test] fn test_distances() { let pool = testdb!(); let res = Logbook::distances(&pool).await; assert_eq!( res, vec![ ("Ottensheim".into(), 25 as i64), ("Ottensheim + Regattastrecke".into(), 29 as i64), ] ); } #[sqlx::test] fn test_succ_home() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); let user = User::find_by_id(&pool, 2).await.unwrap(); logbook .home( &pool, &user, super::LogToFinalize { destination: "new-destination".into(), distance_in_km: 42, comments: Some("Perfect water".into()), logtype: None, rowers: vec![], }, ) .await .unwrap(); } #[sqlx::test] fn test_home_wrong_user() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); let user = User::find_by_id(&pool, 1).await.unwrap(); let res = logbook .home( &pool, &user, super::LogToFinalize { destination: "new-destination".into(), distance_in_km: 42, comments: Some("Perfect water".into()), logtype: None, rowers: vec![], }, ) .await; assert_eq!(res, Err(LogbookUpdateError::NotYourEntry)); } #[sqlx::test] fn test_home_too_many_rower() { let pool = testdb!(); let logbook = Logbook::find_by_id(&pool, 1).await.unwrap(); let user = User::find_by_id(&pool, 2).await.unwrap(); let res = logbook .home( &pool, &user, super::LogToFinalize { destination: "new-destination".into(), distance_in_km: 42, comments: Some("Perfect water".into()), logtype: None, rowers: vec![1], }, ) .await; assert_eq!(res, Err(LogbookUpdateError::TooManyRowers(1, 2))); } }