forked from Ruderverein-Donau-Linz/rowt
add edit option of planned_events
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use rocket::{
|
||||
form::Form,
|
||||
get, post,
|
||||
get, post, put,
|
||||
response::{Flash, Redirect},
|
||||
routes, FromForm, Route, State,
|
||||
};
|
||||
@ -49,6 +49,37 @@ async fn create(
|
||||
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")]
|
||||
async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> {
|
||||
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> {
|
||||
routes![create, delete]
|
||||
routes![create, delete, update]
|
||||
}
|
||||
|
Reference in New Issue
Block a user