//! Db executor actor use uuid; use diesel; use actix_web::*; use actix::prelude::*; use diesel::prelude::*; use models; use schema; /// This is db executor actor. We are going to run 3 of them in parallel. pub struct DbExecutor(pub 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; } impl Actor for DbExecutor { type Context = SyncContext; } impl Handler for DbExecutor { type Result = Result; 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, }; diesel::insert_into(users) .values(&new_user) .execute(&self.0) .expect("Error inserting person"); let mut items = users .filter(id.eq(&uuid)) .load::(&self.0) .expect("Error loading person"); Ok(items.pop().unwrap()) } }