Skip to content

Commit

Permalink
Merge pull request #28 from EdwinBetanc0urt/bugfix/error-mandatory-re…
Browse files Browse the repository at this point in the history
…sponse-json

fix: Error with mandatory query response as json.
  • Loading branch information
yamelsenih authored May 25, 2024
2 parents cae98ed + 1b9d70f commit c1728f7
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 80 deletions.
157 changes: 122 additions & 35 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dotenv::dotenv;
use rdkafka::{Message, consumer::{CommitMode, Consumer}};
use salvo::{conn::tcp::TcpAcceptor, cors::Cors, http::header, hyper::Method, prelude::*};
extern crate serde_json;
use serde::Serialize;
use simple_logger::SimpleLogger;
use futures::future::join_all;

Expand Down Expand Up @@ -132,17 +133,33 @@ async fn options_response<'a>(_req: &mut Request, _res: &mut Response) {
_res.status_code(StatusCode::NO_CONTENT);
}

#[derive(Serialize)]
struct ErrorResponse {
status: u16,
message: String
}

#[handler]
async fn get_forms<'a>(_req: &mut Request, _res: &mut Response) {
let _id: Option<i32> = _req.param::<i32>("id");
let _language: Option<&String> = _req.queries().get("language");
let _client_id: Option<&String> = _req.queries().get("client_id");
let _role_id: Option<&String> = _req.queries().get("role_id");
let _user_id: Option<&String> = _req.queries().get("user_id");

