Skip to content

Commit

Permalink
Merge pull request #16 from husni-zuhdi/chore-update-state-factory
Browse files Browse the repository at this point in the history
chore: create populate_blog from api usecase
  • Loading branch information
husni-zuhdi authored Sep 11, 2024
2 parents f16b514 + 629a6a0 commit 2c7ffdd
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions internal/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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));
Expand All @@ -82,3 +47,22 @@ pub async fn state_factory(config: Config) -> AppState {
blog_usecase,
}
}

async fn populate_blog(api_uc: Box<dyn ApiRepo + Send + Sync>, 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);
}
}
}

0 comments on commit 2c7ffdd

Please sign in to comment.