about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-08T18·01+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-08T18·01+0200
commit094b1e07225313c930c26dbeb73755f34a537ce4 (patch)
tree10dd453d62684b13185e5f332cff5d34b336b0d7
parentfc7ca2900d656974891c7e5ccfd532ab93aade94 (diff)
feat(handlers): Add thread submission handler
-rw-r--r--src/handlers.rs15
-rw-r--r--src/main.rs1
-rw-r--r--src/models.rs2
3 files changed, 17 insertions, 1 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index 446f59a4f3ac..cc4ac23c412d 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -74,3 +74,18 @@ pub fn forum_thread(state: State<AppState>, thread_id: Path<i32>) -> ConverseRes
         })
         .responder()
 }
+
+/// This handler receives a "New thread"-form and redirects the user
+/// to the new thread after creation.
+pub fn submit_thread(state: State<AppState>, input: Form<NewThread>) -> ConverseResponse {
+    state.db.send(CreateThread(input.0))
+        .from_err()
+        .and_then(move |res| {
+            let thread = res?;
+            info!("Created new thread \"{}\" with ID {}", thread.title, thread.id);
+            Ok(HttpResponse::TemporaryRedirect()
+               .header("Location", format!("/thread/{}", thread.id))
+               .finish())
+        })
+        .responder()
+}
diff --git a/src/main.rs b/src/main.rs
index 767d45891c3b..ad1f81927d32 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -61,6 +61,7 @@ fn main() {
         App::with_state(AppState { db: db_addr.clone(), tera })
             .middleware(middleware::Logger::default())
             .resource("/", |r| r.method(Method::GET).with(forum_index))
+            .resource("/thread", |r| r.method(Method::POST).with2(submit_thread))
             .resource("/thread/{id}", |r| r.method(Method::GET).with2(forum_thread))})
         .bind(&bind_host).expect(&format!("Could not bind on '{}'", bind_host))
         .start();
diff --git a/src/models.rs b/src/models.rs
index fc67c260a448..d85a54e03090 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -18,7 +18,7 @@ pub struct Post {
     pub posted: DateTime<Utc>,
 }
 
-#[derive(Insertable)]
+#[derive(Deserialize, Insertable)]
 #[table_name="threads"]
 pub struct NewThread {
     pub title: String,