update settings routes

This commit is contained in:
qdequele 2020-01-28 16:27:21 +01:00
parent bbf9f41a04
commit 5c49f08bb2
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
3 changed files with 53 additions and 97 deletions

View File

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

@ -186,43 +186,29 @@ pub async fn delete_ranking(ctx: Request<Data>) -> SResult<Response> {
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 GetRankingRulesSettings {
pub ranking_rules: Option<Vec<String>>,
}
pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> { pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?; ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?; let index = ctx.index()?;
let db = &ctx.state().db; let db = &ctx.state().db;
let reader = db.main_read_txn()?; let reader = db.main_read_txn()?;
let ranking_rules = match index.main.ranking_rules(&reader)? { let ranking_rules: Option<Vec<String>> = match index.main.ranking_rules(&reader)? {
Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()),
None => None, None => None,
}; };
let settings = GetRankingRulesSettings { ranking_rules }; Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap())
Ok(tide::Response::new(200).body_json(&settings).unwrap())
}
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetRankingRulesSettings {
pub ranking_rules: Option<Vec<String>>,
} }
pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: SetRankingRulesSettings = let ranking_rules: Option<Vec<String>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
ranking_rules: settings.ranking_rules, ranking_rules,
..Settings::default() ..Settings::default()
}; };
@ -266,9 +252,10 @@ pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
let reader = db.main_read_txn()?; let reader = db.main_read_txn()?;
let ranking_distinct = index.main.ranking_distinct(&reader)?; let ranking_distinct = index.main.ranking_distinct(&reader)?;
let settings = GetRankingDistinctSettings { ranking_distinct };
Ok(tide::Response::new(200).body_json(&settings).unwrap()) Ok(tide::Response::new(200)
.body_json(&ranking_distinct)
.unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
@ -280,12 +267,12 @@ pub struct SetRankingDistinctSettings {
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: SetRankingDistinctSettings = let ranking_distinct: Option<String> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
ranking_distinct: settings.ranking_distinct, ranking_distinct,
..Settings::default() ..Settings::default()
}; };
@ -394,12 +381,6 @@ pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> {
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 AttributesIdentifierSettings {
pub attribute_identifier: Option<String>,
}
pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> { pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?; ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?; let index = ctx.index()?;
@ -410,17 +391,9 @@ pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> {
let attribute_identifier = schema.map(|s| s.identifier()); let attribute_identifier = schema.map(|s| s.identifier());
let settings = AttributesIdentifierSettings { Ok(tide::Response::new(200)
attribute_identifier, .body_json(&attribute_identifier)
}; .unwrap())
Ok(tide::Response::new(200).body_json(&settings).unwrap())
}
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetAttributesSearchableSettings {
pub attributes_searchable: Option<Vec<String>>,
} }
pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> { pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
@ -433,11 +406,9 @@ pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
let attributes_searchable = schema.map(|s| s.get_indexed_name()); let attributes_searchable = schema.map(|s| s.get_indexed_name());
let settings = GetAttributesSearchableSettings { Ok(tide::Response::new(200)
attributes_searchable, .body_json(&attributes_searchable)
}; .unwrap())
Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
@ -449,12 +420,12 @@ pub struct SetAttributesSearchableSettings {
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: SetAttributesSearchableSettings = let attributes_searchable: Option<Vec<String>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_searchable: settings.attributes_searchable, attributes_searchable,
..Settings::default() ..Settings::default()
}; };
@ -484,12 +455,6 @@ pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
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 AttributesDisplayedSettings {
pub attributes_displayed: Option<HashSet<String>>,
}
pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> { pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?; ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?; let index = ctx.index()?;
@ -500,22 +465,20 @@ pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> {
let attributes_displayed = schema.map(|s| s.get_displayed_name()); let attributes_displayed = schema.map(|s| s.get_displayed_name());
let settings = AttributesDisplayedSettings { Ok(tide::Response::new(200)
attributes_displayed, .body_json(&attributes_displayed)
}; .unwrap())
Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: AttributesDisplayedSettings = let attributes_displayed: Option<HashSet<String>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_displayed: settings.attributes_displayed, attributes_displayed,
..Settings::default() ..Settings::default()
}; };
@ -545,12 +508,6 @@ pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
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> { pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?; ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?; let index = ctx.index()?;
@ -561,22 +518,20 @@ pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> {
let index_new_fields = schema.map(|s| s.must_index_new_fields()); let index_new_fields = schema.map(|s| s.must_index_new_fields());
let settings = IndexNewFieldsSettings { Ok(tide::Response::new(200)
index_new_fields, .body_json(&index_new_fields)
}; .unwrap())
Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: IndexNewFieldsSettings = let index_new_fields: Option<bool> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
index_new_fields: settings.index_new_fields, index_new_fields,
..Settings::default() ..Settings::default()
}; };

View File

@ -1273,28 +1273,28 @@ fn search_with_settings_attributes_searchable_2() {
let query = "q=avangers&limit=3"; let query = "q=avangers&limit=3";
let response = json!([ let response = json!([
{ {
"id": 299536, "id": 299536,
"title": "Avengers: Infinity War", "title": "Avengers: Infinity War",
"tagline": "An entire universe. Once and for all.", "tagline": "An entire universe. Once and for all.",
"overview": "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle, a new danger has emerged from the cosmic shadows: Thanos. A despot of intergalactic infamy, his goal is to collect all six Infinity Stones, artifacts of unimaginable power, and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.", "overview": "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle, a new danger has emerged from the cosmic shadows: Thanos. A despot of intergalactic infamy, his goal is to collect all six Infinity Stones, artifacts of unimaginable power, and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.",
"poster_path": "https://image.tmdb.org/t/p/w500/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg" "poster_path": "https://image.tmdb.org/t/p/w500/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg"
}, },
{ {
"id": 299534, "id": 299534,
"title": "Avengers: Endgame", "title": "Avengers: Endgame",
"tagline": "Part of the journey is the end.", "tagline": "Part of the journey is the end.",
"overview": "After the devastating events of Avengers: Infinity War, the universe is in ruins due to the efforts of the Mad Titan, Thanos. With the help of remaining allies, the Avengers must assemble once more in order to undo Thanos' actions and restore order to the universe once and for all, no matter what consequences may be in store.", "overview": "After the devastating events of Avengers: Infinity War, the universe is in ruins due to the efforts of the Mad Titan, Thanos. With the help of remaining allies, the Avengers must assemble once more in order to undo Thanos' actions and restore order to the universe once and for all, no matter what consequences may be in store.",
"poster_path": "https://image.tmdb.org/t/p/w500/or06FN3Dka5tukK1e9sl16pB3iy.jpg" "poster_path": "https://image.tmdb.org/t/p/w500/or06FN3Dka5tukK1e9sl16pB3iy.jpg"
}, },
{ {
"id": 100402, "id": 100402,
"title": "Captain America: The Winter Soldier", "title": "Captain America: The Winter Soldier",
"tagline": "In heroes we trust.", "tagline": "In heroes we trust.",
"overview": "After the cataclysmic events in New York with The Avengers, Steve Rogers, aka Captain America is living quietly in Washington, D.C. and trying to adjust to the modern world. But when a S.H.I.E.L.D. colleague comes under attack, Steve becomes embroiled in a web of intrigue that threatens to put the world at risk. Joining forces with the Black Widow, Captain America struggles to expose the ever-widening conspiracy while fighting off professional assassins sent to silence him at every turn. When the full scope of the villainous plot is revealed, Captain America and the Black Widow enlist the help of a new ally, the Falcon. However, they soon find themselves up against an unexpected and formidable enemy—the Winter Soldier.", "overview": "After the cataclysmic events in New York with The Avengers, Steve Rogers, aka Captain America is living quietly in Washington, D.C. and trying to adjust to the modern world. But when a S.H.I.E.L.D. colleague comes under attack, Steve becomes embroiled in a web of intrigue that threatens to put the world at risk. Joining forces with the Black Widow, Captain America struggles to expose the ever-widening conspiracy while fighting off professional assassins sent to silence him at every turn. When the full scope of the villainous plot is revealed, Captain America and the Black Widow enlist the help of a new ally, the Falcon. However, they soon find themselves up against an unexpected and formidable enemy—the Winter Soldier.",
"poster_path": "https://image.tmdb.org/t/p/w500/5TQ6YDmymBpnF005OyoB7ohZps9.jpg" "poster_path": "https://image.tmdb.org/t/p/w500/5TQ6YDmymBpnF005OyoB7ohZps9.jpg"
} }
]); ]);
common::search(&mut server, query, response); common::search(&mut server, query, response);
} }