use actix_web::web::Payload; use actix_web::{delete, get, post, put}; use actix_web::{web, HttpResponse}; use indexmap::IndexMap; use log::error; use milli::update::{IndexDocumentsMethod, UpdateFormat}; use serde::Deserialize; use serde_json::Value; use crate::Data; use crate::error::ResponseError; use crate::helpers::Authentication; use crate::routes::IndexParam; macro_rules! guard_content_type { ($fn_name:ident, $guard_value:literal) => { fn $fn_name(head: &actix_web::dev::RequestHead) -> bool { if let Some(content_type) = head.headers.get("Content-Type") { content_type.to_str().map(|v| v.contains($guard_value)).unwrap_or(false) } else { false } } }; } guard_content_type!(guard_json, "application/json"); type Document = IndexMap; #[derive(Deserialize)] struct DocumentParam { _index_uid: String, _document_id: String, } pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(get_document) .service(delete_document) .service(get_all_documents) .service(add_documents_json) .service(update_documents) .service(delete_documents) .service(clear_all_documents); } #[get( "/indexes/{index_uid}/documents/{document_id}", wrap = "Authentication::Public" )] async fn get_document( _data: web::Data, _path: web::Path, ) -> Result { todo!() } #[delete( "/indexes/{index_uid}/documents/{document_id}", wrap = "Authentication::Private" )] async fn delete_document( _data: web::Data, _path: web::Path, ) -> Result { todo!() } #[derive(Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] struct BrowseQuery { _offset: Option, _limit: Option, _attributes_to_retrieve: Option, } #[get("/indexes/{index_uid}/documents", wrap = "Authentication::Public")] async fn get_all_documents( _data: web::Data, _path: web::Path, _params: web::Query, ) -> Result { todo!() } #[derive(Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] struct UpdateDocumentsQuery { _primary_key: Option, } async fn update_multiple_documents( _data: web::Data, _path: web::Path, _params: web::Query, _body: web::Json>, _is_partial: bool, ) -> Result { todo!() } /// Route used when the payload type is "application/json" #[post( "/indexes/{index_uid}/documents", wrap = "Authentication::Private", guard = "guard_json" )] async fn add_documents_json( data: web::Data, path: web::Path, _params: web::Query, body: Payload, ) -> Result { let addition_result = data .add_documents( path.into_inner().index_uid, IndexDocumentsMethod::UpdateDocuments, UpdateFormat::Json, body ).await; match addition_result { Ok(update) => { let value = serde_json::to_string(&update).unwrap(); let response = HttpResponse::Ok().body(value); Ok(response) } Err(e) => { error!("{}", e); todo!() } } } /// Default route for addign documents, this should return an error en redirect to the docuentation #[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn add_documents_default( _data: web::Data, _path: web::Path, _params: web::Query, _body: web::Json>, ) -> Result { error!("Unknown document type"); todo!() } #[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn update_documents( data: web::Data, path: web::Path, params: web::Query, body: web::Json>, ) -> Result { update_multiple_documents(data, path, params, body, true).await } #[post( "/indexes/{index_uid}/documents/delete-batch", wrap = "Authentication::Private" )] async fn delete_documents( _data: web::Data, _path: web::Path, _body: web::Json>, ) -> Result { todo!() } #[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn clear_all_documents( data: web::Data, path: web::Path, ) -> Result { match data.clear_documents(&path.index_uid).await { Ok(update) => { let json = serde_json::to_string(&update).unwrap(); Ok(HttpResponse::Ok().body(json)) } Err(e) => { error!("{}", e); unimplemented!(); } } }