rowt/src/tera/boatdamage.rs

182 lines
4.9 KiB
Rust
Raw Normal View History

2023-08-02 14:29:19 +02:00
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;
2023-10-04 13:56:58 +02:00
use crate::{
model::{
boat::Boat,
boatdamage::{BoatDamage, BoatDamageFixed, BoatDamageToAdd, BoatDamageVerified},
2024-01-10 14:08:15 +01:00
user::{CoxUser, DonauLinzUser, TechUser, User, UserWithRoles},
2023-10-04 13:56:58 +02:00
},
tera::log::KioskCookie,
2023-08-02 14:29:19 +02:00
};
#[get("/")]
2023-10-04 13:56:58 +02:00
async fn index_kiosk(
db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>,
_kiosk: KioskCookie,
) -> Template {
let boatdamages = BoatDamage::all(db).await;
let boats = Boat::all(db).await;
2023-10-23 21:38:08 +02:00
let user = User::all(db).await;
2023-10-04 13:56:58 +02:00
let mut context = Context::new();
if let Some(msg) = flash {
context.insert("flash", &msg.into_inner());
}
context.insert("boatdamages", &boatdamages);
context.insert("boats", &boats);
2023-10-23 21:38:08 +02:00
context.insert("user", &user);
2023-10-23 21:05:59 +02:00
context.insert("show_kiosk_header", &true);
2023-10-04 13:56:58 +02:00
Template::render("boatdamages", context.into_json())
}
#[get("/", rank = 2)]
2023-10-24 10:57:51 +02:00
async fn index(
db: &State<SqlitePool>,
flash: Option<FlashMessage<'_>>,
2024-01-10 14:08:15 +01:00
user: DonauLinzUser,
2023-10-24 10:57:51 +02:00
) -> Template {
2023-08-02 14:29:19 +02:00
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",
2024-01-10 14:08:15 +01:00
&UserWithRoles::from_user(user.into(), db).await,
);
2023-08-02 14:29:19 +02:00
Template::render("boatdamages", context.into_json())
}
#[derive(FromForm)]
pub struct FormBoatDamageToAdd<'r> {
pub boat_id: i64,
pub desc: &'r str,
pub lock_boat: bool,
}
2023-10-04 13:56:58 +02:00
#[post("/", data = "<data>", rank = 2)]
2023-08-02 14:29:19 +02:00
async fn create<'r>(
db: &State<SqlitePool>,
data: Form<FormBoatDamageToAdd<'r>>,
2024-01-10 14:08:15 +01:00
user: DonauLinzUser,
2023-08-02 14:29:19 +02:00
) -> Flash<Redirect> {
2024-01-10 14:08:15 +01:00
let user: User = user.into();
2023-08-02 14:29:19 +02:00
let boatdamage_to_add = BoatDamageToAdd {
boat_id: data.boat_id,
desc: data.desc,
lock_boat: data.lock_boat,
2024-01-10 14:08:15 +01:00
user_id_created: user.id as i32,
2023-08-02 14:29:19 +02:00
};
match BoatDamage::create(db, boatdamage_to_add).await {
Ok(_) => Flash::success(
Redirect::to("/boatdamage"),
2023-10-04 13:56:58 +02:00
"Bootsschaden erfolgreich hinzugefügt",
),
Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Fehler: {e}")),
}
}
#[derive(FromForm)]
pub struct FormBoatDamageToAddKiosk<'r> {
pub boat_id: i64,
pub desc: &'r str,
pub lock_boat: bool,
pub user_id: i32,
}
#[post("/", data = "<data>")]
async fn create_from_kiosk<'r>(
db: &State<SqlitePool>,
data: Form<FormBoatDamageToAddKiosk<'r>>,
_kiosk: KioskCookie,
) -> Flash<Redirect> {
let boatdamage_to_add = BoatDamageToAdd {
boat_id: data.boat_id,
desc: data.desc,
lock_boat: data.lock_boat,
user_id_created: data.user_id,
};
match BoatDamage::create(db, boatdamage_to_add).await {
Ok(_) => Flash::success(
Redirect::to("/boatdamage"),
"Bootsschaden erfolgreich hinzugefügt",
2023-08-02 14:29:19 +02:00
),
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 {
2023-10-23 22:52:11 +02:00
Ok(_) => Flash::success(Redirect::to("/boatdamage"), "Bootsschaden behoben."),
2023-08-02 14:29:19 +02:00
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 {
2023-10-24 09:14:11 +02:00
Ok(_) => Flash::success(Redirect::to("/boatdamage"), "Bootsschaden verifiziert"),
2023-08-02 14:29:19 +02:00
Err(e) => Flash::error(Redirect::to("/boatdamage"), format!("Error: {e}")),
}
}
pub fn routes() -> Vec<Route> {
2023-10-04 13:56:58 +02:00
routes![
index,
index_kiosk,
create,
fixed,
verified,
create_from_kiosk
]
2023-08-02 14:29:19 +02:00
}