BLOG-56 refactor: remove controller initialization from container
Some checks failed
Frontend CI / build (push) Successful in 1m28s
PR Title Check / pr-title-check (pull_request) Failing after 16s

This commit is contained in:
Yu Squire[ Yu, Tsung-Ying ] 2025-07-01 12:23:25 +08:00
parent 9dcefe90c5
commit 6a2c35a74a
5 changed files with 42 additions and 22 deletions

View File

@ -45,7 +45,10 @@ impl PostController for PostControllerImpl {
&self,
is_published_only: bool,
) -> Result<Vec<PostInfoResponseDto>, PostError> {
let result = self.get_all_post_info_use_case.execute(is_published_only).await;
let result = self
.get_all_post_info_use_case
.execute(is_published_only)
.await;
result.map(|post_info_list| {
let post_info_response_dto_list: Vec<PostInfoResponseDto> = post_info_list

View File

@ -25,6 +25,8 @@ impl GetAllPostInfoUseCaseImpl {
#[async_trait]
impl GetAllPostInfoUseCase for GetAllPostInfoUseCaseImpl {
async fn execute(&self, is_published_only: bool) -> Result<Vec<PostInfo>, PostError> {
self.post_repository.get_all_post_info(is_published_only).await
self.post_repository
.get_all_post_info(is_published_only)
.await
}
}

View File

@ -1,10 +1,17 @@
use std::sync::Arc;
use actix_web::{HttpResponse, Responder, web};
use crate::{
adapter::delivery::{post_controller::PostController, post_info_query_dto::PostQueryDto},
application::error::post_error::PostError,
adapter::delivery::{
post_controller::{PostController, PostControllerImpl},
post_info_query_dto::PostQueryDto,
},
application::{
error::post_error::PostError,
use_case::{
get_all_post_info_use_case::GetAllPostInfoUseCase,
get_full_post_use_case::GetFullPostUseCase,
},
},
};
pub fn configure_post_routes(cfg: &mut web::ServiceConfig) {
@ -16,9 +23,15 @@ pub fn configure_post_routes(cfg: &mut web::ServiceConfig) {
}
async fn get_all_post_info_handler(
post_controller: web::Data<Arc<dyn PostController>>,
get_all_post_info_use_case: web::Data<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: web::Data<dyn GetFullPostUseCase>,
query: web::Query<PostQueryDto>,
) -> impl Responder {
let post_controller = PostControllerImpl::new(
get_all_post_info_use_case.into_inner(),
get_full_post_use_case.into_inner(),
);
let is_published_only = query.is_published_only.unwrap_or_else(|| true);
let result = post_controller.get_all_post_info(is_published_only).await;
@ -32,9 +45,15 @@ async fn get_all_post_info_handler(
}
async fn get_full_post_handler(
post_controller: web::Data<Arc<dyn PostController>>,
get_all_post_info_use_case: web::Data<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: web::Data<dyn GetFullPostUseCase>,
path: web::Path<i32>,
) -> impl Responder {
let post_controller = PostControllerImpl::new(
get_all_post_info_use_case.into_inner(),
get_full_post_use_case.into_inner(),
);
let id = path.into_inner();
let result = post_controller.get_full_post(id).await;

View File

@ -1,20 +1,18 @@
use std::sync::Arc;
use post::{
adapter::{
delivery::post_controller::{PostController, PostControllerImpl},
gateway::post_repository_impl::PostRepositoryImpl,
},
adapter::gateway::post_repository_impl::PostRepositoryImpl,
application::use_case::{
get_all_post_info_use_case::GetAllPostInfoUseCaseImpl,
get_full_post_use_case::GetFullPostUseCaseImpl,
get_all_post_info_use_case::{GetAllPostInfoUseCase, GetAllPostInfoUseCaseImpl},
get_full_post_use_case::{GetFullPostUseCase, GetFullPostUseCaseImpl},
},
framework::db::post_db_service_impl::PostDbServiceImpl,
};
use sqlx::{Pool, Postgres};
pub struct Container {
pub post_controller: Arc<dyn PostController>,
pub get_all_post_info_use_case: Arc<dyn GetAllPostInfoUseCase>,
pub get_full_post_use_case: Arc<dyn GetFullPostUseCase>,
}
impl Container {
@ -27,11 +25,9 @@ impl Container {
Arc::new(GetAllPostInfoUseCaseImpl::new(post_repository.clone()));
let get_full_post_use_case = Arc::new(GetFullPostUseCaseImpl::new(post_repository.clone()));
let post_controller = Arc::new(PostControllerImpl::new(
Self {
get_all_post_info_use_case,
get_full_post_use_case,
));
Self { post_controller }
}
}
}

View File

@ -51,10 +51,10 @@ fn create_app(
Error = Error,
>,
> {
let container = Container::new(db_pool.clone());
let container = Container::new(db_pool);
App::new()
.app_data(web::Data::new(db_pool))
.app_data(web::Data::new(container.post_controller))
.app_data(web::Data::from(container.get_all_post_info_use_case))
.app_data(web::Data::from(container.get_full_post_use_case))
.configure(configure_post_routes)
}