Skip to content

Commit

Permalink
refactor(api): [torrust#179] Axum API, category context, get all cate…
Browse files Browse the repository at this point in the history
…gories
  • Loading branch information
josecelano committed Jun 14, 2023
1 parent 6f9c1a2 commit bb6d9bf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/web/api/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! API handlers for the the [`category`](crate::web::api::v1::contexts::category) API
//! context.
use std::sync::Arc;

use axum::extract::State;
use axum::response::Json;

use crate::common::AppData;
use crate::databases::database::{self, Category};
use crate::web::api::v1::responses;

/// It handles the request to get all the categories.
///
/// It returns:
///
/// - `200` response with a json containing the category list [`Vec<Category>`](crate::databases::database::Category).
/// - Other error status codes if there is a database error.
///
/// Refer to the [API endpoint documentation](crate::web::api::v1::contexts::category)
/// for more information about this endpoint.
///
/// # Errors
///
/// It returns an error if there is a database error.
#[allow(clippy::unused_async)]
pub async fn get_all_handler(
State(app_data): State<Arc<AppData>>,
) -> Result<Json<responses::OkResponse<Vec<Category>>>, database::Error> {
match app_data.category_repository.get_all().await {
Ok(categories) => Ok(Json(responses::OkResponse { data: categories })),
Err(error) => Err(error),
}
}
2 changes: 2 additions & 0 deletions src/web/api/v1/contexts/category/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@
//! Refer to [`OkResponse`](crate::models::response::OkResponse<T>) for more
//! information about the response attributes. The response contains only the
//! name of the deleted category.
pub mod handlers;
pub mod routes;
15 changes: 15 additions & 0 deletions src/web/api/v1/contexts/category/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! API routes for the [`category`](crate::web::api::v1::contexts::category) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::category).
use std::sync::Arc;

use axum::routing::get;
use axum::Router;

use super::handlers::get_all_handler;
use crate::common::AppData;

/// Routes for the [`category`](crate::web::api::v1::contexts::category) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
Router::new().route("/", get(get_all_handler).with_state(app_data))
}
13 changes: 7 additions & 6 deletions src/web/api/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ use std::sync::Arc;
use axum::Router;

//use tower_http::cors::CorsLayer;
use super::contexts::{about, user};
use super::contexts::about;
use super::contexts::{category, user};
use crate::common::AppData;

/// Add all API routes to the router.
#[allow(clippy::needless_pass_by_value)]
pub fn router(app_data: Arc<AppData>) -> Router {
let user_routes = user::routes::router(app_data.clone());
let about_routes = about::routes::router(app_data);
let api_routes = Router::new()
.nest("/user", user::routes::router(app_data.clone()))
.nest("/about", about::routes::router(app_data.clone()))
.nest("/category", category::routes::router(app_data));

let api_routes = Router::new().nest("/user", user_routes).nest("/about", about_routes);
Router::new().nest("/v1", api_routes)

// For development purposes only.
// It allows calling the API on a different port. For example
// API: http://localhost:3000/v1
// Webapp: http://localhost:8080
//Router::new().nest("/v1", api_routes).layer(CorsLayer::permissive())

Router::new().nest("/v1", api_routes)
}
20 changes: 20 additions & 0 deletions tests/e2e/contexts/category/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,23 @@ async fn it_should_not_allow_guests_to_delete_categories() {

assert_eq!(response.status, 401);
}

mod with_axum_implementation {
use torrust_index_backend::web::api;

use crate::common::asserts::assert_json_ok;
use crate::common::client::Client;
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_return_an_empty_category_list_when_there_are_no_categories() {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.get_categories().await;

assert_json_ok(&response);
}
}

0 comments on commit bb6d9bf

Please sign in to comment.