use super::*;
use getset::CopyGetters;
#[derive(Debug, Clone, Getters, CopyGetters)]
pub(crate) struct Ciboulette2PgBuilderState<'store, 'request> {
#[getset(get_copy = "pub")]
store: &'store CibouletteStore,
#[getset(get_copy = "pub")]
table_store: &'store Ciboulette2PgTableStore,
#[getset(get_copy = "pub")]
path: &'request CiboulettePath<'request>,
#[getset(get_copy = "pub")]
query: &'request CibouletteQueryParameters<'request>,
#[getset(get = "pub")]
inclusion_map: BTreeMap<
Vec<CibouletteResourceRelationshipDetails>,
(Ciboulette2PgResponseType, Vec<CibouletteSortingElement>),
>,
#[getset(get = "pub")]
main_type: Arc<CibouletteResourceType>,
#[getset(get = "pub")]
main_table: Arc<Ciboulette2PgTable>,
#[getset(get_copy = "pub")]
expected_response_type: Ciboulette2PgResponseType,
}
impl<'store, 'request> Ciboulette2PgBuilderState<'store, 'request>
where
'store: 'request,
{
fn build_inclusion_map(
query: &'request CibouletteQueryParameters<'request>
) -> BTreeMap<
Vec<CibouletteResourceRelationshipDetails>,
(Ciboulette2PgResponseType, Vec<CibouletteSortingElement>),
> {
let mut res: BTreeMap<
Vec<CibouletteResourceRelationshipDetails>,
(Ciboulette2PgResponseType, Vec<CibouletteSortingElement>),
> = BTreeMap::new();
for include_param in query.include() {
res.insert(
include_param.clone(),
(Ciboulette2PgResponseType::Object, Vec::default()),
);
}
for sort in query.sorting() {
if let Some(mut x) = res.insert(
sort.rel_chain().clone(),
(Ciboulette2PgResponseType::None, vec![sort.clone()]),
) {
if let Some((y, z)) = res.get_mut(sort.rel_chain()) {
*y = x.0;
z.append(&mut x.1);
}
}
}
res
}
pub fn new(
store: &'store CibouletteStore,
table_store: &'store Ciboulette2PgTableStore,
path: &'request CiboulettePath<'request>,
query: &'request CibouletteQueryParameters<'request>,
expected_response_type: Ciboulette2PgResponseType,
) -> Result<Self, Ciboulette2PgError> {
let main_type = path.main_type().clone();
let main_table = table_store.get(main_type.name().as_str())?.clone();
Ok(Ciboulette2PgBuilderState {
inclusion_map: Self::build_inclusion_map(query),
store,
table_store,
path,
query,
main_type,
main_table,
expected_response_type,
})
}
}