From 629a6a0a30f39ef64d762ea3e00fd9f3970cb97e Mon Sep 17 00:00:00 2001 From: "husni.zuhdi@accelbyte.net" Date: Thu, 12 Sep 2024 00:06:39 +0700 Subject: [PATCH] chore: create populate_blog from api usecase --- internal/src/state.rs | 58 ++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/internal/src/state.rs b/internal/src/state.rs index 06a5d4f..2059504 100644 --- a/internal/src/state.rs +++ b/internal/src/state.rs @@ -20,34 +20,15 @@ pub async fn state_factory(config: Config) -> AppState { !config.gh_owner.is_empty() && !config.gh_repo.is_empty() && !config.gh_branch.is_empty(); let mut blog_uc = if data_source_is_configured_sqlite { - // Use SqliteBlogRepo let repo = SqliteBlogRepo::new(config.database_url.clone()).await; BlogUseCase::new(Box::new(repo)) } else { - // // Use MemoryBlogRepo let repo = MemoryBlogRepo::default(); BlogUseCase::new(Box::new(repo)) - // } }; let fs_usecase = FilesystemApiUseCase::new("./statics/blogs/".to_string()).await; - let blogs_metadata = fs_usecase.list_metadata().await; - for metadata in blogs_metadata { - // Check if blog id is in the database - let blog_is_not_stored = !blog_uc.check_id(metadata.id.clone()).await.0; - if blog_is_not_stored { - info!("Start to populate Blog {}.", &metadata.id); - debug!("Start to fetch Blog {}.", &metadata.id); - let blog = fs_usecase.fetch(metadata.clone()).await; - debug!("Finished to fetch Blog {}.", &metadata.id); - - debug!("Start to store Blog {}.", &metadata.id); - let _ = blog_uc - .add(blog.id, blog.name, blog.filename, blog.source, blog.body) - .await; - debug!("Finished to store Blog {}.", &metadata.id); - } - } + let _ = populate_blog(Box::new(fs_usecase), &mut blog_uc).await; if github_api_is_enabled { let github_usecase = GithubApiUseCase::new( @@ -56,23 +37,7 @@ pub async fn state_factory(config: Config) -> AppState { config.gh_branch.clone(), ) .await; - let blogs_metadata = github_usecase.list_metadata().await; - for metadata in blogs_metadata { - // Check if blog id is in the database - let blog_is_not_stored = !blog_uc.check_id(metadata.id.clone()).await.0; - if blog_is_not_stored { - info!("Start to populate Blog {}.", &metadata.id); - debug!("Start to fetch Blog {}.", &metadata.id); - let blog = github_usecase.fetch(metadata.clone()).await; - debug!("Finished to fetch Blog {}.", &metadata.id); - - debug!("Start to store Blog {}.", &metadata.id); - let _ = blog_uc - .add(blog.id, blog.name, blog.filename, blog.source, blog.body) - .await; - debug!("Finished to store Blog {}.", &metadata.id); - } - } + let _ = populate_blog(Box::new(github_usecase), &mut blog_uc).await; } let blog_usecase = Arc::new(Mutex::new(blog_uc)); @@ -82,3 +47,22 @@ pub async fn state_factory(config: Config) -> AppState { blog_usecase, } } + +async fn populate_blog(api_uc: Box, blog_uc: &mut BlogUseCase) { + let blogs_metadata = api_uc.list_metadata().await; + for metadata in blogs_metadata { + let blog_is_not_stored = !blog_uc.check_id(metadata.id.clone()).await.0; + if blog_is_not_stored { + info!("Start to populate Blog {}.", &metadata.id); + debug!("Start to fetch Blog {}.", &metadata.id); + let blog = api_uc.fetch(metadata.clone()).await; + debug!("Finished to fetch Blog {}.", &metadata.id); + + debug!("Start to store Blog {}.", &metadata.id); + let _ = blog_uc + .add(blog.id, blog.name, blog.filename, blog.source, blog.body) + .await; + debug!("Finished to store Blog {}.", &metadata.id); + } + } +}