implement retrieve all documents

This commit is contained in:
mpostma 2021-02-10 17:08:37 +01:00
parent f44f8a823a
commit 8fd9dc231c
No known key found for this signature in database
GPG key ID: CBC8A7C1D7A28C3A
3 changed files with 68 additions and 10 deletions

View file

@ -12,6 +12,9 @@ use crate::error::ResponseError;
use crate::helpers::Authentication;
use crate::routes::IndexParam;
const DEFAULT_RETRIEVE_DOCUMENTS_OFFSET: usize = 0;
const DEFAULT_RETRIEVE_DOCUMENTS_LIMIT: usize = 20;
macro_rules! guard_content_type {
($fn_name:ident, $guard_value:literal) => {
fn $fn_name(head: &actix_web::dev::RequestHead) -> bool {
@ -69,18 +72,35 @@ async fn delete_document(
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BrowseQuery {
_offset: Option<usize>,
_limit: Option<usize>,
_attributes_to_retrieve: Option<String>,
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<String>,
}
#[get("/indexes/{index_uid}/documents", wrap = "Authentication::Public")]
async fn get_all_documents(
_data: web::Data<Data>,
_path: web::Path<IndexParam>,
_params: web::Query<BrowseQuery>,
data: web::Data<Data>,
path: web::Path<IndexParam>,
params: web::Query<BrowseQuery>,
) -> Result<HttpResponse, ResponseError> {
todo!()
let attributes_to_retrieve = params
.attributes_to_retrieve
.as_ref()
.map(|attrs| attrs
.split(",")
.collect::<Vec<_>>());
match data.retrieve_documents(
&path.index_uid,
params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET),
params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT),
attributes_to_retrieve.as_deref()) {
Ok(docs) => {
let json = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(_) => { todo!() }
}
}
#[derive(Deserialize)]