diff options
-rw-r--r-- | src/controllers/event.rs | 20 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/models/event.rs | 9 | ||||
-rw-r--r-- | templates/event/edit.html.tera | 1 |
4 files changed, 31 insertions, 0 deletions
diff --git a/src/controllers/event.rs b/src/controllers/event.rs index eca8c21..c7ac4d3 100644 --- a/src/controllers/event.rs +++ b/src/controllers/event.rs @@ -92,3 +92,23 @@ pub async fn edit(db: db::Connection, eventid: i32) -> Result<Template, Status> Ok(Template::render("event/edit", Context { event })) } + +#[derive(FromForm)] +pub struct EditEventForm { + pub id: i32, + pub name: String, + pub description: String, +} + +#[post("/<eventid>/update", format = "application/x-www-form-urlencoded", data = "<form>")] +pub async fn update(db: db::Connection, eventid: i32, form: Form<EditEventForm>) -> Result<Redirect, Status> { + if eventid != form.id { + return Err(Status::BadRequest); + } + + models::Event::update(&db, eventid, form.name.to_owned(), form.description.to_owned()) + .await + .map_err(|_| Status::InternalServerError)?; + + Ok(Redirect::to("/")) +} diff --git a/src/main.rs b/src/main.rs index 978c702..b0f00ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ fn rocket() -> _ { event::new, event::create, event::edit, + event::update, ]) .mount("/", FileServer::from("public/")) .mount("/rooms", routes![ diff --git a/src/models/event.rs b/src/models/event.rs index baa44fc..62269f4 100644 --- a/src/models/event.rs +++ b/src/models/event.rs @@ -60,4 +60,13 @@ impl Event { .get_result(conn) }).await } + + pub async fn update(db: &db::Connection, eventid: i32, eventname: String, desc: String) -> QueryResult<Event> { + db.run(move |conn| { + use crate::schema::event::dsl::*; + diesel::update(event.filter(id.eq(eventid))) + .set((name.eq(eventname), description.eq(desc))) + .get_result(conn) + }).await + } } diff --git a/templates/event/edit.html.tera b/templates/event/edit.html.tera index 6e9fe3d..9804fb4 100644 --- a/templates/event/edit.html.tera +++ b/templates/event/edit.html.tera @@ -4,6 +4,7 @@ <h1>Edit {{ event.name }}</h1> <form name="new_event_form" action="update" method="POST"> + <input name="id" type="hidden" value="{{ event.id }}"> <label for="name">Name: </label><input name="name" type="text" value="{{ event.name }}"> <label for="description">Description: </label> <textarea name="description">{{ event.description }}</textarea> |