rowt/src/tera/log.rs

101 lines
3.0 KiB
Rust
Raw Normal View History

2023-07-23 12:17:57 +02:00
use rocket::{
form::Form,
get, post,
request::FlashMessage,
response::{Flash, Redirect},
2023-07-25 13:22:11 +02:00
routes, Route, State,
2023-07-23 12:17:57 +02:00
};
use rocket_dyn_templates::Template;
use sqlx::SqlitePool;
use tera::Context;
use crate::model::{
boat::Boat,
2023-07-25 13:22:11 +02:00
logbook::{LogToAdd, LogToFinalize, 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;
2023-07-26 12:56:19 +02:00
let distances = Logbook::distances(db).await;
2023-07-23 12:17:57 +02:00
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);
2023-07-26 12:56:19 +02:00
context.insert("distances", &distances);
2023-07-23 12:17:57 +02:00
Template::render("log", context.into_json())
}
#[post("/", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
2023-07-25 13:22:11 +02:00
data: Form<LogToAdd>,
2023-07-23 12:17:57 +02:00
_adminuser: AdminUser,
) -> Flash<Redirect> {
match Logbook::create(
2023-07-23 12:17:57 +02:00
db,
2023-07-25 13:22:11 +02:00
data.into_inner()
2023-07-23 12:17:57 +02:00
)
.await
{
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)")),
}
}
#[post("/<logbook_id>", data = "<data>")]
async fn home(
db: &State<SqlitePool>,
2023-07-25 13:22:11 +02:00
data: Form<LogToFinalize>,
logbook_id: i32,
2023-07-25 13:32:20 +02:00
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),
)
};
2023-07-25 13:32:20 +02:00
match logbook.home(db, &adminuser.user, data.into_inner()).await {
2023-07-24 13:01:39 +02:00
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),
2023-07-24 13:01:39 +02:00
),
2023-07-23 12:17:57 +02:00
}
}
pub fn routes() -> Vec<Route> {
routes![index, create, home]
2023-07-23 12:17:57 +02:00
}
#[cfg(test)]
mod test {}