use crate::{attribute_models::*, format_models::*};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct CommonParams {
pub format: Option<Format>,
pub attributes: Option<AttributesSelect>,
pub query: Option<String>,
pub filter: Option<FacetReq>,
pub per_page: Option<u32>,
pub page: Option<u32>,
pub sort: Option<SortField>,
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct SearchParams {
pub common: CommonParams,
pub include_collections: bool,
}
#[derive(Debug, Serialize, Clone, Default, Deserialize)]
pub struct ItemParams {
pub format: Option<Format>,
pub attributes: Option<ItemAttributes>,
}
#[derive(Debug, Serialize, Clone, Default, Deserialize)]
pub struct ResourceParams {
pub format: Option<Format>,
pub attributes: Option<ResourceAttributes>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FacetReq {
pub filters: Vec<Facet>,
}
impl FacetReq {
pub fn to_query_param(&self) -> String {
self.filters.iter().map(|f| f.to_string()).collect::<Vec<String>>().join("|")
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Facet {
AccessRestricted {
value: bool,
},
Contributor {
value: String,
},
Language {
value: String,
},
Subject {
value: String,
},
Location {
value: String,
},
OnlineFormat {
value: String,
},
Other {
key: String,
value: String,
},
}
impl Facet {
fn to_string(&self) -> String {
match self {
Facet::AccessRestricted { value } => format!("access_restricted:{}", value),
Facet::Contributor { value } => format!("contributor:{}", value.replace(" ", "+")),
Facet::Language { value } => format!("language:{}", value.replace(" ", "+")),
Facet::Subject { value } => format!("subject:{}", value.replace(" ", "+")),
Facet::Location { value } => format!("location:{}", value.replace(" ", "+")),
Facet::OnlineFormat { value } => format!("online-format:{}", value.replace(" ", "+")),
Facet::Other { key, value } => format!("{}:{}", key, value.replace(" ", "+")),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum QueryParam {
Common(CommonParams),
Search(SearchParams),
Collections(CommonParams),
Collection {
name: String,
params: CommonParams,
},
Format {
format: MediaType,
params: CommonParams,
},
Item {
item_id: String,
params: ItemParams,
},
Resource {
resource_id: String,
params: ResourceParams,
},
}