add boatdamages functionality
This commit is contained in:
@ -63,6 +63,7 @@ struct UserEditForm {
|
||||
is_guest: bool,
|
||||
is_cox: bool,
|
||||
is_admin: bool,
|
||||
is_tech: bool,
|
||||
}
|
||||
|
||||
#[post("/user", data = "<data>")]
|
||||
@ -73,13 +74,13 @@ async fn update(
|
||||
) -> Flash<Redirect> {
|
||||
let user = User::find_by_id(db, data.id).await;
|
||||
let Some(user) = user else {
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/user"),
|
||||
format!("User with ID {} does not exist!", data.id),
|
||||
)
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/user"),
|
||||
format!("User with ID {} does not exist!", data.id),
|
||||
);
|
||||
};
|
||||
|
||||
user.update(db, data.is_cox, data.is_admin, data.is_guest)
|
||||
user.update(db, data.is_cox, data.is_admin, data.is_guest, data.is_tech)
|
||||
.await;
|
||||
|
||||
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
||||
|
114
src/tera/boatdamage.rs
Normal file
114
src/tera/boatdamage.rs
Normal file
@ -0,0 +1,114 @@
|
||||
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,
|
||||
boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified},
|
||||
user::{CoxUser, TechUser, User},
|
||||
};
|
||||
|
||||
#[get("/")]
|
||||
async fn index(db: &State<SqlitePool>, flash: Option<FlashMessage<'_>>, user: User) -> Template {
|
||||
let boatdamages = BoatDamage::all(db).await;
|
||||
let boats = Boat::all(db).await;
|
||||
|
||||
let mut context = Context::new();
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
|
||||
context.insert("boatdamages", &boatdamages);
|
||||
context.insert("boats", &boats);
|
||||
context.insert("loggedin_user", &user);
|
||||
|
||||
Template::render("boatdamages", context.into_json())
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct FormBoatDamageToAdd<'r> {
|
||||
pub boat_id: i64,
|
||||
pub desc: &'r str,
|
||||
pub lock_boat: bool,
|
||||
}
|
||||
|
||||
#[post("/", data = "<data>")]
|
||||
async fn create<'r>(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<FormBoatDamageToAdd<'r>>,
|
||||
coxuser: CoxUser,
|
||||
) -> Flash<Redirect> {
|
||||
let boatdamage_to_add = BoatDamageToAdd {
|
||||
boat_id: data.boat_id,
|
||||
desc: data.desc,
|
||||
lock_boat: data.lock_boat,
|
||||
user_id_created: coxuser.id as i32,
|
||||
};
|
||||
match BoatDamage::create(db, boatdamage_to_add).await {
|
||||
Ok(_) => Flash::success(
|
||||
Redirect::to("/boatdamage"),
|
||||
"Ausfahrt erfolgreich hinzugefügt",
|
||||
),
|
||||
Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Fehler: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct FormBoatDamageFixed<'r> {
|
||||
pub desc: &'r str,
|
||||
}
|
||||
|
||||
#[post("/<boatdamage_id>/fixed", data = "<data>")]
|
||||
async fn fixed<'r>(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<FormBoatDamageFixed<'r>>,
|
||||
boatdamage_id: i32,
|
||||
coxuser: CoxUser,
|
||||
) -> Flash<Redirect> {
|
||||
let boatdamage = BoatDamage::find_by_id(db, boatdamage_id).await.unwrap(); //TODO: Fix
|
||||
let boatdamage_fixed = BoatDamageFixed {
|
||||
desc: data.desc,
|
||||
user_id_fixed: coxuser.id as i32,
|
||||
};
|
||||
match boatdamage.fixed(db, boatdamage_fixed).await {
|
||||
Ok(_) => Flash::success(Redirect::to("/boatdamage"), "Successfully fixed the boat."),
|
||||
Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Error: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct FormBoatDamageVerified<'r> {
|
||||
pub desc: &'r str,
|
||||
}
|
||||
|
||||
#[post("/<boatdamage_id>/verified", data = "<data>")]
|
||||
async fn verified<'r>(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<FormBoatDamageFixed<'r>>,
|
||||
boatdamage_id: i32,
|
||||
techuser: TechUser,
|
||||
) -> Flash<Redirect> {
|
||||
let boatdamage = BoatDamage::find_by_id(db, boatdamage_id).await.unwrap(); //TODO: Fix
|
||||
let boatdamage_verified = BoatDamageVerified {
|
||||
desc: data.desc,
|
||||
user_id_verified: techuser.id as i32,
|
||||
};
|
||||
match boatdamage.verified(db, boatdamage_verified).await {
|
||||
Ok(_) => Flash::success(
|
||||
Redirect::to("/boatdamage"),
|
||||
"Successfully verified the boat.",
|
||||
),
|
||||
Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Error: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![index, create, fixed, verified]
|
||||
}
|
@ -21,6 +21,7 @@ use crate::model::{
|
||||
|
||||
mod admin;
|
||||
mod auth;
|
||||
mod boatdamage;
|
||||
mod cox;
|
||||
mod log;
|
||||
mod misc;
|
||||
@ -120,6 +121,7 @@ pub fn config(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
.mount("/auth", auth::routes())
|
||||
.mount("/log", log::routes())
|
||||
.mount("/stat", stat::routes())
|
||||
.mount("/boatdamage", boatdamage::routes())
|
||||
.mount("/cox", cox::routes())
|
||||
.mount("/admin", admin::routes())
|
||||
.mount("/", misc::routes())
|
||||
|
Reference in New Issue
Block a user