Skip to content

Commit

Permalink
code refactorings and version up to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TaeyoonKwon committed Feb 15, 2022
1 parent 15ebb1f commit 496d451
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-rocket-sample"
version = "0.1.1"
version = "1.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
5 changes: 4 additions & 1 deletion src/errors/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ pub struct MyError {
}

impl MyError {
// building a custom error.
pub fn build(code: u16, description: Option<String>) -> MyError {
let reason: String;
match code {
400 => reason = "Bad Request".to_string(),
401 => reason = "Unauthorized".to_string(),
_ => reason = "Error".to_string(),
}
MyError {
Expand Down Expand Up @@ -93,7 +95,8 @@ impl OpenApiResponderInner for MyError {
Ok(Responses {
responses: okapi::map! {
"400".to_owned() => RefOr::Object(bad_request_response(gen)),
"401".to_owned() => RefOr::Object(unauthorized_response(gen)),
// Note: 401 is already declared for ApiKey. so this is not essential.
// "401".to_owned() => RefOr::Object(unauthorized_response(gen)),
},
..Default::default()
})
Expand Down
5 changes: 2 additions & 3 deletions src/request_guards/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rocket_okapi::{
};
use std::env;

use crate::errors::response::{bad_request_response, unauthorized_response};
use crate::errors::response::unauthorized_response;

// #[derive(OpenApiFromRequest)]
pub struct ApiKey(String);
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> OpenApiFromRequest<'a> for ApiKey {
// Each security requirement needs to be met before access is allowed.
security_req.insert("ApiKey".to_owned(), Vec::new());
Ok(RequestHeaderInput::Security(
"ApiKeyAuth".to_owned(),
"ApiKey".to_owned(),
security_scheme,
security_req,
))
Expand All @@ -71,7 +71,6 @@ impl<'a> OpenApiFromRequest<'a> for ApiKey {
use rocket_okapi::okapi::openapi3::RefOr;
Ok(Responses {
responses: okapi::map! {
"400".to_owned() => RefOr::Object(bad_request_response(gen)),
"401".to_owned() => RefOr::Object(unauthorized_response(gen)),
},
..Default::default()
Expand Down
38 changes: 19 additions & 19 deletions src/routes/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ use crate::errors::response::MyError;
#[get("/customer?<limit>&<page>")]
pub async fn get_customers(
db: &State<Database>,
limit: i64,
limit: Option<i64>,
page: Option<i64>,
) -> Result<Json<Vec<Customer>>, BadRequest<Json<MessageResponse>>> {
) -> Result<Json<Vec<Customer>>, MyError> {
// Error handling
if limit < 0 {
return Err(BadRequest(Some(Json(MessageResponse {
message: "limit cannot be less than 0".to_string(),
}))));
}

if !page.is_none() && page.unwrap() < 1 {
return Err(BadRequest(Some(Json(MessageResponse {
message: "page cannot be less than 1".to_string(),
}))));
}

match customer::find_customer(&db, limit, if page.is_none() { 1 } else { page.unwrap() }).await
{
// This is also valid when strict checking is necessary.
// if limit < 0 {
// return Err(BadRequest(Some(Json(MessageResponse {
// message: "limit cannot be less than 0".to_string(),
// }))));
// }
// if !page.is_none() && page.unwrap() < 1 {
// return Err(BadRequest(Some(Json(MessageResponse {
// message: "page cannot be less than 1".to_string(),
// }))));
// }

// Setting default values
let limit: i64 = limit.unwrap_or(12);
let page: i64 = page.unwrap_or(1);
match customer::find_customer(&db, limit, page).await {
Ok(_customer_docs) => Ok(Json(_customer_docs)),
Err(_error) => {
println!("{:?}", _error);
Err(BadRequest(Some(Json(MessageResponse {
message: _error.to_string(),
}))))
return Err(MyError::build(400, Some(_error.to_string())));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::models::response::MessageResponse;
pub mod customer;

/// This is a description. <br />You can do simple html <br /> like <b>this<b/>
#[openapi(tag = "Hello")]
#[openapi(tag = "Hello World")]
#[get("/")]
pub fn index() -> Json<MessageResponse> {
Json(MessageResponse {
Expand Down

0 comments on commit 496d451

Please sign in to comment.