From 148dfc39c80266e9c3077737e4258739dba7d86a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 8 Apr 2018 20:19:46 +0200 Subject: [PATCH] feat(db/models): Add handling of CreatePost message --- src/db.rs | 25 +++++++++++++++++++++++-- src/models.rs | 7 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 8c828d9c6..baf404af2 100644 --- a/src/db.rs +++ b/src/db.rs @@ -75,7 +75,28 @@ impl Handler for DbExecutor { let conn = self.0.get()?; Ok(diesel::insert_into(threads::table) - .values(&msg.0) - .get_result(&conn)?) + .values(&msg.0) + .get_result(&conn)?) + } +} + +/// Message used to create a new reply +pub struct CreatePost(pub NewPost); + +impl Message for CreatePost { + type Result = Result; +} + +impl Handler for DbExecutor { + type Result = ::Result; + + fn handle(&mut self, msg: CreatePost, _: &mut Self::Context) -> Self::Result { + use schema::posts; + + let conn = self.0.get()?; + + Ok(diesel::insert_into(posts::table) + .values(&msg.0) + .get_result(&conn)?) } } diff --git a/src/models.rs b/src/models.rs index d85a54e03..28b1caa7c 100644 --- a/src/models.rs +++ b/src/models.rs @@ -24,3 +24,10 @@ pub struct NewThread { pub title: String, pub body: String, } + +#[derive(Deserialize, Insertable)] +#[table_name="posts"] +pub struct NewPost { + pub thread_id: i32, + pub body: String, +}