2023-07-23 12:17:57 +02:00
use chrono ::NaiveDateTime ;
use rocket ::{
form ::Form ,
get , post ,
request ::FlashMessage ,
response ::{ Flash , Redirect } ,
routes , FromForm , Route , State ,
} ;
use rocket_dyn_templates ::Template ;
use sqlx ::SqlitePool ;
use tera ::Context ;
use crate ::model ::{
boat ::Boat ,
2023-07-24 13:01:39 +02:00
logbook ::{ Logbook , LogbookCreateError } ,
2023-07-23 12:17:57 +02:00
logtype ::LogType ,
user ::{ AdminUser , User } ,
} ;
#[ get( " / " ) ]
async fn index (
db : & State < SqlitePool > ,
flash : Option < FlashMessage < '_ > > ,
adminuser : AdminUser ,
) -> Template {
let boats = Boat ::all ( db ) . await ;
2023-07-24 13:01:39 +02:00
let coxes = User ::cox ( db ) . await ;
let users = User ::all ( db ) . await ;
2023-07-23 12:17:57 +02:00
let logtypes = LogType ::all ( db ) . await ;
let on_water = Logbook ::on_water ( db ) . await ;
let completed = Logbook ::completed ( db ) . await ;
let mut context = Context ::new ( ) ;
if let Some ( msg ) = flash {
context . insert ( " flash " , & msg . into_inner ( ) ) ;
}
context . insert ( " boats " , & boats ) ;
2023-07-24 13:01:39 +02:00
context . insert ( " coxes " , & coxes ) ;
2023-07-23 12:17:57 +02:00
context . insert ( " users " , & users ) ;
context . insert ( " logtypes " , & logtypes ) ;
context . insert ( " loggedin_user " , & adminuser . user ) ;
context . insert ( " on_water " , & on_water ) ;
context . insert ( " completed " , & completed ) ;
Template ::render ( " log " , context . into_json ( ) )
}
#[ derive(FromForm) ]
struct LogAddForm {
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 : String ,
arrival : Option < String > ,
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 12:17:57 +02:00
}
#[ post( " / " , data = " <data> " ) ]
async fn create (
db : & State < SqlitePool > ,
data : Form < LogAddForm > ,
_adminuser : AdminUser ,
) -> Flash < Redirect > {
2023-07-23 16:49:14 +02:00
match Logbook ::create (
2023-07-23 12:17:57 +02:00
db ,
data . boat_id ,
data . shipmaster ,
data . shipmaster_only_steering ,
NaiveDateTime ::parse_from_str ( & data . departure , " %Y-%m-%dT%H:%M " ) . unwrap ( ) , //TODO: fix
data . arrival
. clone ( )
. map ( | a | NaiveDateTime ::parse_from_str ( & a , " %Y-%m-%dT%H:%M " ) . unwrap ( ) ) , //TODO: fix
data . destination . clone ( ) , //TODO: fix
data . distance_in_km ,
data . comments . clone ( ) , //TODO: fix
data . logtype ,
2023-07-24 13:01:39 +02:00
data . rower . clone ( ) , //TODO: fix
2023-07-23 12:17:57 +02:00
)
. await
{
2023-07-23 16:49:14 +02:00
Ok ( _ ) = > Flash ::success ( Redirect ::to ( " /log " ) , " Ausfahrt erfolgreich hinzugefügt " ) ,
2023-07-24 13:01:39 +02:00
Err ( LogbookCreateError ::BoatAlreadyOnWater ) = > Flash ::error ( Redirect ::to ( " /log " ) , format! ( " Boot schon am Wasser " ) ) ,
Err ( LogbookCreateError ::BoatLocked ) = > Flash ::error ( Redirect ::to ( " /log " ) , format! ( " Boot gesperrt " ) ) ,
Err ( LogbookCreateError ::BoatNotFound ) = > Flash ::error ( Redirect ::to ( " /log " ) , format! ( " Boot gibt's ned " ) ) ,
Err ( LogbookCreateError ::TooManyRowers ( expected , actual ) ) = > Flash ::error ( Redirect ::to ( " /log " ) , format! ( " Zu viele Ruderer (Boot fasst maximal {expected} , es wurden jedoch {actual} Ruderer ausgewählt) " ) ) ,
}
2023-07-23 16:49:14 +02:00
}
#[ derive(FromForm) ]
struct LogHomeForm {
destination : String ,
distance_in_km : i64 ,
comments : Option < String > ,
logtype : Option < i64 > ,
2023-07-24 20:56:46 +02:00
rower : Vec < i64 > ,
2023-07-23 16:49:14 +02:00
}
#[ post( " /<logbook_id> " , data = " <data> " ) ]
async fn home (
db : & State < SqlitePool > ,
data : Form < LogHomeForm > ,
logbook_id : i32 ,
_adminuser : AdminUser ,
) -> Flash < Redirect > {
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
2023-07-24 13:01:39 +02:00
data . logtype ,
2023-07-24 20:56:46 +02:00
data . rower . clone ( ) , //TODO: fixme
2023-07-23 16:49:14 +02:00
)
. await
{
2023-07-24 13:01:39 +02:00
Ok ( _ ) = > Flash ::success ( Redirect ::to ( " /log " ) , " Successfully updated log " ) ,
Err ( _ ) = > Flash ::error (
2023-07-23 16:49:14 +02:00
Redirect ::to ( " /log " ) ,
format! ( " Logbook with ID {} could not be updated! " , logbook_id ) ,
2023-07-24 13:01:39 +02:00
) ,
2023-07-23 12:17:57 +02:00
}
}
pub fn routes ( ) -> Vec < Route > {
2023-07-23 16:49:14 +02:00
routes! [ index , create , home ]
2023-07-23 12:17:57 +02:00
}
#[ cfg(test) ]
mod test { }