feat(db): Implement ListThreads message

Implements support for a message for listing threads. This does not
have any pagination support yet.
This commit is contained in:
Vincent Ambo 2018-04-08 17:04:45 +02:00
parent bea6eb8eb3
commit 36e520a2b2

View file

@ -1,8 +1,10 @@
//! This module implements the database connection actor. //! This module implements the database connection actor.
use actix::prelude::*; use actix::prelude::*;
use diesel::prelude::PgConnection; use actix_web::Error;
use diesel::prelude::*;
use diesel::r2d2::{Pool, ConnectionManager}; use diesel::r2d2::{Pool, ConnectionManager};
use models::*;
/// The DB actor itself. Several of these will be run in parallel by /// The DB actor itself. Several of these will be run in parallel by
/// `SyncArbiter`. /// `SyncArbiter`.
@ -11,3 +13,23 @@ pub struct DbExecutor(pub Pool<ConnectionManager<PgConnection>>);
impl Actor for DbExecutor { impl Actor for DbExecutor {
type Context = SyncContext<Self>; 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)
}
}