Skip to content

Commit

Permalink
questions GET endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Jun 17, 2019
1 parent 82461fd commit a63edac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@
//! ```
//!
//! **Response:** 200 OK
//!
//! #### `/questions/{id}`
//!
//! **Method:** GET
//!
//! **Headers:**
//!
//! ```txt
//! Authorization: token <access_token>
//! ```
//!
//! **Response:**
//!
//! ```json
//! {
//! "id": 23,
//! "title": "New Question",
//! "created": "2019-11-01T14:30:30",
//! "presentation_id": 3,
//! "user_id": 7,
//! }
//! ```
extern crate chrono;
extern crate env_logger;
extern crate reqwest;
Expand Down Expand Up @@ -211,4 +233,7 @@ pub fn create_app() -> App<AppState> {
.resource("/questions", |r| {
r.method(Method::POST).with_async(questions::post)
})
.resource("/questions/{id}", |r| {
r.method(Method::GET).with_async(questions::get)
})
}
36 changes: 35 additions & 1 deletion src/questions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix::{Handler, Message};
use actix_web::middleware::session::RequestSession;
use actix_web::AsyncResponder;
use actix_web::{AsyncResponder, Path};
use actix_web::{Error, HttpRequest, HttpResponse, Json, State};
use chrono::Utc;
use diesel::prelude::*;
Expand Down Expand Up @@ -100,3 +100,37 @@ pub fn post(
})
.responder()
}

/// `/questions/{id}` GET
///
/// Headers:
///
/// Authorization: token <access_token>
///
/// Response:
/// ```json
/// {
/// "id": 23,
/// "title": "New Question",
/// "created": "2019-11-01T14:30:30",
/// "presentation_id": 3,
/// "user_id": 7,
/// }
/// ```
pub fn get(
data: Path<GetQuestion>,
req: HttpRequest<AppState>,
) -> Box<Future<Item = HttpResponse, Error = Error>> {
let state: &AppState = req.state();

state
.db
.send(data.into_inner())
.from_err()
.and_then(|response| match response {
Ok(result) => Ok(HttpResponse::Ok().json(result)),
Err(DieselError::NotFound) => Ok(HttpResponse::NotFound().into()),
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}

0 comments on commit a63edac

Please sign in to comment.