mirror of
https://github.com/actix/actix-extras.git
synced 2024-12-01 02:44:37 +01:00
56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
//! Db executor actor
|
|
use uuid;
|
|
use diesel;
|
|
use actix_web::*;
|
|
use actix::prelude::*;
|
|
use diesel::prelude::*;
|
|
use diesel::r2d2::{ Pool, ConnectionManager };
|
|
|
|
use models;
|
|
use schema;
|
|
|
|
/// This is db executor actor. We are going to run 3 of them in parallel.
|
|
pub struct DbExecutor(pub Pool<ConnectionManager<SqliteConnection>>);
|
|
|
|
/// This is only message that this actor can handle, but it is easy to extend number of
|
|
/// messages.
|
|
pub struct CreateUser {
|
|
pub name: String,
|
|
}
|
|
|
|
impl Message for CreateUser {
|
|
type Result = Result<models::User, Error>;
|
|
}
|
|
|
|
impl Actor for DbExecutor {
|
|
type Context = SyncContext<Self>;
|
|
}
|
|
|
|
impl Handler<CreateUser> for DbExecutor {
|
|
type Result = Result<models::User, Error>;
|
|
|
|
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
|
|
use self::schema::users::dsl::*;
|
|
|
|
let uuid = format!("{}", uuid::Uuid::new_v4());
|
|
let new_user = models::NewUser {
|
|
id: &uuid,
|
|
name: &msg.name,
|
|
};
|
|
|
|
let conn: &SqliteConnection = &self.0.get().unwrap();
|
|
|
|
diesel::insert_into(users)
|
|
.values(&new_user)
|
|
.execute(conn)
|
|
.expect("Error inserting person");
|
|
|
|
let mut items = users
|
|
.filter(id.eq(&uuid))
|
|
.load::<models::User>(conn)
|
|
.expect("Error loading person");
|
|
|
|
Ok(items.pop().unwrap())
|
|
}
|
|
}
|