diff --git a/src/web/api/v1/contexts/category/handlers.rs b/src/web/api/v1/contexts/category/handlers.rs new file mode 100644 index 00000000..4cc34dc3 --- /dev/null +++ b/src/web/api/v1/contexts/category/handlers.rs @@ -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`](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>, +) -> Result>>, database::Error> { + match app_data.category_repository.get_all().await { + Ok(categories) => Ok(Json(responses::OkResponse { data: categories })), + Err(error) => Err(error), + } +} diff --git a/src/web/api/v1/contexts/category/mod.rs b/src/web/api/v1/contexts/category/mod.rs index 68cf07e3..65d74e49 100644 --- a/src/web/api/v1/contexts/category/mod.rs +++ b/src/web/api/v1/contexts/category/mod.rs @@ -138,3 +138,5 @@ //! Refer to [`OkResponse`](crate::models::response::OkResponse) for more //! information about the response attributes. The response contains only the //! name of the deleted category. +pub mod handlers; +pub mod routes; diff --git a/src/web/api/v1/contexts/category/routes.rs b/src/web/api/v1/contexts/category/routes.rs new file mode 100644 index 00000000..c405714a --- /dev/null +++ b/src/web/api/v1/contexts/category/routes.rs @@ -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) -> Router { + Router::new().route("/", get(get_all_handler).with_state(app_data)) +} diff --git a/src/web/api/v1/routes.rs b/src/web/api/v1/routes.rs index 79185bf6..f7cbf5e2 100644 --- a/src/web/api/v1/routes.rs +++ b/src/web/api/v1/routes.rs @@ -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) -> 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) } diff --git a/tests/e2e/contexts/category/contract.rs b/tests/e2e/contexts/category/contract.rs index 43d854c9..a26df3b5 100644 --- a/tests/e2e/contexts/category/contract.rs +++ b/tests/e2e/contexts/category/contract.rs @@ -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); + } +}