add first draft of logbook

This commit is contained in:
2023-07-23 12:17:57 +02:00
parent f09454fb38
commit 1d4c5f356d
10 changed files with 507 additions and 3 deletions

94
src/tera/log.rs Normal file
View File

@ -0,0 +1,94 @@
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,
logbook::Logbook,
logtype::LogType,
user::{AdminUser, User},
};
#[get("/")]
async fn index(
db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>,
adminuser: AdminUser,
) -> Template {
let boats = Boat::all(db).await;
let users = User::cox(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("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 {
boat_id: i64,
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>,
}
#[post("/", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<LogAddForm>,
_adminuser: AdminUser,
) -> Flash<Redirect> {
if Logbook::create(
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,
)
.await
{
Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt")
} else {
Flash::error(Redirect::to("/log"), format!("Fehler beim hinzufügen!"))
}
}
pub fn routes() -> Vec<Route> {
routes![index, create]
}
#[cfg(test)]
mod test {}

View File

@ -22,6 +22,7 @@ use crate::model::{
mod admin;
mod auth;
mod cox;
mod log;
mod misc;
#[get("/")]
@ -114,6 +115,7 @@ pub fn config(rocket: Rocket<Build>) -> Rocket<Build> {
rocket
.mount("/", routes![index, join, remove])
.mount("/auth", auth::routes())
.mount("/log", log::routes())
.mount("/cox", cox::routes())
.mount("/admin", admin::routes())
.mount("/", misc::routes())