clean code with clippy
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
use crate::model::{
|
||||
boat::Boat,
|
||||
boat::{Boat, BoatToAdd, BoatToUpdate},
|
||||
location::Location,
|
||||
user::{AdminUser, User},
|
||||
};
|
||||
@ -8,7 +8,7 @@ use rocket::{
|
||||
get, post,
|
||||
request::FlashMessage,
|
||||
response::{Flash, Redirect},
|
||||
routes, FromForm, Route, State,
|
||||
routes, Route, State,
|
||||
};
|
||||
use rocket_dyn_templates::{tera::Context, Template};
|
||||
use sqlx::SqlitePool;
|
||||
@ -50,96 +50,39 @@ async fn delete(db: &State<SqlitePool>, _admin: AdminUser, boat: i32) -> Flash<R
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct BoatEditForm<'r> {
|
||||
id: i32,
|
||||
name: &'r str,
|
||||
amount_seats: i64,
|
||||
year_built: Option<i64>,
|
||||
boatbuilder: Option<&'r str>,
|
||||
default_shipmaster_only_steering: bool,
|
||||
skull: bool,
|
||||
external: bool,
|
||||
location_id: Option<i64>,
|
||||
owner: Option<i64>,
|
||||
}
|
||||
|
||||
#[post("/boat", data = "<data>")]
|
||||
async fn update(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<BoatEditForm<'_>>,
|
||||
data: Form<BoatToUpdate<'_>>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
let boat = Boat::find_by_id(db, data.id).await;
|
||||
let Some(boat) = boat else {
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Boat with ID {} does not exist!", data.id),
|
||||
"Boat does not exist!",
|
||||
)
|
||||
};
|
||||
|
||||
if !boat
|
||||
.update(
|
||||
db,
|
||||
data.name,
|
||||
data.amount_seats,
|
||||
data.year_built,
|
||||
data.boatbuilder,
|
||||
data.default_shipmaster_only_steering,
|
||||
data.skull,
|
||||
data.external,
|
||||
data.location_id,
|
||||
data.owner,
|
||||
)
|
||||
.await
|
||||
{
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Boat with ID {} could not be updated!", data.id),
|
||||
);
|
||||
if !boat.update(db, data.into_inner()).await {
|
||||
return Flash::error(Redirect::to("/admin/boat"), "Boat could not be updated!");
|
||||
}
|
||||
|
||||
Flash::success(Redirect::to("/admin/boat"), "Successfully updated boat")
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct BoatAddForm<'r> {
|
||||
name: &'r str,
|
||||
amount_seats: i64,
|
||||
year_built: Option<i64>,
|
||||
boatbuilder: Option<&'r str>,
|
||||
default_shipmaster_only_steering: bool,
|
||||
skull: bool,
|
||||
external: bool,
|
||||
location_id: Option<i64>,
|
||||
owner: Option<i64>,
|
||||
}
|
||||
|
||||
#[post("/boat/new", data = "<data>")]
|
||||
async fn create(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<BoatAddForm<'_>>,
|
||||
data: Form<BoatToAdd<'_>>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
if Boat::create(
|
||||
db,
|
||||
data.name,
|
||||
data.amount_seats,
|
||||
data.year_built,
|
||||
data.boatbuilder,
|
||||
data.default_shipmaster_only_steering,
|
||||
data.skull,
|
||||
data.external,
|
||||
data.location_id,
|
||||
data.owner,
|
||||
)
|
||||
.await
|
||||
{
|
||||
if Boat::create(db, data.into_inner()).await {
|
||||
Flash::success(Redirect::to("/admin/boat"), "Successfully created boat")
|
||||
} else {
|
||||
Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Error while creating boat {} in DB", data.name),
|
||||
"Error while creating the boat in DB",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user