75 lines
3.3 KiB
Rust
75 lines
3.3 KiB
Rust
|
/// This macro unwraps the value and if its an error it rolls back the transaction and returns a TypedHttpResponse with the corresponding erorr.
|
||
|
#[macro_export]
|
||
|
macro_rules! handle_db_write_op {
|
||
|
($e:expr, $tx:expr) => {
|
||
|
match $e.await {
|
||
|
Ok(value) => value,
|
||
|
Err(error) => {
|
||
|
handle_tx!($tx.rollback());
|
||
|
return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(400, err::MessageResource::new_from_string(error.to_string()));
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#[macro_export]
|
||
|
macro_rules! handle_db_read_op {
|
||
|
($e:expr) => {
|
||
|
match $e.await {
|
||
|
Ok(value) => value,
|
||
|
Err(error) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(400, err::MessageResource::new_from_string(error.to_string())),
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// This macro calls await on whatever you give it and if it gets an error it returns a TypedHttpResponse with an InternalServerError status code (500) and an error message.
|
||
|
#[macro_export]
|
||
|
macro_rules! handle_tx {
|
||
|
($e:expr) => {
|
||
|
match $e.await {
|
||
|
Ok(value) => value,
|
||
|
Err(_) => {
|
||
|
return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(500, err::MessageResource::new_from_str("Failed to acquire, commit or rollback tx..."));
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// This macro just returns a TypedHttpResponse with a success status code (200) and whatever you give it inside.
|
||
|
#[macro_export]
|
||
|
macro_rules! success {
|
||
|
($e:expr) => {
|
||
|
return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_response(200, $e)
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// This macro just returns a TypedHttpResponse with a not found status code (404) and an error concatenated.
|
||
|
/// The literal should be a subject in plural form:
|
||
|
/// Agent -> agents
|
||
|
#[macro_export]
|
||
|
macro_rules! unwrap_or_not_found {
|
||
|
($e:expr, $what:literal) => {
|
||
|
match $e {
|
||
|
Some(value) => value,
|
||
|
None => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(404, err::MessageResource::new_from_string(format!("No {} found with specified Id.", $what))),
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/// This macro does the authentication needed for all the admin routes. give it a user and password as params.
|
||
|
#[macro_export]
|
||
|
macro_rules! auth {
|
||
|
($request:expr) => {
|
||
|
let __auth_token_header = match $request.headers().get("auth_token") {
|
||
|
Some(header_val) => {header_val.to_str().expect("ASCII Chars")},
|
||
|
None => {return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("No Token in header.")))}
|
||
|
};
|
||
|
match crate::utils::auth::get_token() {
|
||
|
Some(token) => if token == __auth_token_header {} else {
|
||
|
return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("Incorrect Token.")))
|
||
|
},
|
||
|
None => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(format!("No Token in backend. Please authenticate."))),
|
||
|
}
|
||
|
};
|
||
|
}
|