use std::sync::Arc; use async_trait::async_trait; use crate::{ adapter::delivery::{ create_label_request_dto::CreateLabelRequestDto, post_info_query_dto::PostQueryDto, update_label_request_dto::UpdateLabelRequestDto, }, application::{ error::post_error::PostError, use_case::{ create_label_use_case::CreateLabelUseCase, get_all_labels_use_case::GetAllLabelsUseCase, get_all_post_info_use_case::GetAllPostInfoUseCase, get_full_post_use_case::GetFullPostUseCase, update_label_use_case::UpdateLabelUseCase, }, }, }; use super::{ label_response_dto::LabelResponseDto, post_info_response_dto::PostInfoResponseDto, post_response_dto::PostResponseDto, }; #[async_trait] pub trait PostController: Send + Sync { async fn get_all_post_info( &self, query: PostQueryDto, ) -> Result, PostError>; async fn get_post_by_id(&self, id: i32) -> Result; async fn create_label( &self, label: CreateLabelRequestDto, ) -> Result; async fn update_label( &self, id: i32, label: UpdateLabelRequestDto, ) -> Result; async fn get_all_labels(&self) -> Result, PostError>; } pub struct PostControllerImpl { get_all_post_info_use_case: Arc, get_full_post_use_case: Arc, create_label_use_case: Arc, update_label_use_case: Arc, get_all_labels_use_case: Arc, } impl PostControllerImpl { pub fn new( get_all_post_info_use_case: Arc, get_full_post_use_case: Arc, create_label_use_case: Arc, update_label_use_case: Arc, get_all_labels_use_case: Arc, ) -> Self { Self { get_all_post_info_use_case, get_full_post_use_case, create_label_use_case, update_label_use_case, get_all_labels_use_case, } } } #[async_trait] impl PostController for PostControllerImpl { async fn get_all_post_info( &self, query: PostQueryDto, ) -> Result, PostError> { let result = self .get_all_post_info_use_case .execute(query.is_published_only.unwrap_or(true)) .await; result.map(|post_info_list| { let post_info_response_dto_list: Vec = post_info_list .into_iter() .map(|post_info| PostInfoResponseDto::from(post_info)) .collect(); post_info_response_dto_list }) } async fn get_post_by_id(&self, id: i32) -> Result { let result = self.get_full_post_use_case.execute(id).await; result.map(PostResponseDto::from) } async fn create_label( &self, label: CreateLabelRequestDto, ) -> Result { let mut label_entity = label.into_entity(); let id = self .create_label_use_case .execute(label_entity.clone()) .await?; label_entity.id = id; Ok(LabelResponseDto::from(label_entity)) } async fn update_label( &self, id: i32, label: UpdateLabelRequestDto, ) -> Result { let label_entity = label.into_entity(id); self.update_label_use_case .execute(label_entity.clone()) .await?; Ok(LabelResponseDto::from(label_entity)) } async fn get_all_labels(&self) -> Result, PostError> { let result = self.get_all_labels_use_case.execute().await; result.map(|labels| { labels .into_iter() .map(|label| LabelResponseDto::from(label)) .collect() }) } }