Compare commits

..

1 Commits

Author SHA1 Message Date
Yu Squire[ Yu, Tsung-Ying ]
5ea5879ad7 BLOG-56 refactor: make use cases stateless
Some checks failed
Frontend CI / build (push) Successful in 1m27s
PR Title Check / pr-title-check (pull_request) Failing after 16s
2025-07-01 11:39:42 +08:00
4 changed files with 26 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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