clean code with clippy

This commit is contained in:
2023-07-25 13:22:11 +02:00
parent 3765541674
commit a7789af713
10 changed files with 243 additions and 463 deletions

View File

@ -1,10 +1,9 @@
use chrono::NaiveDateTime;
use rocket::{
form::Form,
get, post,
request::FlashMessage,
response::{Flash, Redirect},
routes, FromForm, Route, State,
routes, Route, State,
};
use rocket_dyn_templates::Template;
use sqlx::SqlitePool;
@ -12,7 +11,7 @@ use tera::Context;
use crate::model::{
boat::Boat,
logbook::{Logbook, LogbookCreateError},
logbook::{LogToAdd, LogToFinalize, Logbook, LogbookCreateError},
logtype::LogType,
user::{AdminUser, User},
};
@ -47,40 +46,15 @@ async fn index(
Template::render("log", context.into_json())
}
#[derive(FromForm)]
struct LogAddForm {
boat_id: i32,
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>,
rower: Vec<i64>,
}
#[post("/", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<LogAddForm>,
data: Form<LogToAdd>,
_adminuser: AdminUser,
) -> Flash<Redirect> {
match 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,
data.rower.clone(), //TODO: fix
data.into_inner()
)
.await
{
@ -92,19 +66,10 @@ async fn create(
}
}
#[derive(FromForm)]
struct LogHomeForm {
destination: String,
distance_in_km: i64,
comments: Option<String>,
logtype: Option<i64>,
rower: Vec<i64>,
}
#[post("/<logbook_id>", data = "<data>")]
async fn home(
db: &State<SqlitePool>,
data: Form<LogHomeForm>,
data: Form<LogToFinalize>,
logbook_id: i32,
_adminuser: AdminUser,
) -> Flash<Redirect> {
@ -116,18 +81,7 @@ async fn home(
)
};
match logbook
.home(
db,
&_adminuser.user,
data.destination.clone(), //TODO: fixme
data.distance_in_km,
data.comments.clone(), //TODO: fixme
data.logtype,
data.rower.clone(), //TODO: fixme
)
.await
{
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"),