From 36e520a2b25e15cc227242d6af3784e08e45c1b1 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 8 Apr 2018 17:04:45 +0200 Subject: [PATCH] feat(db): Implement ListThreads message Implements support for a message for listing threads. This does not have any pagination support yet. --- src/db.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/db.rs b/src/db.rs index 57aa5f982..17eecc938 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>); impl Actor for DbExecutor { type Context = SyncContext; } + +/// Message used to request a list of threads. +/// TODO: This should support page numbers. +pub struct ListThreads; + +impl Message for ListThreads { + type Result = Result, Error>; +} + +impl Handler for DbExecutor { + type Result = ::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::(&conn).expect("Error loading threads"); + Ok(results) + } +}