feat(handlers): Add thread submission handler

This commit is contained in:
Vincent Ambo 2018-04-08 20:01:32 +02:00
parent fc7ca2900d
commit 094b1e0722
3 changed files with 17 additions and 1 deletions

View file

@ -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()
}

View file

@ -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();

View file

@ -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,