2020-12-23 23:12:37 +08:00
|
|
|
use actix_web::web::Payload;
|
2020-12-12 20:32:06 +08:00
|
|
|
use actix_web::{delete, get, post, put};
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use indexmap::IndexMap;
|
2020-12-23 23:12:37 +08:00
|
|
|
use log::error;
|
2021-02-27 00:14:11 +08:00
|
|
|
use milli::update::{IndexDocumentsMethod, UpdateFormat};
|
2020-12-12 20:32:06 +08:00
|
|
|
use serde::Deserialize;
|
2020-12-23 23:12:37 +08:00
|
|
|
use serde_json::Value;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
use crate::Data;
|
2020-12-22 21:02:41 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 20:32:06 +08:00
|
|
|
use crate::helpers::Authentication;
|
2020-12-22 21:02:41 +08:00
|
|
|
use crate::routes::IndexParam;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-02-11 00:08:37 +08:00
|
|
|
const DEFAULT_RETRIEVE_DOCUMENTS_OFFSET: usize = 0;
|
|
|
|
const DEFAULT_RETRIEVE_DOCUMENTS_LIMIT: usize = 20;
|
|
|
|
|
2020-12-23 23:12:37 +08:00
|
|
|
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");
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
type Document = IndexMap<String, Value>;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DocumentParam {
|
2021-02-13 17:44:20 +08:00
|
|
|
index_uid: String,
|
|
|
|
document_id: String,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(get_document)
|
|
|
|
.service(delete_document)
|
|
|
|
.service(get_all_documents)
|
2020-12-23 23:12:37 +08:00
|
|
|
.service(add_documents_json)
|
2020-12-12 20:32:06 +08:00
|
|
|
.service(update_documents)
|
|
|
|
.service(delete_documents)
|
|
|
|
.service(clear_all_documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get(
|
|
|
|
"/indexes/{index_uid}/documents/{document_id}",
|
|
|
|
wrap = "Authentication::Public"
|
|
|
|
)]
|
|
|
|
async fn get_document(
|
2021-02-11 17:59:23 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<DocumentParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-16 06:02:20 +08:00
|
|
|
let index = path.index_uid.clone();
|
|
|
|
let id = path.document_id.clone();
|
|
|
|
match data.retrieve_document(index, id, None as Option<Vec<String>>).await {
|
2021-02-11 17:59:23 +08:00
|
|
|
Ok(document) => {
|
|
|
|
let json = serde_json::to_string(&document).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-11 17:59:23 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete(
|
|
|
|
"/indexes/{index_uid}/documents/{document_id}",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn delete_document(
|
2021-02-13 17:44:20 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<DocumentParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-13 17:44:20 +08:00
|
|
|
match data.delete_documents(path.index_uid.clone(), vec![path.document_id.clone()]).await {
|
|
|
|
Ok(result) => {
|
|
|
|
let json = serde_json::to_string(&result).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-13 17:44:20 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct BrowseQuery {
|
2021-02-11 00:08:37 +08:00
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
attributes_to_retrieve: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/documents", wrap = "Authentication::Public")]
|
|
|
|
async fn get_all_documents(
|
2021-02-11 00:08:37 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Query<BrowseQuery>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-11 00:08:37 +08:00
|
|
|
let attributes_to_retrieve = params
|
|
|
|
.attributes_to_retrieve
|
|
|
|
.as_ref()
|
|
|
|
.map(|attrs| attrs
|
|
|
|
.split(",")
|
2021-02-16 06:02:20 +08:00
|
|
|
.map(String::from)
|
2021-02-11 00:08:37 +08:00
|
|
|
.collect::<Vec<_>>());
|
|
|
|
|
|
|
|
match data.retrieve_documents(
|
2021-02-16 06:02:20 +08:00
|
|
|
path.index_uid.clone(),
|
2021-02-11 00:08:37 +08:00
|
|
|
params.offset.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_OFFSET),
|
|
|
|
params.limit.unwrap_or(DEFAULT_RETRIEVE_DOCUMENTS_LIMIT),
|
2021-02-16 06:02:20 +08:00
|
|
|
attributes_to_retrieve).await {
|
2021-02-11 00:08:37 +08:00
|
|
|
Ok(docs) => {
|
|
|
|
let json = serde_json::to_string(&docs).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
2021-02-16 22:26:13 +08:00
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
}
|
2021-02-11 00:08:37 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct UpdateDocumentsQuery {
|
2021-02-13 19:22:59 +08:00
|
|
|
primary_key: Option<String>,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2020-12-23 23:12:37 +08:00
|
|
|
/// 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(
|
2020-12-23 20:52:28 +08:00
|
|
|
data: web::Data<Data>,
|
2020-12-23 23:12:37 +08:00
|
|
|
path: web::Path<IndexParam>,
|
2021-02-13 19:22:59 +08:00
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
2020-12-23 23:12:37 +08:00
|
|
|
body: Payload,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-27 00:14:11 +08:00
|
|
|
let addition_result = data
|
|
|
|
.add_documents(
|
|
|
|
path.into_inner().index_uid,
|
|
|
|
IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
UpdateFormat::Json,
|
|
|
|
body,
|
|
|
|
params.primary_key.clone(),
|
|
|
|
).await;
|
|
|
|
|
|
|
|
match addition_result {
|
|
|
|
Ok(update) => {
|
|
|
|
let value = serde_json::to_string(&update).unwrap();
|
|
|
|
let response = HttpResponse::Ok().body(value);
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 23:12:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-13 19:22:59 +08:00
|
|
|
/// Default route for adding documents, this should return an error and redirect to the documentation
|
2020-12-23 23:12:37 +08:00
|
|
|
#[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
|
|
|
async fn add_documents_default(
|
|
|
|
_data: web::Data<Data>,
|
2020-12-22 21:02:41 +08:00
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_params: web::Query<UpdateDocumentsQuery>,
|
2020-12-23 23:12:37 +08:00
|
|
|
_body: web::Json<Vec<Document>>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-04 19:34:12 +08:00
|
|
|
error!("Unknown document type");
|
2020-12-12 23:04:37 +08:00
|
|
|
todo!()
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-13 19:22:59 +08:00
|
|
|
/// Default route for adding documents, this should return an error and redirect to the documentation
|
2020-12-12 20:32:06 +08:00
|
|
|
#[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
2021-02-13 19:22:59 +08:00
|
|
|
async fn update_documents_default(
|
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
_body: web::Json<Vec<Document>>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
error!("Unknown document type");
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put(
|
|
|
|
"/indexes/{index_uid}/documents",
|
|
|
|
wrap = "Authentication::Private",
|
|
|
|
guard = "guard_json",
|
|
|
|
)]
|
2020-12-12 20:32:06 +08:00
|
|
|
async fn update_documents(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
2021-02-13 19:22:59 +08:00
|
|
|
body: web::Payload,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-26 16:10:04 +08:00
|
|
|
todo!()
|
|
|
|
//let addition_result = data
|
|
|
|
//.add_documents(
|
|
|
|
//path.into_inner().index_uid,
|
|
|
|
//IndexDocumentsMethod::UpdateDocuments,
|
|
|
|
//UpdateFormat::Json,
|
|
|
|
//body,
|
|
|
|
//params.primary_key.clone(),
|
|
|
|
//).await;
|
2021-02-13 19:22:59 +08:00
|
|
|
|
2021-02-26 16:10:04 +08:00
|
|
|
//match addition_result {
|
|
|
|
//Ok(update) => {
|
|
|
|
//let value = serde_json::to_string(&update).unwrap();
|
|
|
|
//let response = HttpResponse::Ok().body(value);
|
|
|
|
//Ok(response)
|
|
|
|
//}
|
|
|
|
//Err(e) => {
|
|
|
|
//Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
//}
|
|
|
|
//}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post(
|
|
|
|
"/indexes/{index_uid}/documents/delete-batch",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn delete_documents(
|
2021-02-13 00:39:14 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<Vec<Value>>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-26 16:10:04 +08:00
|
|
|
todo!()
|
|
|
|
//let ids = body
|
|
|
|
//.iter()
|
|
|
|
//.map(|v| v.as_str().map(String::from).unwrap_or_else(|| v.to_string()))
|
|
|
|
//.collect();
|
2021-02-13 00:39:14 +08:00
|
|
|
|
2021-02-26 16:10:04 +08:00
|
|
|
//match data.delete_documents(path.index_uid.clone(), ids).await {
|
|
|
|
//Ok(result) => {
|
|
|
|
//let json = serde_json::to_string(&result).unwrap();
|
|
|
|
//Ok(HttpResponse::Ok().body(json))
|
|
|
|
//}
|
|
|
|
//Err(e) => {
|
|
|
|
//Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
//}
|
|
|
|
//}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
|
|
|
async fn clear_all_documents(
|
2021-02-11 19:03:00 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-26 16:10:04 +08:00
|
|
|
todo!()
|
|
|
|
//match data.clear_documents(path.index_uid.clone()).await {
|
|
|
|
//Ok(update) => {
|
|
|
|
//let json = serde_json::to_string(&update).unwrap();
|
|
|
|
//Ok(HttpResponse::Ok().body(json))
|
|
|
|
//}
|
|
|
|
//Err(e) => {
|
|
|
|
//Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
//}
|
|
|
|
//}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|