diff --git a/Cargo.lock b/Cargo.lock index 85e2b05..0f3708f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ name = "questionnaire-rs" version = "0.1.0" dependencies = [ + "actix 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 0.7.19 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 12a2e99..6d25f02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ futures = "^0.1" env_logger = "^0.6" failure = "^0.1.5" reqwest = "^0.9.14" +actix = { version = "^0.7" } diff --git a/src/lib.rs b/src/lib.rs index 4d52951..5a091fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,6 +104,7 @@ extern crate reqwest; extern crate serde_json; #[macro_use] extern crate diesel; +extern crate actix; extern crate actix_web; extern crate dotenv; extern crate failure; @@ -133,6 +134,7 @@ pub mod helpers; pub mod middleware; pub mod models; pub mod presentations; +pub mod questions; pub mod schema; pub mod session; diff --git a/src/questions.rs b/src/questions.rs new file mode 100644 index 0000000..63e1e37 --- /dev/null +++ b/src/questions.rs @@ -0,0 +1,25 @@ +use actix::{Handler, Message}; +use diesel::query_dsl::RunQueryDsl; +use diesel::MysqlConnection; +use models::NewQuestion; +use DbExecutor; + +impl Message for NewQuestion { + type Result = Result<(), super::error::Db>; +} + +impl Handler for DbExecutor { + type Result = Result<(), super::error::Db>; + + fn handle(&mut self, msg: NewQuestion, _ctx: &mut Self::Context) -> Self::Result { + use schema::questions::dsl::questions; + let connection: &MysqlConnection = &self.0.get().unwrap(); + + diesel::insert_into(questions) + .values(&msg) + .execute(connection) + .expect("Error saving the question"); + + Ok(()) + } +}