Compare commits

..

1 Commits

Author SHA1 Message Date
Yu Squire[ Yu, Tsung-Ying ]
6a2c35a74a 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
2025-07-01 12:23:25 +08:00
4 changed files with 43 additions and 26 deletions

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use async_trait::async_trait;
use crate::application::{
@ -21,14 +23,14 @@ pub trait PostController: Send + Sync {
}
pub struct PostControllerImpl {
get_all_post_info_use_case: Box<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: Box<dyn GetFullPostUseCase>,
get_all_post_info_use_case: Arc<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: Arc<dyn GetFullPostUseCase>,
}
impl PostControllerImpl {
pub fn new(
get_all_post_info_use_case: Box<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: Box<dyn GetFullPostUseCase>,
get_all_post_info_use_case: Arc<dyn GetAllPostInfoUseCase>,
get_full_post_use_case: Arc<dyn GetFullPostUseCase>,
) -> Self {
Self {
get_all_post_info_use_case,

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 {
@ -24,14 +22,12 @@ impl Container {
let post_repository = Arc::new(PostRepositoryImpl::new(post_db_service.clone()));
let get_all_post_info_use_case =
Box::new(GetAllPostInfoUseCaseImpl::new(post_repository.clone()));
let get_full_post_use_case = Box::new(GetFullPostUseCaseImpl::new(post_repository.clone()));
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)
}