forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): [torrust#179] Axum API, category context, get all cate…
…gories
- Loading branch information
1 parent
6f9c1a2
commit bb6d9bf
Showing
5 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters