From 7b33c425e89a214a55a32a24bd37d4dc9f729509 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 25 Aug 2021 20:54:15 +0200 Subject: Implement update events. --- src/controllers/event.rs | 20 ++++++++++++++++++++ src/main.rs | 1 + src/models/event.rs | 9 +++++++++ templates/event/edit.html.tera | 1 + 4 files changed, 31 insertions(+) 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 Ok(Template::render("event/edit", Context { event })) } + +#[derive(FromForm)] +pub struct EditEventForm { + pub id: i32, + pub name: String, + pub description: String, +} + +#[post("//update", format = "application/x-www-form-urlencoded", data = "
")] +pub async fn update(db: db::Connection, eventid: i32, form: Form) -> Result { + 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 { + 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 @@

Edit {{ event.name }}

+ -- cgit v1.2.3