From bf23aa5d4b2467a3e920d7e835abd4b7cc2664f6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 20 Dec 2017 17:43:43 -0800 Subject: [PATCH] move db code to separate module --- .travis.yml | 5 +--- examples/diesel/src/db.rs | 53 +++++++++++++++++++++++++++++++++++++ examples/diesel/src/main.rs | 48 +++------------------------------ 3 files changed, 58 insertions(+), 48 deletions(-) create mode 100644 examples/diesel/src/db.rs diff --git a/.travis.yml b/.travis.yml index f3e0ebbf0..8cfdaefd6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,13 +30,10 @@ before_script: script: - USE_SKEPTIC=1 cargo test --features=alpn - | - if [[ "$TRAVIS_RUST_VERSION" == "1.20.0" ]]; then + if [[ "$TRAVIS_RUST_VERSION" == "beta" ]]; then cd examples/diesel && cargo check && cd ../.. cd examples/multipart && cargo check && cd ../.. cd examples/json && cargo check && cd ../.. - fi - - | - if [[ "$TRAVIS_RUST_VERSION" == "beta" ]]; then cd examples/template_tera && cargo check && cd ../.. cd examples/tls && cargo check && cd ../.. cd examples/websocket-chat && cargo check && cd ../.. diff --git a/examples/diesel/src/db.rs b/examples/diesel/src/db.rs new file mode 100644 index 000000000..4e7bced91 --- /dev/null +++ b/examples/diesel/src/db.rs @@ -0,0 +1,53 @@ +//! 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 parallele. +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 ResponseType for CreateUser { + type Item = models::User; + type Error = Error; +} + +impl Actor for DbExecutor { + type Context = SyncContext; +} + +impl Handler for DbExecutor { + fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) + -> Response + { + 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"); + + Self::reply(items.pop().unwrap()) + } +} diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs index 18d926343..e29f5dbc2 100644 --- a/examples/diesel/src/main.rs +++ b/examples/diesel/src/main.rs @@ -21,9 +21,13 @@ use actix::prelude::*; use diesel::prelude::*; use futures::future::Future; +mod db; mod models; mod schema; +use db::{CreateUser, DbExecutor}; + + /// State with DbExecutor address struct State { db: SyncAddress, @@ -44,50 +48,6 @@ fn index(req: HttpRequest) -> Box> })) } -/// This is db executor actor. We are going to run 3 of them in parallele. -struct DbExecutor(SqliteConnection); - -/// This is only message that this actor can handle, but it is easy to extend number of -/// messages. -struct CreateUser { - name: String, -} - -impl ResponseType for CreateUser { - type Item = models::User; - type Error = Error; -} - -impl Actor for DbExecutor { - type Context = SyncContext; -} - -impl Handler for DbExecutor { - fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) - -> Response - { - 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"); - - Self::reply(items.pop().unwrap()) - } -} - fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); let _ = env_logger::init();