From 7c73949066cf1b730a2c8c3408fa016048538716 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 9 Apr 2018 09:20:27 +0200 Subject: feat(handlers): Extract & add author to thread and post information --- src/handlers.rs | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/handlers.rs') diff --git a/src/handlers.rs b/src/handlers.rs index e709fdd202..c31cdf679b 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -81,10 +81,28 @@ pub fn forum_thread(state: State, thread_id: Path) -> ConverseRes .responder() } +#[derive(Deserialize)] +pub struct NewThreadForm { + pub title: String, + pub body: String, +} + /// This handler receives a "New thread"-form and redirects the user /// to the new thread after creation. -pub fn submit_thread(state: State, input: Form) -> ConverseResponse { - state.db.send(CreateThread(input.0)) +pub fn submit_thread(state: State, + input: Form, + mut req: HttpRequest) -> ConverseResponse { + // Author is "unwrapped" because the RequireLogin middleware + // guarantees it to be present. + let author: Author = req.session().get(AUTHOR).unwrap().unwrap(); + let new_thread = NewThread { + title: input.0.title, + body: input.0.body, + author_name: author.name, + author_email: author.email, + }; + + state.db.send(CreateThread(new_thread)) .from_err() .and_then(move |res| { let thread = res?; @@ -96,10 +114,28 @@ pub fn submit_thread(state: State, input: Form) -> Converse .responder() } +#[derive(Deserialize)] +pub struct NewPostForm { + pub thread_id: i32, + pub body: String, +} + /// This handler receives a "Reply"-form and redirects the user to the /// new post after creation. -pub fn reply_thread(state: State, input: Form) -> ConverseResponse { - state.db.send(CreatePost(input.0)) +pub fn reply_thread(state: State, + input: Form, + mut req: HttpRequest) -> ConverseResponse { + // Author is "unwrapped" because the RequireLogin middleware + // guarantees it to be present. + let author: Author = req.session().get(AUTHOR).unwrap().unwrap(); + let new_post = NewPost { + thread_id: input.thread_id, + body: input.0.body, + author_name: author.name, + author_email: author.email, + }; + + state.db.send(CreatePost(new_post)) .from_err() .and_then(move |res| { let post = res?; -- cgit 1.4.1