add /settings/index-new-fields routes

This commit is contained in:
qdequele 2020-01-27 18:33:40 +01:00
parent 037724576e
commit 6a32432b01
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
2 changed files with 46 additions and 0 deletions

View File

@ -136,6 +136,9 @@ pub fn load_routes(app: &mut tide::Server<Data>) {
.post(|ctx| into_response(setting::update_displayed(ctx))) .post(|ctx| into_response(setting::update_displayed(ctx)))
.delete(|ctx| into_response(setting::delete_displayed(ctx))); .delete(|ctx| into_response(setting::delete_displayed(ctx)));
}); });
router.at("/index-new-fields")
.get(|ctx| into_response(setting::get_index_new_fields(ctx)))
.post(|ctx| into_response(setting::update_index_new_fields(ctx)));
router router
.at("/synonyms") .at("/synonyms")

View File

@ -544,3 +544,46 @@ pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body).unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct IndexNewFieldsSettings {
pub index_new_fields: Option<bool>,
}
pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?;
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
let schema = index.main.schema(&reader)?;
let index_new_fields = schema.map(|s| s.must_index_new_fields());
let settings = IndexNewFieldsSettings {
index_new_fields,
};
Ok(tide::Response::new(200).body_json(&settings).unwrap())
}
pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?;
let settings: IndexNewFieldsSettings =
ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db;
let settings = Settings {
index_new_fields: settings.index_new_fields,
..Settings::default()
};
let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
writer.commit()?;
let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
}