Skip to content

Commit

Permalink
refactor: [torrust#524] handlers to inject only the needed dependeencies
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 7, 2024
1 parent 0cc2663 commit 2a7d2eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/web/api/server/v1/contexts/category/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::sync::Arc;

use axum::extract::{self, State};
use axum::response::{IntoResponse, Json, Response};
use axum::Extension;

use super::forms::{AddCategoryForm, DeleteCategoryForm};
use super::responses::{added_category, deleted_category, Category};
use crate::common::AppData;
use crate::services::category::DbCategoryRepository;
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
use crate::web::api::server::v1::responses::{self};

Expand All @@ -25,8 +27,8 @@ use crate::web::api::server::v1::responses::{self};
///
/// 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>>) -> Response {
match app_data.category_repository.get_all().await {
pub async fn get_all_handler(Extension(category_repository): Extension<Arc<DbCategoryRepository>>) -> Response {
match category_repository.get_all().await {
Ok(categories) => {
let categories: Vec<Category> = categories.into_iter().map(Category::from).collect();
Json(responses::OkResponseData { data: categories }).into_response()
Expand Down
9 changes: 5 additions & 4 deletions src/web/api/server/v1/contexts/category/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
use std::sync::Arc;

use axum::routing::{delete, get, post};
use axum::Router;
use axum::{Extension, Router};

use super::handlers::{add_handler, delete_handler, get_all_handler};
use crate::common::AppData;

/// Routes for the [`category`](crate::web::api::server::v1::contexts::category) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
pub fn router(app_data: &Arc<AppData>) -> Router {
Router::new()
.route("/", get(get_all_handler).with_state(app_data.clone()))
.route("/", get(get_all_handler))
.route("/", post(add_handler).with_state(app_data.clone()))
.route("/", delete(delete_handler).with_state(app_data))
.route("/", delete(delete_handler).with_state(app_data.clone()))
.layer(Extension(app_data.category_repository.clone()))
}
2 changes: 1 addition & 1 deletion src/web/api/server/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {
.route("/", get(redirect_to_about))
.nest("/user", user::routes::router(app_data.clone()))
.nest("/about", about::routes::router(app_data.clone()))
.nest("/category", category::routes::router(app_data.clone()))
.nest("/category", category::routes::router(&app_data.clone()))
.nest("/tag", tag::routes::router_for_single_resources(app_data.clone()))
.nest("/tags", tag::routes::router_for_multiple_resources(app_data.clone()))
.nest("/settings", settings::routes::router(app_data.clone()))
Expand Down

0 comments on commit 2a7d2eb

Please sign in to comment.