Skip to content

Commit

Permalink
Merge pull request #17 from husni-zuhdi/refactor-port-repo-and-usecase
Browse files Browse the repository at this point in the history
Refactor port repo and usecase
  • Loading branch information
husni-zuhdi authored Sep 18, 2024
2 parents 2c7ffdd + b01816d commit f6b4426
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 245 deletions.
12 changes: 0 additions & 12 deletions build/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ services:
- "../.env"
ports:
- ${SVC_PORT}:${SVC_PORT}
db:
image: postgres:12-alpine
restart: always
env_file:
- "../.env"
ports:
- 5432:5432
volumes:
- db-data:/var/lib/postgresql/data

volumes:
db-data: {}
46 changes: 27 additions & 19 deletions internal/src/api/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,50 @@ pub struct FilesystemApiUseCase {

#[async_trait]
impl ApiRepo for FilesystemApiUseCase {
async fn list_metadata(&self) -> Vec<BlogMetadata> {
async fn list_metadata(&self) -> Option<Vec<BlogMetadata>> {
let read_dir = fs::read_dir(self.blogs_dir.clone());
let blogs_metadata: Vec<BlogMetadata> = match read_dir {
match read_dir {
Ok(value) => {
// Collect Blog Filename
value
let metadatas = value
.filter_map(|blog_path| {
let blog_path_buf = blog_path.expect("Failed to get blog DirEntry").path();
Self::process_blog_path(&self, blog_path_buf)
})
// Collect Blog Metadata
.map(|blog_filename| Self::process_blog_metadata(&self, blog_filename))
.collect()
.collect();
Some(metadatas)
}
Err(err) => {
error!(
"Failed to read directory. Returned empty Vector. Error: {}",
err
);
let value: Vec<BlogMetadata> = Vec::new();
value
None
}
};
blogs_metadata
}
}
async fn fetch(&self, metadata: BlogMetadata) -> Blog {
let body = Self::process_markdown(metadata.filename.0.clone())
.expect("Failed to convert markdown to html");
debug!("Blog Body with Id {}: {}", &metadata.id.0, &body);
async fn fetch(&self, metadata: BlogMetadata) -> Option<Blog> {
let result = Self::process_markdown(metadata.filename.0.clone());
match result {
Ok(body) => {
debug!("Blog Body with Id {}: {}", &metadata.id.0, &body);

Blog {
id: metadata.id,
name: metadata.name,
source: BlogSource::Filesystem,
filename: metadata.filename,
body: BlogBody(body),
Some(Blog {
id: metadata.id,
name: metadata.name,
source: BlogSource::Filesystem,
filename: metadata.filename,
body: BlogBody(body),
})
}
Err(err) => {
error!(
"Failed to process markdown to html for Blog Id {}. Error: {}",
&metadata.id.0, err
);
None
}
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions internal/src/api/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct GithubApiUseCase {

#[async_trait]
impl ApiRepo for GithubApiUseCase {
async fn list_metadata(&self) -> Vec<BlogMetadata> {
async fn list_metadata(&self) -> Option<Vec<BlogMetadata>> {
let trees_result = Self::fetch_github_trees(&self).await;

let mut blogs_metadata: Vec<BlogMetadata> = Vec::new();
Expand All @@ -43,12 +43,12 @@ impl ApiRepo for GithubApiUseCase {
error!("Failed to filter Github Trees result")
}
};
blogs_metadata
Some(blogs_metadata)
}
async fn fetch(&self, metadata: BlogMetadata) -> Blog {
let content = Self::fetch_github_content(&self, metadata.filename.clone()).await;
async fn fetch(&self, metadata: BlogMetadata) -> Option<Blog> {
let result = Self::fetch_github_content(&self, metadata.filename.clone()).await;

let blog = match content {
match result {
Some(content) => Self::process_github_content(&self, content, metadata),
None => {
error!(
Expand All @@ -57,8 +57,7 @@ impl ApiRepo for GithubApiUseCase {
);
None
}
};
blog.unwrap()
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion internal/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Config {
pub environment: String,
pub data_source: String,
pub database_url: String,
pub filesystem_dir: String,
pub gh_owner: String,
pub gh_repo: String,
pub gh_branch: String,
Expand All @@ -24,7 +25,8 @@ impl Default for Config {
let log_level = tracing::Level::INFO;
let environment: String = "release".to_string();
let data_source: String = "memory".to_string();
let database_url: String = "".to_owned();
let database_url: String = "".to_string();
let filesystem_dir: String = "".to_string();
let gh_owner: String = "".to_string();
let gh_repo: String = "".to_string();
let gh_branch: String = "".to_string();
Expand All @@ -36,6 +38,7 @@ impl Default for Config {
environment,
data_source,
database_url,
filesystem_dir,
gh_owner,
gh_repo,
gh_branch,
Expand Down Expand Up @@ -89,6 +92,7 @@ impl Config {
let environment: String = Self::parse_optional_envar("ENVIRONMENT", "release");
let data_source: String = Self::parse_optional_envar("DATA_SOURCE", "memory");
let database_url: String = Self::parse_optional_envar("DATABASE_URL", "");
let filesystem_dir: String = Self::parse_optional_envar("FILESYSTEM_DIR", "");
let gh_owner: String = Self::parse_optional_envar("GITHUB_OWNER", "");
let gh_repo: String = Self::parse_optional_envar("GITHUB_REPO", "");
let gh_branch: String = Self::parse_optional_envar("GITHUB_BRANCH", "");
Expand All @@ -100,6 +104,7 @@ impl Config {
environment,
data_source,
database_url,
filesystem_dir,
gh_owner,
gh_repo,
gh_branch,
Expand All @@ -119,6 +124,7 @@ mod test {
let environment: String = "release".to_string();
let data_source: String = "memory".to_string();
let database_url: String = "".to_string();
let filesystem_dir: String = "".to_string();
let gh_owner: String = "".to_string();
let gh_repo: String = "".to_string();
let gh_branch: String = "".to_string();
Expand All @@ -131,6 +137,7 @@ mod test {
assert_eq!(result.environment, environment);
assert_eq!(result.data_source, data_source);
assert_eq!(result.database_url, database_url);
assert_eq!(result.filesystem_dir, filesystem_dir);
assert_eq!(result.gh_owner, gh_owner);
assert_eq!(result.gh_repo, gh_repo);
assert_eq!(result.gh_branch, gh_branch);
Expand All @@ -147,6 +154,7 @@ mod test {
let data_source = "";
let expected_data_source = "memory";
let database_url = "";
let filesystem_dir = "";
let gh_owner = "";
let gh_repo = "";
let gh_branch = "";
Expand All @@ -158,6 +166,7 @@ mod test {
environment,
data_source,
database_url,
filesystem_dir,
gh_owner,
gh_repo,
gh_branch,
Expand All @@ -171,6 +180,7 @@ mod test {
assert_eq!(result.environment, expected_environment);
assert_eq!(result.data_source, expected_data_source);
assert_eq!(result.database_url, database_url);
assert_eq!(result.filesystem_dir, filesystem_dir);
assert_eq!(result.gh_owner, gh_owner);
assert_eq!(result.gh_repo, gh_repo);
assert_eq!(result.gh_branch, gh_branch);
Expand All @@ -187,6 +197,7 @@ mod test {
let environment = "dev";
let data_source = "sqlite";
let database_url = "sqlite:husni-portfolio.db";
let filesystem_dir = "";
let gh_owner = "husni-zuhdi";
let gh_repo = "husni-blog-resources";
let gh_branch = "main";
Expand All @@ -198,6 +209,7 @@ mod test {
environment,
data_source,
database_url,
filesystem_dir,
gh_owner,
gh_repo,
gh_branch,
Expand All @@ -211,6 +223,7 @@ mod test {
assert_eq!(result.environment, environment);
assert_eq!(result.data_source, data_source);
assert_eq!(result.database_url, database_url);
assert_eq!(result.filesystem_dir, filesystem_dir);
assert_eq!(result.gh_owner, gh_owner);
assert_eq!(result.gh_repo, gh_repo);
assert_eq!(result.gh_branch, gh_branch);
Expand All @@ -225,6 +238,7 @@ mod test {
environment: &str,
data_source: &str,
database_url: &str,
filesystem_dir: &str,
gh_owner: &str,
gh_repo: &str,
gh_branch: &str,
Expand All @@ -235,6 +249,7 @@ mod test {
env::set_var("ENVIRONMENT", environment);
env::set_var("DATA_SOURCE", data_source);
env::set_var("DATABASE_URL", database_url);
env::set_var("FILESYSTEM_DIR", filesystem_dir);
env::set_var("GITHUB_OWNER", gh_owner);
env::set_var("GITHUB_REPO", gh_repo);
env::set_var("GITHUB_BRANCH", gh_branch);
Expand All @@ -247,6 +262,7 @@ mod test {
env::remove_var("ENVIRONMENT");
env::remove_var("DATA_SOURCE");
env::remove_var("DATABASE_URL");
env::remove_var("FILESYSTEM_DIR");
env::remove_var("GITHUB_OWNER");
env::remove_var("GITHUB_REPO");
env::remove_var("GITHUB_BRANCH");
Expand Down
Loading

0 comments on commit f6b4426

Please sign in to comment.