use rocket::{ form::Form, get, post, request::FlashMessage, response::{Flash, Redirect}, routes, Route, State, }; use rocket_dyn_templates::Template; use sqlx::SqlitePool; use tera::Context; use crate::model::{ boat::Boat, logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError}, logtype::LogType, user::{AdminUser, User}, }; #[get("/")] async fn index( db: &State, flash: Option>, adminuser: AdminUser, ) -> Template { let boats = Boat::all(db).await; let coxes = User::cox(db).await; let users = User::all(db).await; 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); context.insert("coxes", &coxes); 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()) } #[post("/", data = "")] async fn create( db: &State, data: Form, _adminuser: AdminUser, ) -> Flash { match Logbook::create( db, data.into_inner() ) .await { Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"), 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)")), } } #[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.into_inner()).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, home] } #[cfg(test)] mod test {}