-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add ports and adapters (hex architecture) structure (#45)
- Loading branch information
Showing
35 changed files
with
219 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use dsp_meta::errors::DspMetaError; | ||
use tracing::{trace, Level}; | ||
use tracing_subscriber::FmtSubscriber; | ||
|
||
fn main() -> Result<(), DspMetaError> { | ||
// configure tracing library | ||
let subscriber = FmtSubscriber::builder() | ||
.with_max_level(Level::TRACE) | ||
.finish(); | ||
|
||
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); | ||
|
||
trace!("Hello, world!"); | ||
|
||
dsp_meta::operation::server::run(); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pub(crate) mod dataset; | ||
pub(crate) mod grant; | ||
pub(crate) mod metadata; | ||
mod organization; | ||
mod person; | ||
mod project; | ||
pub mod project_metadata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#[derive(Debug, Default, PartialEq)] | ||
#[derive(Debug, Clone, Default, PartialEq)] | ||
pub struct Person { | ||
id: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod convert; | ||
pub mod server; | ||
pub mod validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use tracing::trace; | ||
|
||
use crate::repo::project_metadata_repository::ProjectMetadataRepository; | ||
use crate::service::project_metadata_service::ProjectMetadataService; | ||
|
||
pub fn run() { | ||
trace!("tracing from dsp_meta::core::server::run()"); | ||
let _project_metadata_service = ProjectMetadataService::new(ProjectMetadataRepository::new()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod project_metadata_repository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use tracing::trace; | ||
|
||
use crate::domain::entity::project_metadata::ProjectMetadata; | ||
use crate::domain::value::Shortcode; | ||
use crate::errors::DspMetaError; | ||
use crate::service::project_metadata_repository_contract::ProjectMetadataRepositoryContract; | ||
|
||
pub struct ProjectMetadataRepository { | ||
db: Arc<RwLock<HashMap<String, ProjectMetadata>>>, | ||
} | ||
|
||
impl ProjectMetadataRepository { | ||
pub fn new() -> Self { | ||
trace!("ProjectMetadataRepository::new"); | ||
let db: Arc<RwLock<HashMap<String, ProjectMetadata>>> = | ||
Arc::new(RwLock::new(HashMap::new())); | ||
Self { db } | ||
} | ||
} | ||
|
||
impl ProjectMetadataRepositoryContract for ProjectMetadataRepository { | ||
fn get_by_shortcode(&self, shortcode: Shortcode) -> Result<ProjectMetadata, DspMetaError> { | ||
let db = self.db.read().unwrap(); | ||
match db.get(shortcode.0.as_str()) { | ||
Some(metadata) => Ok(metadata.clone()), | ||
None => Err(DspMetaError::NotFound), | ||
} | ||
} | ||
|
||
fn get_all(&self) -> Result<Vec<ProjectMetadata>, DspMetaError> { | ||
let mut result: Vec<ProjectMetadata> = vec![]; | ||
let db = self.db.read().unwrap(); | ||
|
||
for project_metadata in db.values() { | ||
result.push(project_metadata.clone()); | ||
} | ||
|
||
Ok(result) | ||
} | ||
|
||
fn store(&self, shortcode: Shortcode, metadata: ProjectMetadata) -> Result<(), DspMetaError> { | ||
let mut db = self.db.write().unwrap(); | ||
db.insert(shortcode.0, metadata); | ||
Ok(()) | ||
} | ||
|
||
fn delete(&self, shortcode: Shortcode) -> Result<(), DspMetaError> { | ||
let mut db = self.db.write().unwrap(); | ||
|
||
match db.remove(shortcode.0.as_str()) { | ||
Some(_) => Ok(()), | ||
None => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn successfully_store_project_metadata() { | ||
let metadata = ProjectMetadata::default(); | ||
let shortcode = Shortcode("1234".to_owned()); | ||
|
||
let repo = ProjectMetadataRepository::new(); | ||
let result = repo.store(shortcode, metadata); | ||
assert!(result.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub(crate) mod project_metadata_api_contract; | ||
pub(crate) mod project_metadata_repository_contract; | ||
pub(crate) mod project_metadata_service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::domain::entity::project_metadata::ProjectMetadata; | ||
use crate::domain::value::Shortcode; | ||
use crate::errors::DspMetaError; | ||
|
||
pub trait ProjectMetadataApiContract { | ||
fn get_by_shortcode(&self, id: Shortcode) -> Result<ProjectMetadata, DspMetaError>; | ||
fn get_all(&self) -> Result<Vec<ProjectMetadata>, DspMetaError>; | ||
fn store(&self, id: Shortcode, metadata: ProjectMetadata) -> Result<(), DspMetaError>; | ||
fn delete(&self, id: Shortcode) -> Result<(), DspMetaError>; | ||
} |
Oops, something went wrong.