2020-12-12 13:32:06 +01:00
|
|
|
use actix_web::{delete, get, post, put};
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use serde_json::Value;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
use crate::Data;
|
2020-12-22 14:02:41 +01:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 13:32:06 +01:00
|
|
|
use crate::helpers::Authentication;
|
2020-12-22 14:02:41 +01:00
|
|
|
use crate::routes::IndexParam;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
|
|
|
type Document = IndexMap<String, Value>;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DocumentParam {
|
2020-12-22 14:02:41 +01:00
|
|
|
_index_uid: String,
|
|
|
|
_document_id: String,
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(get_document)
|
|
|
|
.service(delete_document)
|
|
|
|
.service(get_all_documents)
|
|
|
|
.service(add_documents)
|
|
|
|
.service(update_documents)
|
|
|
|
.service(delete_documents)
|
|
|
|
.service(clear_all_documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get(
|
|
|
|
"/indexes/{index_uid}/documents/{document_id}",
|
|
|
|
wrap = "Authentication::Public"
|
|
|
|
)]
|
|
|
|
async fn get_document(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<DocumentParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete(
|
|
|
|
"/indexes/{index_uid}/documents/{document_id}",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn delete_document(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<DocumentParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct BrowseQuery {
|
2020-12-22 14:02:41 +01:00
|
|
|
_offset: Option<usize>,
|
|
|
|
_limit: Option<usize>,
|
|
|
|
_attributes_to_retrieve: Option<String>,
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/documents", wrap = "Authentication::Public")]
|
|
|
|
async fn get_all_documents(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_params: web::Query<BrowseQuery>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 14:02:41 +01:00
|
|
|
//fn find_primary_key(document: &IndexMap<String, Value>) -> Option<String> {
|
|
|
|
//for key in document.keys() {
|
|
|
|
//if key.to_lowercase().contains("id") {
|
|
|
|
//return Some(key.to_string());
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
//None
|
|
|
|
//}
|
2020-12-12 13:32:06 +01:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct UpdateDocumentsQuery {
|
2020-12-22 14:02:41 +01:00
|
|
|
_primary_key: Option<String>,
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_multiple_documents(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
_body: web::Json<Vec<Document>>,
|
|
|
|
_is_partial: bool,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
|
|
|
async fn add_documents(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
_body: web::Json<Vec<Document>>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
|
|
|
async fn update_documents(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
params: web::Query<UpdateDocumentsQuery>,
|
|
|
|
body: web::Json<Vec<Document>>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
update_multiple_documents(data, path, params, body, true).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post(
|
|
|
|
"/indexes/{index_uid}/documents/delete-batch",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn delete_documents(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
|
|
|
_body: web::Json<Vec<Value>>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
|
|
|
|
async fn clear_all_documents(
|
2020-12-22 14:02:41 +01:00
|
|
|
_data: web::Data<Data>,
|
|
|
|
_path: web::Path<IndexParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2020-12-12 16:04:37 +01:00
|
|
|
todo!()
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|