diff --git a/src/model/boatreservation.rs b/src/model/boatreservation.rs index 12db94e..804d384 100644 --- a/src/model/boatreservation.rs +++ b/src/model/boatreservation.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use crate::model::{boat::Boat, user::User}; +use crate::tera::boatreservation::ReservationEditForm; use chrono::NaiveDate; use chrono::NaiveDateTime; use rocket::serde::{Deserialize, Serialize}; @@ -204,6 +205,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 06d42b3..2aeda0a 100644 --- a/src/tera/boatreservation.rs +++ b/src/tera/boatreservation.rs @@ -14,7 +14,8 @@ use crate::{ model::{ boat::Boat, boatreservation::{BoatReservation, BoatReservationToAdd}, - user::{DonauLinzUser, User, UserWithRolesAndNotificationCount}, + 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 fece01b..33bbf53 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..ffd0b7d 100644 --- a/templates/boatreservations.html.tera +++ b/templates/boatreservations.html.tera @@ -18,7 +18,7 @@