Skip to content

Commit

Permalink
add filter and query
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jul 25, 2024
1 parent 938a2d2 commit f962ce8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
6 changes: 4 additions & 2 deletions dsp-meta/src/api/handler/project_metadata_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::api::model::project_metadata_dto::{ProjectMetadataDto, ProjectMetadat
use crate::api::model::project_metadata_graph_dto::ProjectMetadataGraphDto;
use crate::app_state::AppState;
use crate::domain::service::project_metadata_api_contract::ProjectMetadataApiContract;
use crate::domain::service::repository_contract::Pagination;
use crate::domain::service::repository_contract::{Filter, Pagination};
use crate::error::DspMetaError;

/// GET /project_metadata/:shortcode
Expand Down Expand Up @@ -54,10 +54,12 @@ pub async fn get_project_metadata_by_shortcode_as_rdf(
pub async fn get_all_project_metadata(
State(state): State<Arc<AppState>>,
pagination: Option<Query<Pagination>>,
filter: Option<Query<Filter>>,
) -> Result<Response, DspMetaError> {
trace!("entered get_all_project_metadata()");
let Query(pagination) = pagination.unwrap_or_default();
let page = state.project_metadata_service.find(&pagination)?;
let Query(filter) = filter.unwrap_or_default();
let page = state.project_metadata_service.find(&filter, &pagination)?;
let mut response = Json(
page.data
.into_iter()
Expand Down
8 changes: 6 additions & 2 deletions dsp-meta/src/domain/service/project_metadata_api_contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use dsp_domain::metadata::value::Shortcode;

use crate::api::convert::serde::draft_model::DraftMetadata;
use crate::domain::service::repository_contract::{Page, Pagination};
use crate::domain::service::repository_contract::{Filter, Page, Pagination};
use crate::error::DspMetaError;

pub trait ProjectMetadataApiContract {
fn find_by_id(&self, id: Shortcode) -> Result<Option<DraftMetadata>, DspMetaError>;
fn find(&self, pagination: &Pagination) -> Result<Page<DraftMetadata>, DspMetaError>;
fn find(
&self,
filter: &Filter,
pagination: &Pagination,
) -> Result<Page<DraftMetadata>, DspMetaError>;
}
10 changes: 7 additions & 3 deletions dsp-meta/src/domain/service/project_metadata_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing::{instrument, trace};

use crate::api::convert::serde::draft_model::DraftMetadata;
use crate::domain::service::project_metadata_api_contract::ProjectMetadataApiContract;
use crate::domain::service::repository_contract::{Page, Pagination, RepositoryContract};
use crate::domain::service::repository_contract::{Filter, Page, Pagination, RepositoryContract};
use crate::error::DspMetaError;

#[derive(Debug, Clone)]
Expand All @@ -30,7 +30,11 @@ where
}

#[instrument(skip(self))]
fn find(&self, pagination: &Pagination) -> Result<Page<DraftMetadata>, DspMetaError> {
self.repo.find(pagination)
fn find(
&self,
filter: &Filter,
pagination: &Pagination,
) -> Result<Page<DraftMetadata>, DspMetaError> {
self.repo.find(filter, pagination)
}
}
11 changes: 9 additions & 2 deletions dsp-meta/src/domain/service/repository_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ impl Default for Pagination {
}
}

#[derive(Deserialize, Default, Debug)]
pub struct Filter {
#[serde(rename = "q")]
pub query: Option<String>,
#[serde(rename = "filter")]
pub filter: Option<String>,
}

pub struct Page<T> {
pub data: Vec<T>,
pub total: usize,
}

/// The contract for the project metadata repository.
/// It defines the methods that the repository must implement.
/// The trait is generically typed for the entity type `Entity`, the id type `Id`, and
Expand All @@ -28,7 +35,7 @@ pub trait RepositoryContract<Entity, Id, Error> {
fn find_by_id(&self, id: &Id) -> Result<Option<Entity>, Error>;

/// Returns all entities.
fn find(&self, pagination: &Pagination) -> Result<Page<Entity>, Error>;
fn find(&self, filter: &Filter, pagination: &Pagination) -> Result<Page<Entity>, Error>;

/// Returns the number of entities.
fn count(&self) -> Result<usize, Error>;
Expand Down
49 changes: 38 additions & 11 deletions dsp-meta/src/repo/service/project_metadata_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::sync::{Arc, RwLock};
use dsp_domain::metadata::value::Shortcode;
use tracing::{instrument, trace};

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

Expand Down Expand Up @@ -58,19 +58,46 @@ impl RepositoryContract<DraftMetadata, Shortcode, DspMetaError> for ProjectMetad
}

#[instrument(skip(self))]
fn find(&self, pagination: &Pagination) -> Result<Page<DraftMetadata>, DspMetaError> {
trace!("repository: find_all");
fn find(
&self,
filter: &Filter,
pagination: &Pagination,
) -> Result<Page<DraftMetadata>, DspMetaError> {
let db = self.db.read().unwrap();
let query_status: Option<Vec<DraftProjectStatus>> = match filter.filter.as_deref() {
Some("o") => Some(vec![DraftProjectStatus::Ongoing]),
Some("f") => Some(vec![DraftProjectStatus::Finished]),
Some("of") => Some(vec![
DraftProjectStatus::Ongoing,
DraftProjectStatus::Finished,
]),
_ => None,
};

let values = db
.values()
.filter(|metadata| {
if let Some(query_status) = &query_status {
let actual_status = &metadata.project.status.clone().unwrap_or_default();
!query_status.contains(actual_status)
} else {
true
}
})
.filter(|metadata| {
if let Some(query) = &filter.query {
serde_json::to_string(metadata).unwrap().contains(query)
} else {
true
}
}).cloned().collect::<Vec<DraftMetadata>>();
let total = values.len();
let data = values
.into_iter()
.skip((pagination.page - 1) * pagination.limit)
.take(pagination.limit);
let result = values.cloned().collect();
let total = self.count()?;
Ok(Page {
data: result,
total,
})
.take(pagination.limit)
.collect::<Vec<DraftMetadata>>();
Ok(Page { data, total })
}

fn count(&self) -> Result<usize, DspMetaError> {
Expand Down

0 comments on commit f962ce8

Please sign in to comment.