-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
464c4fb
commit 1301800
Showing
2 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use actix::{Handler, Message}; | ||
use actix_web::middleware::session::RequestSession; | ||
use actix_web::AsyncResponder; | ||
use actix_web::Error; | ||
use actix_web::{HttpRequest, HttpResponse, Json, State}; | ||
use chrono::Utc; | ||
use diesel::query_dsl::RunQueryDsl; | ||
use diesel::result::Error as DieselError; | ||
use diesel::MysqlConnection; | ||
use futures::future::IntoFuture; | ||
use futures::Future; | ||
use middleware::GitHubUserId; | ||
use models::{NewOption, NewOptionJson}; | ||
use GH_USER_SESSION_ID_KEY; | ||
use {AppState, DbExecutor}; | ||
|
||
impl Message for NewOption { | ||
type Result = Result<(), DieselError>; | ||
} | ||
|
||
impl Handler<NewOption> for DbExecutor { | ||
type Result = Result<(), DieselError>; | ||
|
||
fn handle(&mut self, msg: NewOption, _ctx: &mut Self::Context) -> Self::Result { | ||
use schema::options::dsl::options; | ||
let connection: &MysqlConnection = &self.0.get().unwrap(); | ||
|
||
diesel::insert_into(options) | ||
.values(&msg) | ||
.execute(connection) | ||
.expect("Error saving the option."); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// `/options` POST | ||
/// | ||
/// Headers: | ||
/// | ||
/// Content type: application/json | ||
/// Authorization: token <access_token> | ||
/// | ||
/// Body: | ||
/// ```json | ||
/// { | ||
/// "data": "Option 1", | ||
/// "question_id": 1, | ||
/// } | ||
/// ``` | ||
/// | ||
/// Response: 200 OK | ||
pub fn post( | ||
data: Json<NewOptionJson>, | ||
state: State<AppState>, | ||
req: HttpRequest<AppState>, | ||
) -> Box<Future<Item = HttpResponse, Error = Error>> { | ||
let gh_user_id_session = req | ||
.session() | ||
.get::<GitHubUserId>(GH_USER_SESSION_ID_KEY) | ||
.into_future(); | ||
|
||
let now = Utc::now(); | ||
|
||
gh_user_id_session | ||
.from_err() | ||
.and_then(move |gh_user_id| { | ||
let input = data.into_inner(); | ||
let new_option = NewOption::new( | ||
input.data, | ||
gh_user_id.unwrap().id, | ||
input.question_id, | ||
now.naive_utc(), | ||
); | ||
|
||
state | ||
.db | ||
.send(new_option) | ||
.from_err() | ||
.and_then(|response| match response { | ||
Ok(_) => Ok(HttpResponse::Ok().finish()), | ||
Err(_) => Ok(HttpResponse::InternalServerError().into()), | ||
}) | ||
}) | ||
.responder() | ||
} |