diff --git a/README.md b/README.md index 0e33590..6aa96d8 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,7 @@ ## New large features ### Logbuch -Next: -- Start with CREATE for logbook +- Next: add rower to logbook ### Guest-Scheckbuch - guest_trip diff --git a/src/model/logbook.rs b/src/model/logbook.rs index 2a5a207..0884be3 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -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 { - // 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 { + 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 { // sqlx::query_as!( // User, @@ -110,46 +121,48 @@ impl Logbook { distance_in_km: Option, comments: Option, logtype: Option, - ) -> 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, - // boatbuilder: Option<&str>, - // default_shipmaster_only_steering: bool, - // skull: bool, - // external: bool, - // location_id: Option, - // owner: Option, - // ) -> 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, + logtype: Option, + ) -> 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) diff --git a/src/tera/log.rs b/src/tera/log.rs index dc2bcd8..4a93341 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -64,7 +64,7 @@ async fn create( data: Form, _adminuser: AdminUser, ) -> Flash { - if Logbook::create( + match Logbook::create( db, data.boat_id, data.shipmaster, @@ -80,14 +80,61 @@ async fn create( ) .await { - Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt") - } else { - Flash::error(Redirect::to("/log"), format!("Fehler beim hinzufügen!")) - } + Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"), + Err(_) => Flash::error(Redirect::to("/log"), format!("Fehler beim hinzufügen!")) + } + + } +#[derive(FromForm)] +struct LogHomeForm { + destination: String, + distance_in_km: i64, + comments: Option, + logtype: Option, +} + +#[post("/", data = "")] +async fn home( + db: &State, + data: Form, + logbook_id: i32, + _adminuser: AdminUser, +) -> Flash { + let logbook = Logbook::find_by_id(db, logbook_id).await; + let Some(logbook) = logbook else { + return Flash::error( + Redirect::to("/admin/log"), + format!("Log with ID {} does not exist!", logbook_id), + ) + }; + + match logbook + .home( + db, + &_adminuser.user, + data.destination.clone(), //TODO: fixme + data.distance_in_km, + data.comments.clone(), //TODO: fixme + data.logtype + ) + .await + { + Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"), + Err(_) => + Flash::error( + Redirect::to("/log"), + format!("Logbook with ID {} could not be updated!", logbook_id), + ) + } + +} + + + pub fn routes() -> Vec { - routes![index, create] + routes![index, create, home] } #[cfg(test)] diff --git a/templates/auth/login.html.tera b/templates/auth/login.html.tera index a099df4..02fbdca 100644 --- a/templates/auth/login.html.tera +++ b/templates/auth/login.html.tera @@ -15,9 +15,9 @@
{% if name %} - {{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true,value=name) }} + {{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true,value=name, autofocus=true) }} {% else %} - {{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true) }} + {{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true, autofocus=true) }} {% endif %}
diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index e1ab708..f2a9a67 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -38,10 +38,10 @@
{% endmacro header %} -{% macro input(label, name, type, required=false, class='rounded-md', value='', min='', hide_label=false) %} +{% macro input(label, name, type, required=false, class='rounded-md', value='', min='', hide_label=false, id='', autofocus=false) %}
- +
{% endmacro input %} diff --git a/templates/log.html.tera b/templates/log.html.tera index e2347a9..0d92f0a 100644 --- a/templates/log.html.tera +++ b/templates/log.html.tera @@ -32,7 +32,7 @@