add edit option of planned_events

This commit is contained in:
philipp 2023-05-03 22:40:22 +02:00
parent 822680929a
commit 9f7fa6b8c4
3 changed files with 78 additions and 3 deletions

View File

@ -159,6 +159,34 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even
.unwrap(); //Okay, as TripDetails can only be created with proper DB backing .unwrap(); //Okay, as TripDetails can only be created with proper DB backing
} }
//TODO: create unit test
pub async fn update(
&self,
db: &SqlitePool,
planned_amount_cox: i32,
max_people: i32,
notes: Option<String>,
) {
sqlx::query!(
"UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?",
planned_amount_cox,
self.id
)
.execute(db)
.await
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
sqlx::query!(
"UPDATE trip_details SET max_people = ?, notes = ? WHERE id = ?",
planned_amount_cox,
notes,
self.trip_details_id
)
.execute(db)
.await
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
}
pub async fn delete(&self, db: &SqlitePool) { pub async fn delete(&self, db: &SqlitePool) {
sqlx::query!("DELETE FROM planned_event WHERE id = ?", self.id) sqlx::query!("DELETE FROM planned_event WHERE id = ?", self.id)
.execute(db) .execute(db)

View File

@ -1,6 +1,6 @@
use rocket::{ use rocket::{
form::Form, form::Form,
get, post, get, post, put,
response::{Flash, Redirect}, response::{Flash, Redirect},
routes, FromForm, Route, State, routes, FromForm, Route, State,
}; };
@ -49,6 +49,37 @@ async fn create(
Flash::success(Redirect::to("/"), "Successfully planned the event") Flash::success(Redirect::to("/"), "Successfully planned the event")
} }
//TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)]
struct UpdatePlannedEventForm {
id: i64,
planned_amount_cox: i32,
max_people: i32,
notes: Option<String>,
}
#[put("/planned-event", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<UpdatePlannedEventForm>,
_admin: AdminUser,
) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, data.id).await {
Some(planned_event) => {
planned_event
.update(
db,
data.planned_amount_cox,
data.max_people,
data.notes.clone(),
)
.await;
Flash::success(Redirect::to("/"), "Successfully edited the event")
}
None => Flash::error(Redirect::to("/"), "Planned event id not found"),
}
}
#[get("/planned-event/<id>/delete")] #[get("/planned-event/<id>/delete")]
async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> { async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, id).await { match PlannedEvent::find_by_id(db, id).await {
@ -61,5 +92,5 @@ async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Red
} }
pub fn routes() -> Vec<Route> { pub fn routes() -> Vec<Route> {
routes![create, delete] routes![create, delete, update]
} }

View File

@ -120,8 +120,24 @@
<div class="text-primary-900 bg-primary-50 text-center p-1 mb-4">Gäste sind erlaubt</div> <div class="text-primary-900 bg-primary-50 text-center p-1 mb-4">Gäste sind erlaubt</div>
{% endif %} {% endif %}
{# --- START Delete Btn --- #}
{% if loggedin_user.is_admin %} {% if loggedin_user.is_admin %}
{# --- START Edit Form --- #}
<div class="bg-gray-100 p-3 mt-4 rounded-md">
<h3 class="text-primary-950 font-bold uppercase tracking-wide mb-2">Ausfahrt bearbeiten</h3>
<form action="/admin/planned-event" method="post" class="grid gap-3">
<input type="hidden" name="_method" value="put" />
<input type="hidden" name="id" value="{{ planned_event.id }}" />
{{ macros::input(label='Anzahl Ruderer', name='max_people', type='number', required=true, value=planned_event.max_people, min='0') }}
{{ macros::input(label='Anzahl Steuerleute', name='planned_amount_cox', type='number', value=planned_event.planned_amount_cox, required=true, min='0') }}
{{ macros::input(label='Anmerkungen', name='notes', type='input', value=planned_event.notes) }}
<input value="Bearbeiten" class="btn btn-primary" type="submit" />
</form>
</div>
{# --- END Edit Form --- #}
{# --- START Delete Btn --- #}
<div class="text-right"> <div class="text-right">
<a href="/admin/planned-event/{{ planned_event.id }}/delete" class="inline-block btn btn-alert"> <a href="/admin/planned-event/{{ planned_event.id }}/delete" class="inline-block btn btn-alert">
{% include "includes/delete-icon" %} {% include "includes/delete-icon" %}