use ciborium::Value as CborValue;
use vantage_core::Result;
use vantage_dataset::traits::ReadableValueSet;
use vantage_table::column::core::Column;
use vantage_table::pagination::Pagination;
use vantage_table::sorting::{OrderBy, SortDirection};
use vantage_table::table::Table;
use vantage_types::{EmptyEntity, Record};
use crate::graphql::api::GraphqlApi;
use crate::graphql::operation::GraphqlOperation;
use crate::graphql::types::AnyGraphqlType;
use crate::graphql::vista::source::GraphqlApiTableShell;
impl GraphqlApiTableShell {
fn ordered_for_paging(&self) -> Table<GraphqlApi, EmptyEntity> {
let mut table = self.table.clone();
if !table.data_source().can_order() || table.orders().next().is_some() {
return table;
}
let Some(id) = table.id_field().map(|c| c.name().to_string()) else {
return table;
};
if id.contains('.') {
return table;
}
table.add_order(OrderBy {
expression: Column::<AnyGraphqlType>::new(&id).eq(id.as_str()),
direction: SortDirection::Ascending,
});
table
}
pub(crate) async fn list_paginated(
&self,
pagination: Pagination,
) -> Result<Vec<(String, Record<CborValue>)>> {
let mut table = self.ordered_for_paging();
table.set_pagination(Some(pagination));
let raw = table.list_values().await?;
Ok(raw
.into_iter()
.map(|(id, record)| (id, Self::record_to_cbor(record)))
.collect())
}
}