Skip to content

Commit

Permalink
docs: Add annotation doc to the function
Browse files Browse the repository at this point in the history
  • Loading branch information
bush1D3v committed Jul 28, 2024
1 parent 3817277 commit 56465a9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/utils/error_construct.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
use crate::shared::structs::error_struct::{ErrorParams, ErrorStruct};
use serde_json::json;

// Erros do tipo validator::ValidationErros não precisam passar por aqui, pois esta é uma
// função que visa simular um erro deste mesmo tipo, padronizando o retorno de erros e exceções.
/// Construct an default error response.
///
/// This function constructs an error response with the provided `data`, `code`, `message`, `value`, `min`, and `max`.
///
/// # Parameters
///
/// - `data`: The input data that caused the error.
/// - `code`: The text description of status code.
/// - `message`: The message description of the error to be shown to the user.
/// - `value`: The optional value of the input data.
/// - `max`: The optional max length of the input data.
/// - `min`: The optional min length of the input data.
///
/// # Returns
///
/// Returns a `serde_json::Value` with the default error response.
///
/// # Example
///
/// ```rust
/// use navarro_blog_api::utils::error_construct::error_construct;
///
/// let error = error_construct(
/// String::from("email"),
/// String::from("conflict"),
/// String::from("Este e-mail já está sendo utilizado por outro usuário."),
/// Some(String::from("emailDeTeste@gmail.com")),
/// Some(10),
/// Some(127),
/// );
/// ```
pub fn error_construct(
data: String,
code: String,
Expand Down
40 changes: 39 additions & 1 deletion src/utils/query_constructor_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ use actix_web::HttpResponse;
use sql_builder::SqlBuilder;
use tokio_postgres::Row;

/// Construct and execute the query.
///
/// This function constructs and executes the query, based on the provided `SqlBuilder` and `Pool`.
///
/// # Parameters
///
/// - `pg_pool`: A connection pool for the database.
/// - `sql_builder`: An instance of the `SqlBuilder` struct.
///
/// # Returns
///
/// Returns a `Result` which, on success, contains the result of the query. On failure, returns an `HttpResponse` with the corresponding error.
///
/// # Errors
///
/// This function may return an error if:
///
/// - It is not possible to obtain a connection from the pool.
/// - The transaction fails.
/// - The conversion from `sql_builder::SqlBuilder` to `String` fails.
/// - The query fails.
/// - The commit fails.
///
/// # Example
///
/// ```rust
/// use navarro_blog_api::utils::query_constructor_executor::query_constructor_executor;
/// use sql_builder::SqlBuilder;
/// use actix_web::{web::Data, HttpResponse};
/// use deadpool_postgres::Pool;
///
/// pub async fn example(pg_pool: Data<Pool>, sql_builder: SqlBuilder) -> Result<Vec<postgres::Row>, HttpResponse> {
/// match query_constructor_executor(pg_pool, sql_builder).await {
/// Ok(x) => Ok(x),
/// Err(e) => return Err(e),
/// }
/// }
/// ```
pub async fn query_constructor_executor(
pg_pool: actix_web::web::Data<deadpool_postgres::Pool>,
sql_builder: SqlBuilder,
Expand All @@ -21,7 +59,7 @@ pub async fn query_constructor_executor(
Ok(x) => x,
Err(e) => return Err(custom_error_to_io_error_kind(CustomError::AnyhowError(e))),
};
let rows = match transaction.query(sql.as_str(), &[]).await {
let rows = match transaction.query(&sql, &[]).await {
Ok(x) => x,
Err(e) => return Err(custom_error_to_io_error_kind(CustomError::TokioPostgres(e))),
};
Expand Down

0 comments on commit 56465a9

Please sign in to comment.