Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jul 26, 2024
1 parent 7066053 commit 324ba70
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions dsp-meta/src/api/handler/project_metadata_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use dsp_domain::metadata::value::Shortcode;
use tracing::{info_span, instrument, trace};

use crate::api::model::project_metadata_dto::{ProjectMetadataDto, ProjectMetadataWithInfoDto};
use crate::api::model::project_metadata_graph_dto::ProjectMetadataGraphDto;
use crate::app_state::AppState;
use crate::domain::model::draft_model::Shortcode;
use crate::domain::service::project_metadata_api_contract::ProjectMetadataApiContract;
use crate::domain::service::repository_contract::{Filter, Pagination};
use crate::error::DspMetaError;
Expand All @@ -20,13 +20,13 @@ use crate::error::DspMetaError;
/// TODO: Add error handling with correct status codes
#[instrument(skip(state))]
pub async fn get_project_metadata_by_shortcode(
Path(shortcode): Path<String>,
Path(shortcode): Path<Shortcode>,
State(state): State<Arc<AppState>>,
) -> Result<Response, DspMetaError> {
trace!("entered get_project_metadata_by_shortcode()");
state
.project_metadata_service
.find_by_id(Shortcode(shortcode))
.find_by_id(shortcode)
.map(|option| match option {
Some(metadata) => (StatusCode::OK, ProjectMetadataDto(metadata)).into_response(),
None => (StatusCode::NOT_FOUND, "No project 9999 available").into_response(),
Expand Down
2 changes: 1 addition & 1 deletion dsp-meta/src/api/model/project_metadata_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl From<DraftMetadata> for ProjectMetadataWithInfoDto {
fn from(value: DraftMetadata) -> Self {
let project = value.project.clone();
ProjectMetadataWithInfoDto {
id: project.shortcode,
id: project.shortcode.0,
name: project.name,
description: project.teaser_text,
status: project.status.unwrap_or_default(),
Expand Down
9 changes: 6 additions & 3 deletions dsp-meta/src/domain/model/draft_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct DraftProject {
pub created_at: Option<String>,
#[serde(rename = "__createdBy")]
pub created_by: Option<String>,
pub shortcode: String,
pub shortcode: Shortcode,
pub status: Option<DraftProjectStatus>,
pub name: String,
pub description: Option<DraftText>,
Expand All @@ -50,6 +50,9 @@ pub struct DraftProject {
pub alternative_names: Option<NonEmpty<DraftText>>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Hash, Eq)]
pub struct Shortcode(pub String);

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
pub enum DraftProjectStatus {
#[default]
Expand Down Expand Up @@ -175,10 +178,10 @@ pub struct DraftGrant {
pub struct DraftText(HashMap<DraftIsoCode, String>);

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct DraftIsoCode(String);
pub struct DraftIsoCode(pub String);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DraftDate(NaiveDate);
pub struct DraftDate(pub NaiveDate);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down
4 changes: 1 addition & 3 deletions dsp-meta/src/domain/service/project_metadata_api_contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use dsp_domain::metadata::value::Shortcode;

use crate::domain::model::draft_model::DraftMetadata;
use crate::domain::model::draft_model::*;
use crate::domain::service::repository_contract::{Filter, Page, Pagination};
use crate::error::DspMetaError;

Expand Down
3 changes: 1 addition & 2 deletions dsp-meta/src/domain/service/project_metadata_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dsp_domain::metadata::value::Shortcode;
use tracing::{instrument, trace};

use crate::domain::model::draft_model::DraftMetadata;
use crate::domain::model::draft_model::*;
use crate::domain::service::project_metadata_api_contract::ProjectMetadataApiContract;
use crate::domain::service::repository_contract::{Filter, Page, Pagination, RepositoryContract};
use crate::error::DspMetaError;
Expand Down
13 changes: 6 additions & 7 deletions dsp-meta/src/repo/service/project_metadata_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@ use std::fs::File;
use std::path::Path;
use std::sync::{Arc, RwLock};

use dsp_domain::metadata::value::Shortcode;
use log::info;
use tracing::{instrument, trace};

use crate::domain::model::draft_model::{DraftMetadata, DraftProjectStatus};
use crate::domain::model::draft_model::*;
use crate::domain::service::repository_contract::{Filter, Page, Pagination, RepositoryContract};
use crate::error::DspMetaError;
use crate::infrastructure::load_json_file_paths;

#[derive(Debug, Default, Clone)]
pub struct ProjectMetadataRepository {
db: Arc<RwLock<HashMap<String, DraftMetadata>>>,
db: Arc<RwLock<HashMap<Shortcode, DraftMetadata>>>,
}

impl ProjectMetadataRepository {
pub fn new(data_path: &Path) -> Self {
info!("Init Repository {:?}", data_path);
let db: Arc<RwLock<HashMap<String, DraftMetadata>>> = Arc::new(RwLock::new(HashMap::new()));
let db: Arc<RwLock<HashMap<Shortcode, DraftMetadata>>> = Arc::new(RwLock::new(HashMap::new()));

let file_paths = load_json_file_paths(data_path);
info!("Found {} projects", file_paths.len());

let mut known_shortcodes: Vec<String> = Vec::new();
let mut known_shortcodes: Vec<Shortcode> = Vec::new();
for file in file_paths {
let file = File::open(file).expect("open file.");
let entity: DraftMetadata = serde_json::from_reader(file).expect("parse file as JSON.");
let mut db = db.write().unwrap();
let shortcode = entity.project.shortcode.to_owned();
if known_shortcodes.contains(&shortcode) {
panic!("Duplicate shortcode: {}", shortcode);
panic!("Duplicate shortcode: {:?}", shortcode);
}
known_shortcodes.push(shortcode);

Expand All @@ -52,7 +51,7 @@ impl RepositoryContract<DraftMetadata, Shortcode, DspMetaError> for ProjectMetad
#[instrument(skip(self))]
fn find_by_id(&self, id: &Shortcode) -> Result<Option<DraftMetadata>, DspMetaError> {
let db = self.db.read().unwrap();
match db.get(id.0.as_str()) {
match db.get(id) {
Some(metadata) => Ok(Some(metadata.clone())),
None => Ok(None),
}
Expand Down
4 changes: 2 additions & 2 deletions dsp-meta/tests/draft_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn test_draft_json_schema() {
#[test]
fn test_unique_shortcode() {
let path_bufs = load_json_file_paths(&data_dir());
let mut shortcodes: HashMap<String, PathBuf> = HashMap::new();
let mut shortcodes: HashMap<Shortcode, PathBuf> = HashMap::new();
for path_buf in path_bufs {
let path = path_buf.as_path();
let file = File::open(path).expect("Should have been able to open the file");
Expand All @@ -100,7 +100,7 @@ fn test_unique_shortcode() {
if shortcodes.contains_key(&shortcode) {
let last_path = shortcodes.get(&shortcode).unwrap();
panic!(
"Shortcode {} is not unique!\nFound in files '{}' and '{}'. Please fix this.",
"Shortcode {:?} is not unique!\nFound in files '{}' and '{}'. Please fix this.",
shortcode,
path.file_name()
.and_then(|os| os.to_str())
Expand Down

0 comments on commit 324ba70

Please sign in to comment.