about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-08T15·04+0200
committerVincent Ambo <tazjin@gmail.com>2018-04-08T15·04+0200
commit36e520a2b25e15cc227242d6af3784e08e45c1b1 (patch)
tree4e8abc3c8a7a64fb2d2a129d15c635dafb16b63a /src
parentbea6eb8eb3cf97586155fa4857c6edd6a64fc84e (diff)
feat(db): Implement ListThreads message
Implements support for a message for listing threads. This does not
have any pagination support yet.
Diffstat (limited to 'src')
-rw-r--r--src/db.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/db.rs b/src/db.rs
index 57aa5f9827f6..17eecc938a82 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,8 +1,10 @@
 //! This module implements the database connection actor.
 
 use actix::prelude::*;
-use diesel::prelude::PgConnection;
+use actix_web::Error;
+use diesel::prelude::*;
 use diesel::r2d2::{Pool, ConnectionManager};
+use models::*;
 
 /// The DB actor itself. Several of these will be run in parallel by
 /// `SyncArbiter`.
@@ -11,3 +13,23 @@ pub struct DbExecutor(pub Pool<ConnectionManager<PgConnection>>);
 impl Actor for DbExecutor {
     type Context = SyncContext<Self>;
 }
+
+/// Message used to request a list of threads.
+/// TODO: This should support page numbers.
+pub struct ListThreads;
+
+impl Message for ListThreads {
+    type Result = Result<Vec<Thread>, Error>;
+}
+
+impl Handler<ListThreads> for DbExecutor {
+    type Result = <ListThreads as Message>::Result;
+
+    fn handle(&mut self, _: ListThreads, _: &mut Self::Context) -> Self::Result {
+        use schema::threads::dsl::*;
+
+        let conn = self.0.get().unwrap();
+        let results = threads.load::<Thread>(&conn).expect("Error loading threads");
+        Ok(results)
+    }
+}