Skip to content

Commit

Permalink
questions POST
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Jun 13, 2019
1 parent d758f2f commit 527e092
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@
//! "created": "2019-11-01T14:30:30"
//! }
//! ```
//!
//! #### ``/questions`
//!
//! **Method:** POST
//!
//! **Headers:**
//!
//! ```txt
//! Content type: application/json
//! Authorization: token <access_token>
//! ```
//!
//! **Body:**
//!
//! ```json
//! {
//! "title": "New Question",
//! "presentation_id": 1,
//! }
//! ```
//!
//! **Response:** 200 OK
extern crate chrono;
extern crate env_logger;
extern crate reqwest;
Expand Down Expand Up @@ -186,4 +208,7 @@ pub fn create_app() -> App<AppState> {
.resource("presentations/{id}", |r| {
r.method(Method::GET).with_async(presentations::get)
})
.resource("/questions", |r| {
r.method(Method::POST).with_async(questions::post)
})
}
4 changes: 2 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl NewQuestion {
/// creating the question in database.
#[derive(Deserialize, Serialize, Debug)]
pub struct NewQuestionJson {
title: String,
presentation_id: i32,
pub title: String,
pub presentation_id: i32,
}

#[derive(Queryable, Serialize, Deserialize, Identifiable)]
Expand Down
63 changes: 61 additions & 2 deletions src/questions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use actix::{Handler, Message};
use actix_web::middleware::session::RequestSession;
use actix_web::AsyncResponder;
use actix_web::{Error, HttpRequest, HttpResponse, Json, State};
use chrono::Utc;
use diesel::query_dsl::RunQueryDsl;
use diesel::MysqlConnection;
use models::NewQuestion;
use DbExecutor;
use futures::Future;
use futures::IntoFuture;
use middleware::GitHubUserId;
use models::{NewQuestion, NewQuestionJson};
use GH_USER_SESSION_ID_KEY;
use {AppState, DbExecutor};

impl Message for NewQuestion {
type Result = Result<(), super::error::Db>;
Expand All @@ -23,3 +31,54 @@ impl Handler<NewQuestion> for DbExecutor {
Ok(())
}
}

/// `/questions` POST
///
/// Headers:
///
/// Content type: application/json
/// Authorization: token <access_token>
///
/// Body:
/// ```json
/// {
/// "title": "New Question",
/// "presentation_id": 1,
/// }
/// ```
///
/// Response: 200 OK
pub fn post(
data: Json<NewQuestionJson>,
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_question = NewQuestion::new(
input.title,
now.naive_utc(),
input.presentation_id,
gh_user_id.unwrap().id,
);

state
.db
.send(new_question)
.from_err()
.and_then(|response| match response {
Ok(_) => Ok(HttpResponse::Ok().finish()),
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
})
.responder()
}

0 comments on commit 527e092

Please sign in to comment.