aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-08-25 20:54:15 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-08-25 20:54:15 +0200
commit7b33c425e89a214a55a32a24bd37d4dc9f729509 (patch)
tree3de7ff07748dcbf860ee6d0d1a9b63faf29ec4db
parent4fa322a49f1a24e86786ff8c0e7cf8a42797f61e (diff)
downloadramaskrik-social-7b33c425e89a214a55a32a24bd37d4dc9f729509.tar.gz
ramaskrik-social-7b33c425e89a214a55a32a24bd37d4dc9f729509.tar.bz2
ramaskrik-social-7b33c425e89a214a55a32a24bd37d4dc9f729509.zip
Implement update events.dev
-rw-r--r--src/controllers/event.rs20
-rw-r--r--src/main.rs1
-rw-r--r--src/models/event.rs9
-rw-r--r--templates/event/edit.html.tera1
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>