2021-03-16 01:11:10 +08:00
|
|
|
use actix_web::{delete, get, post, web, HttpResponse};
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
use crate::error::ResponseError;
|
|
|
|
use crate::helpers::Authentication;
|
2021-03-16 01:11:10 +08:00
|
|
|
use crate::index::Settings;
|
|
|
|
use crate::Data;
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! make_setting_route {
|
|
|
|
($route:literal, $type:ty, $attr:ident) => {
|
|
|
|
mod $attr {
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
|
|
|
|
use crate::data;
|
|
|
|
use crate::error::ResponseError;
|
|
|
|
use crate::helpers::Authentication;
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::Settings;
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
#[actix_web::delete($route, wrap = "Authentication::Private")]
|
|
|
|
pub async fn delete(
|
|
|
|
data: web::Data<data::Data>,
|
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-04 18:56:32 +08:00
|
|
|
use crate::index::Settings;
|
2021-01-05 07:21:42 +08:00
|
|
|
let settings = Settings {
|
|
|
|
$attr: Some(None),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-03-12 05:33:31 +08:00
|
|
|
match data.update_settings(index_uid.into_inner(), settings, false).await {
|
2021-04-01 21:50:45 +08:00
|
|
|
Ok(update_status) => {
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
|
|
|
}
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::post($route, wrap = "Authentication::Private")]
|
|
|
|
pub async fn update(
|
|
|
|
data: actix_web::web::Data<data::Data>,
|
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
body: actix_web::web::Json<Option<$type>>,
|
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = Settings {
|
|
|
|
$attr: Some(body.into_inner()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2021-03-12 05:33:31 +08:00
|
|
|
match data.update_settings(index_uid.into_inner(), settings, true).await {
|
2021-04-01 21:50:45 +08:00
|
|
|
Ok(update_status) => {
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() })))
|
|
|
|
}
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::get($route, wrap = "Authentication::Private")]
|
|
|
|
pub async fn get(
|
|
|
|
data: actix_web::web::Data<data::Data>,
|
|
|
|
index_uid: actix_web::web::Path<String>,
|
|
|
|
) -> std::result::Result<HttpResponse, ResponseError> {
|
2021-03-15 23:52:05 +08:00
|
|
|
match data.settings(index_uid.into_inner()).await {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(settings) => Ok(HttpResponse::Ok().json(settings.$attr)),
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/attributes-for-faceting",
|
|
|
|
std::collections::HashMap<String, String>,
|
2021-03-15 20:53:50 +08:00
|
|
|
attributes_for_faceting
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/displayed-attributes",
|
|
|
|
Vec<String>,
|
|
|
|
displayed_attributes
|
|
|
|
);
|
|
|
|
|
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/searchable-attributes",
|
|
|
|
Vec<String>,
|
|
|
|
searchable_attributes
|
|
|
|
);
|
|
|
|
|
2021-04-06 21:41:03 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/stop-words",
|
|
|
|
std::collections::BTreeSet<String>,
|
|
|
|
stop_words
|
|
|
|
);
|
|
|
|
|
2021-04-20 17:28:16 +08:00
|
|
|
make_setting_route!(
|
|
|
|
"/indexes/{index_uid}/settings/distinct-attribute",
|
|
|
|
String,
|
|
|
|
distinct_attribute
|
|
|
|
);
|
2021-01-05 07:21:42 +08:00
|
|
|
|
|
|
|
//make_setting_route!(
|
2021-03-16 01:11:10 +08:00
|
|
|
//"/indexes/{index_uid}/settings/ranking-rules",
|
|
|
|
//Vec<String>,
|
|
|
|
//ranking_rules
|
2021-01-05 07:21:42 +08:00
|
|
|
//);
|
|
|
|
|
|
|
|
macro_rules! create_services {
|
|
|
|
($($mod:ident),*) => {
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg
|
|
|
|
.service(update_all)
|
|
|
|
.service(get_all)
|
|
|
|
.service(delete_all)
|
|
|
|
$(
|
|
|
|
.service($mod::get)
|
|
|
|
.service($mod::update)
|
|
|
|
.service($mod::delete)
|
|
|
|
)*;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
create_services!(
|
2021-03-15 20:53:50 +08:00
|
|
|
attributes_for_faceting,
|
2021-01-05 07:21:42 +08:00
|
|
|
displayed_attributes,
|
2021-04-06 21:41:03 +08:00
|
|
|
searchable_attributes,
|
2021-04-20 17:28:16 +08:00
|
|
|
distinct_attribute,
|
2021-04-06 21:41:03 +08:00
|
|
|
stop_words
|
2021-01-05 07:21:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
#[post("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
|
|
|
|
async fn update_all(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
index_uid: web::Path<String>,
|
|
|
|
body: web::Json<Settings>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-16 01:11:10 +08:00
|
|
|
match data
|
|
|
|
.update_settings(index_uid.into_inner(), body.into_inner(), true)
|
|
|
|
.await
|
|
|
|
{
|
2021-03-30 02:19:37 +08:00
|
|
|
Ok(update_result) => {
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_result.id() })))
|
|
|
|
}
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
|
|
|
|
async fn get_all(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-15 23:52:05 +08:00
|
|
|
match data.settings(index_uid.into_inner()).await {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(settings) => Ok(HttpResponse::Ok().json(settings)),
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}/settings", wrap = "Authentication::Private")]
|
|
|
|
async fn delete_all(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
index_uid: web::Path<String>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let settings = Settings::cleared();
|
2021-03-16 01:11:10 +08:00
|
|
|
match data
|
|
|
|
.update_settings(index_uid.into_inner(), settings, false)
|
|
|
|
.await
|
|
|
|
{
|
2021-03-30 02:19:37 +08:00
|
|
|
Ok(update_result) => {
|
|
|
|
Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_result.id() })))
|
|
|
|
}
|
2021-01-05 07:21:42 +08:00
|
|
|
Err(e) => {
|
2021-03-16 23:09:14 +08:00
|
|
|
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
|
2021-01-05 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|