diff --git a/src/model/boatreservation.rs b/src/model/boatreservation.rs index 311f966..bacddea 100644 --- a/src/model/boatreservation.rs +++ b/src/model/boatreservation.rs @@ -1,4 +1,5 @@ use crate::model::{boat::Boat, user::User}; +use crate::tera::boatreservation::ReservationEditForm; use chrono::NaiveDate; use chrono::NaiveDateTime; use rocket::serde::{Deserialize, Serialize}; @@ -177,6 +178,20 @@ AND start_date <= ? AND end_date >= ?;", > 0 } + pub async fn update(&self, db: &SqlitePool, data: ReservationEditForm) { + let time_desc = data.time_desc.trim(); + let usage = data.usage.trim(); + sqlx::query!( + "UPDATE boat_reservation SET time_desc = ?, usage = ? where id = ?", + time_desc, + usage, + self.id + ) + .execute(db) + .await + .unwrap(); //Okay, because we can only create a User of a valid id + } + pub async fn delete(&self, db: &SqlitePool) { sqlx::query!("DELETE FROM boat_reservation WHERE id=?", self.id) .execute(db) diff --git a/src/tera/boatreservation.rs b/src/tera/boatreservation.rs index 0cc5574..5205fff 100644 --- a/src/tera/boatreservation.rs +++ b/src/tera/boatreservation.rs @@ -14,6 +14,7 @@ use crate::{ model::{ boat::Boat, boatreservation::{BoatReservation, BoatReservationToAdd}, + log::Log, user::{DonauLinzUser, User, UserWithRoles}, }, tera::log::KioskCookie, @@ -90,7 +91,7 @@ pub struct FormBoatReservationToAdd<'r> { pub user_id_applicant: Option, } -#[post("/", data = "", rank = 2)] +#[post("/new", data = "", rank = 2)] async fn create<'r>( db: &State, data: Form>, @@ -115,7 +116,7 @@ async fn create<'r>( } } -#[post("/", data = "")] +#[post("/new", data = "")] async fn create_from_kiosk<'r>( db: &State, data: Form>, @@ -142,6 +143,50 @@ async fn create_from_kiosk<'r>( } } +#[derive(FromForm, Debug)] +pub struct ReservationEditForm { + pub(crate) id: i32, + pub(crate) time_desc: String, + pub(crate) usage: String, +} + +#[post("/", data = "")] +async fn update( + db: &State, + data: Form, + user: User, +) -> Flash { + let Some(reservation) = BoatReservation::find_by_id(db, data.id).await else { + return Flash::error( + Redirect::to("/boatreservation"), + format!("Reservation with ID {} does not exist!", data.id), + ); + }; + + if user.id != reservation.user_id_applicant && !user.has_role(db, "admin").await { + return Flash::error( + Redirect::to("/boatreservation"), + format!("Not allowed to update reservation (only admins + creator do so)."), + ); + } + + Log::create( + db, + format!( + "{} updated reservation from {reservation:?} to {data:?}", + user.name + ), + ) + .await; + + reservation.update(db, data.into_inner()).await; + + Flash::success( + Redirect::to("/boatreservation"), + "Reservierung erfolgreich bearbeitet", + ) +} + #[get("//delete")] async fn delete<'r>( db: &State, @@ -167,5 +212,12 @@ async fn delete<'r>( } pub fn routes() -> Vec { - routes![index, index_kiosk, create, create_from_kiosk, delete] + routes![ + index, + index_kiosk, + create, + create_from_kiosk, + delete, + update + ] } diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 5f89c2b..0ee0bf8 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -30,7 +30,7 @@ pub(crate) mod admin; mod auth; pub(crate) mod board; mod boatdamage; -mod boatreservation; +pub(crate) mod boatreservation; mod cox; mod ergo; mod log; diff --git a/templates/boatreservations.html.tera b/templates/boatreservations.html.tera index 6ddb6cd..0cdb8f3 100644 --- a/templates/boatreservations.html.tera +++ b/templates/boatreservations.html.tera @@ -18,7 +18,7 @@