about summary refs log tree commit diff
path: root/src/db.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-08T17·41+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-08T17·41+0200
commitfc7ca2900d656974891c7e5ccfd532ab93aade94 (patch)
tree67703186886f1868a92b72b6a61e2db91f4ab252 /src/db.rs
parentf281749b8c5fbd80b77159f170a5ab173ec69d71 (diff)
feat(db): Support CreateThread message
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs
index ed4a78659332..8c828d9c60c8 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,6 +1,7 @@
 //! This module implements the database connection actor.
 
 use actix::prelude::*;
+use diesel;
 use diesel::prelude::*;
 use diesel::r2d2::{Pool, ConnectionManager};
 use models::*;
@@ -57,3 +58,24 @@ impl Handler<GetThread> for DbExecutor {
         Ok((thread_result, post_list))
     }
 }
+
+/// Message used to create a new thread
+pub struct CreateThread(pub NewThread);
+
+impl Message for CreateThread {
+    type Result = Result<Thread>;
+}
+
+impl Handler<CreateThread> for DbExecutor {
+    type Result = <CreateThread as Message>::Result;
+
+    fn handle(&mut self, msg: CreateThread, _: &mut Self::Context) -> Self::Result {
+        use schema::threads;
+
+        let conn = self.0.get()?;
+
+        Ok(diesel::insert_into(threads::table)
+            .values(&msg.0)
+            .get_result(&conn)?)
+    }
+}