if _id.is_some() {
match form_from_id(_language, _client_id, _role_id, _user_id, _id).await {
match form_from_id(_id, _language, _client_id, _role_id, _user_id).await {
Ok(form) => _res.render(Json(form)),
Err(error) => _res.render(Json(error))
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
} else {
let _search_value: Option<&String> = _req.queries().get("search_value");
Expand All @@ -151,9 +168,15 @@ async fn get_forms<'a>(_req: &mut Request, _res: &mut Response) {
Ok(forms_list) => {
_res.render(Json(forms_list));
},
Err(e) => {
_res.render(e.to_string());
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
Expand All @@ -162,27 +185,43 @@ async fn get_forms<'a>(_req: &mut Request, _res: &mut Response) {
#[handler]
async fn get_menu<'a>(_req: &mut Request, _res: &mut Response) {
let _id = _req.param::<i32>("id");
if _id.is_some() {
match menu_from_id(_id).await {
Ok(menu) => _res.render(Json(menu)),
Err(error) => _res.render(Json(error))
}
} else {
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _language = _req.queries().get("language");
let _client_id = _req.queries().get("client_id");
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");

if _id.is_some() {
match menu_from_id(_id, _language, _client_id, _role_id, _user_id).await {
Ok(menu) => _res.render(Json(menu)),
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
} else {
let _search_value = _req.queries().get("search_value");
let _page_number: Option<&String> = _req.queries().get("page_number");
let _page_size: Option<&String> = _req.queries().get("page_size");
match menus(_language, _client_id, _role_id, _user_id, _search_value, _page_number, _page_size).await {
Ok(menus_list) => {
_res.render(Json(menus_list));
},
Err(e) => {
_res.render(e.to_string());
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
}
}
Expand All @@ -195,20 +234,36 @@ async fn get_process<'a>(_req: &mut Request, _res: &mut Response) {
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match process_from_id(_language, _client_id, _role_id, _user_id, _id).await {
match process_from_id(_id, _language, _client_id, _role_id, _user_id).await {
Ok(process) => _res.render(Json(process)),
Err(error) => _res.render(Json(error))
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
} else {
match processes(_language, _client_id, _role_id, _user_id, _search_value).await {
Ok(processes_list) => {
_res.render(Json(processes_list));
},
Err(e) => {
_res.render(e.to_string());
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
}
Expand All @@ -221,20 +276,36 @@ async fn get_browsers<'a>(_req: &mut Request, _res: &mut Response) {
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match browser_from_id(_language, _client_id, _role_id, _user_id, _id).await {
match browser_from_id(_id, _language, _client_id, _role_id, _user_id).await {
Ok(browser) => _res.render(Json(browser)),
Err(error) => _res.render(Json(error))
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
} else {
match browsers(_language, _client_id, _role_id, _user_id, _search_value).await {
Ok(browsers_list) => {
_res.render(Json(browsers_list));
},
Err(e) => {
_res.render(e.to_string());
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
}
Expand All @@ -247,19 +318,35 @@ async fn get_windows<'a>(_req: &mut Request, _res: &mut Response) {
let _role_id = _req.queries().get("role_id");
let _user_id = _req.queries().get("user_id");
let _search_value = _req.queries().get("search_value");

if _id.is_some() {
match window_from_id(_language, _client_id, _role_id, _user_id, _id).await {
match window_from_id(_id, _language, _client_id, _role_id, _user_id).await {
Ok(window) => _res.render(Json(window)),
Err(error) => _res.render(Json(error))
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
} else {
match windows(_language, _client_id, _role_id, _user_id, _search_value).await {
Ok(windows_list) => {
_res.render(Json(windows_list));
},
Err(e) => {
_res.render(e.to_string());
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
Err(error) => {
let error_response = ErrorResponse {
status: StatusCode::INTERNAL_SERVER_ERROR.into(),
message: error.to_string()
};
_res.render(
Json(error_response)
);
_res.status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
Expand Down
39 changes: 28 additions & 11 deletions src/models/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,21 @@ pub struct DependendField {
pub parent_name: Option<String>
}

pub async fn browser_from_id(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>, _id: Option<i32>) -> Result<Browser, String> {
pub async fn browser_from_id(_id: Option<i32>, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> Result<Browser, String> {
if _id.is_none() || _id.map(|id| id <= 0).unwrap_or(false) {
return Err(Error::new(ErrorKind::InvalidData.into(), "Browser Identifier is Mandatory").to_string());
}
let mut _document = Browser::from_id(_id);
let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index");
log::info!("Index to search {:}", _index_name);

let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
return Err(error.to_string())
}
};
log::info!("Index to search {:}", _index_name);

_document.index_value = Some(_index_name);
let _browser_document: &dyn IndexDocument = &_document;
match get_by_id(_browser_document).await {
Expand All @@ -284,7 +295,7 @@ pub async fn browser_from_id(_language: Option<&String>, _client_id: Option<&Str
)
},
Err(error) => {
log::warn!("{}", error);
log::error!("{}", error);
Err(error)
},
}
Expand Down Expand Up @@ -330,15 +341,15 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>,
log::info!("Find with client index `{:}`", _client_index);
Ok(_client_index)
},
Err(_) => {
log::info!("No client index `{:}`", _client_index);
Err(_) => {
log::warn!("No client index `{:}`", _client_index);
match exists_index(_language_index.to_owned()).await {
Ok(_) => {
log::info!("Find with language index `{:}`", _language_index);
Ok(_language_index)
},
Err(_) => {
log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index);
Err(_) => {
log::warn!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index);
Ok(_default_index)
}
}
Expand All @@ -356,8 +367,15 @@ pub async fn browsers(_language: Option<&String>, _client_id: Option<&String>, _
None => "".to_owned()
};

let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index");
log::info!("Index to search {:}", _index_name);
// Find index
let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
return Err(Error::new(ErrorKind::InvalidData.into(), error))
}
};
log::info!("Index to search {:}", _index_name);

let mut _document = Browser::default();
_document.index_value = Some(_index_name);
Expand All @@ -375,5 +393,4 @@ pub async fn browsers(_language: Option<&String>, _client_id: Option<&String>, _
},
Err(error) => Err(Error::new(ErrorKind::InvalidData.into(), error))
}
// Ok(BrowserResponse::default())
}
30 changes: 24 additions & 6 deletions src/models/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,21 @@ impl IndexDocument for Form {
}
}

pub async fn form_from_id(_language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>, _id: Option<i32>) -> Result<Form, String> {
pub async fn form_from_id(_id: Option<i32>, _language: Option<&String>, _client_id: Option<&String>, _role_id: Option<&String>, _user_id: Option<&String>) -> Result<Form, String> {
if _id.is_none() || _id.map(|id| id <= 0).unwrap_or(false) {
return Err(Error::new(ErrorKind::InvalidData.into(), "Form Identifier is Mandatory").to_string());
}
let mut _document = Form::from_id(_id);
let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index");

let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
return Err(error.to_string())
}
};
log::info!("Index to search {:}", _index_name);

_document.index_value = Some(_index_name);
let _form_document: &dyn IndexDocument = &_document;
match get_by_id(_form_document).await {
Expand All @@ -138,7 +149,7 @@ pub async fn form_from_id(_language: Option<&String>, _client_id: Option<&String
)
},
Err(error) => {
log::warn!("{}", error);
log::error!("{}", error);
Err(error)
},
}
Expand Down Expand Up @@ -166,14 +177,14 @@ async fn get_index_name(_language: Option<&String>, _client_id: Option<&String>,
Ok(_client_index)
},
Err(_) => {
log::info!("No client index `{:}`", _client_index);
log::warn!("No client index `{:}`", _client_index);
match exists_index(_language_index.to_owned()).await {
Ok(_) => {
log::info!("Find with language index `{:}`", _language_index);
Ok(_language_index)
},
Err(_) => {
log::info!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index);
log::warn!("No language index `{:}`. Find with default index `{:}`.", _language_index, _default_index);
Ok(_default_index)
}
}
Expand All @@ -187,7 +198,14 @@ pub async fn forms(_language: Option<&String>, _client_id: Option<&String>, _rol
None => "".to_owned()
};

let _index_name = get_index_name(_language, _client_id, _role_id, _user_id).await.expect("Error getting index");
// Find index
let _index_name = match get_index_name(_language, _client_id, _role_id, _user_id).await {
Ok(index_name) => index_name,
Err(error) => {
log::error!("Index name error: {:?}", error.to_string());
return Err(Error::new(ErrorKind::InvalidData.into(), error))
}
};
log::info!("Index to search {:}", _index_name);

let mut _document = Form::default();
Expand Down
Loading

0 comments on commit c1728f7

Please sign in to comment.