Repository: meilisearch/meilisearch
File: meilisearch-http/src/routes/indexes/documents.rs (Rust)

Code before:
        let mut names = Vec::new();
        for name in attrs.split(',').map(String::from) {
            if name == "*" {
                return None;
            }
            names.push(name);
        }
        Some(names)
    });

    let (total, documents) = meilisearch
        .documents(
            path.into_inner(),
            params.offset,
            params.limit,
            attributes_to_retrieve,
        )
        .await?;
    debug!("returns: {:?}", documents);
    Ok(HttpResponse::Ok().json(json!(
        { "limit": params.limit, "offset": params.offset, "total": total, "results": documents }
    )))
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdateDocumentsQuery {
    pub primary_key: Option<String>,
}

pub async fn add_documents(
    meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_ADD }>, MeiliSearch>,
    path: web::Path<String>,
    params: web::Query<UpdateDocumentsQuery>,
    body: Payload,
    req: HttpRequest,
    analytics: web::Data<dyn Analytics>,
) -> Result<HttpResponse, ResponseError> {
    debug!("called with params: {:?}", params);
    let params = params.into_inner();
    let index_uid = path.into_inner();

    analytics.add_documents(
        &params,
        meilisearch.get_index(index_uid.clone()).await.is_err(),
        &req,
    );

    let allow_index_creation = meilisearch.filters().allow_index_creation;
    let task = document_addition(
        extract_mime_type(&req)?,

Review comment (suggestion):
We can make a `ListView` type that id shared with all the other resources that return a list, such as listing indexes, or tasks

Code after:
    #[serde(default = "PAGINATION_DEFAULT_LIMIT")]
    limit: usize,
    fields: Option<CS<StarOr<String>>>,
}

pub async fn get_all_documents(
    meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_GET }>, MeiliSearch>,
    path: web::Path<String>,
    params: web::Query<BrowseQuery>,
) -> Result<HttpResponse, ResponseError> {
    debug!("called with params: {:?}", params);
    let BrowseQuery {
        offset,
        limit,
        fields,
    } = params.into_inner();
    let attributes_to_retrieve = fields.map(CS::into_inner).and_then(fold_star_or);

    let (total, documents) = meilisearch
        .documents(path.into_inner(), offset, limit, attributes_to_retrieve)
        .await?;

    debug!("returns: {:?}", documents);
    Ok(HttpResponse::Ok().json(json!(
        { "limit": limit, "offset": offset, "total": total, "results": documents }
    )))
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdateDocumentsQuery {
    pub primary_key: Option<String>,
}

pub async fn add_documents(
    meilisearch: GuardedData<ActionPolicy<{ actions::DOCUMENTS_ADD }>, MeiliSearch>,
    path: web::Path<String>,
    params: web::Query<UpdateDocumentsQuery>,
    body: Payload,
    req: HttpRequest,
    analytics: web::Data<dyn Analytics>,
) -> Result<HttpResponse, ResponseError> {
    debug!("called with params: {:?}", params);
    let params = params.into_inner();
    let index_uid = path.into_inner();

    analytics.add_documents(
        &params,
        meilisearch.get_index(index_uid.clone()).await.is_err(),
        &req,
    );