From b37a81836134ddd30c421803372b658914df0a7c Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:31:56 -0400 Subject: [PATCH 1/2] feat: exclude query args --- client/src/pages/Playground.tsx | 3 +++ docs/pages/api-reference/params.mdx | 10 ++++++++++ server/model/src/api/args.rs | 13 +++++++++++++ server/model/src/db/geofence.rs | 26 +++++++++++++++++++++++++- server/model/src/utils/mod.rs | 8 ++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) diff --git a/client/src/pages/Playground.tsx b/client/src/pages/Playground.tsx index 2b714507..79f0df1f 100644 --- a/client/src/pages/Playground.tsx +++ b/client/src/pages/Playground.tsx @@ -107,6 +107,9 @@ const PARAMS = { underscore: 'string', dash: 'string', space: 'string', + exclude: 'string', + excludeparents: 'string', + excludeproperties: 'string', unpolish: 'boolean', ignoremanualparent: 'boolean', } diff --git a/docs/pages/api-reference/params.mdx b/docs/pages/api-reference/params.mdx index 16832fa5..c4498301 100644 --- a/docs/pages/api-reference/params.mdx +++ b/docs/pages/api-reference/params.mdx @@ -69,6 +69,16 @@ pub struct ApiQueryArgs { /// If true, all non-alphanumeric characters are removed from the `name` property /// (excludes spaces, dashes, and underscores) pub alphanumeric: Option, + /// Exclude areas with the specified properties, no matter the value or type. + /// + /// Property keys separated by a comma + pub excludeproperties: Option, + /// Excludes areas with the specified parent names + /// + /// A list of parents names separated by a comma that are to be excluded by the API query + pub excludeparents: Option, + /// A list of names separated by a comma that are to be excluded by the API query + pub exclude: Option, } ``` diff --git a/server/model/src/api/args.rs b/server/model/src/api/args.rs index e21a08fa..614a10ff 100644 --- a/server/model/src/api/args.rs +++ b/server/model/src/api/args.rs @@ -98,6 +98,16 @@ pub struct ApiQueryArgs { /// If true, all non-alphanumeric characters are removed from the `name` property /// (excludes spaces, dashes, and underscores) pub alphanumeric: Option, + /// Exclude areas with the specified properties, no matter the value or type. + /// + /// Property keys separated by a comma + pub excludeproperties: Option, + /// Excludes areas with the specified parent names + /// + /// A list of parents names separated by a comma that are to be excluded by the API query + pub excludeparents: Option, + /// A list of names separated by a comma that are to be excluded by the API query + pub exclude: Option, } impl Default for ApiQueryArgs { @@ -128,6 +138,9 @@ impl Default for ApiQueryArgs { unpolish: None, ignoremanualparent: None, alphanumeric: None, + excludeproperties: None, + excludeparents: None, + exclude: None, } } } diff --git a/server/model/src/db/geofence.rs b/server/model/src/db/geofence.rs index 2c2661bc..9242fce7 100644 --- a/server/model/src/db/geofence.rs +++ b/server/model/src/db/geofence.rs @@ -11,7 +11,7 @@ use crate::{ error::ModelError, utils::{ json::{determine_category_by_value, JsonToModel}, - json_related_sort, name_modifier, parse_order, + json_related_sort, name_modifier, parse_order, separate_by_comma, }, }; @@ -126,6 +126,7 @@ impl Model { args: &ApiQueryArgs, ) -> Result { let mut has_manual_parent = String::from(""); + let mut properties = if let Some(properties) = property_map.get(&self.id) { properties .into_iter() @@ -143,6 +144,20 @@ impl Model { vec![] }; + log::info!("{:?} {}", args.exclude, self.name); + if separate_by_comma(&args.exclude).contains(&self.name) { + return Err(ModelError::Geofence("Excluded name".to_string())); + } + if properties + .iter() + .find(|prop| { + separate_by_comma(&args.excludeproperties).contains(&prop.name.to_string()) + }) + .is_some() + { + return Err(ModelError::Geofence("Excluded property".to_string())); + } + let parent_name = if has_manual_parent.is_empty() { if let Some(parent_id) = self.parent { if let Some(name) = name_map.get(&parent_id) { @@ -156,6 +171,15 @@ impl Model { } else { Some(has_manual_parent) }; + + if parent_name.is_some() + && separate_by_comma(&args.excludeparents) + .iter() + .any(|parent| parent_name.as_ref().unwrap().eq(parent)) + { + return Err(ModelError::Geofence("Excluded parent".to_string())); + } + if args.internal.unwrap_or(false) { properties.push(Basic { name: "__id", diff --git a/server/model/src/utils/mod.rs b/server/model/src/utils/mod.rs index e0bda9da..014e3dcb 100644 --- a/server/model/src/utils/mod.rs +++ b/server/model/src/utils/mod.rs @@ -227,6 +227,14 @@ pub fn json_related_sort(json: &mut Vec, sort_by: &String, or }); } +pub fn separate_by_comma(param: &Option) -> Vec { + if let Some(param) = param { + param.split(",").map(|x| x.to_string()).collect() + } else { + vec![] + } +} + fn convert_polish_to_ascii(param: String) -> String { let polish_characters: [(char, char); 18] = [ ('ą', 'a'), From 3f5ec6ca22b6b186f4f981af55c3c8e0c922c787 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:49:37 -0400 Subject: [PATCH 2/2] fix: remove test log --- server/model/src/db/geofence.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/server/model/src/db/geofence.rs b/server/model/src/db/geofence.rs index 9242fce7..e4dece6c 100644 --- a/server/model/src/db/geofence.rs +++ b/server/model/src/db/geofence.rs @@ -144,7 +144,6 @@ impl Model { vec![] }; - log::info!("{:?} {}", args.exclude, self.name); if separate_by_comma(&args.exclude).contains(&self.name) { return Err(ModelError::Geofence("Excluded name".to_string())); }