From 61261c981641279fe132cb666203cae77500e03b Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 23 Apr 2024 22:23:24 +0200 Subject: [PATCH 1/2] allow edits of boatreservations, Fixes #417 --- src/model/boatreservation.rs | 15 +++++++ src/tera/boatreservation.rs | 58 ++++++++++++++++++++++++++-- src/tera/mod.rs | 2 +- templates/boatreservations.html.tera | 34 +++++++++++----- 4 files changed, 96 insertions(+), 13 deletions(-) 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 @@ -- 2.45.2 From d9885f9bba5f9377ba1df6ddce4c34b536b8df2b Mon Sep 17 00:00:00 2001 From: Marie Birner Date: Tue, 23 Apr 2024 22:58:48 +0200 Subject: [PATCH 2/2] [TASK] change padding form --- templates/boatreservations.html.tera | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/boatreservations.html.tera b/templates/boatreservations.html.tera index 0cdb8f3..ffd0b7d 100644 --- a/templates/boatreservations.html.tera +++ b/templates/boatreservations.html.tera @@ -74,7 +74,7 @@ {% if allowed_to_edit %}
+ class="bg-white dark:bg-primary-900 pt-3 rounded-md w-full">
{{ macros::input(label='Uhrzeit', name='time_desc', id=loop.index, type="text", value=reservation.time_desc, readonly=false) }} -- 2.45.2