Skip to content

Commit

Permalink
Merge pull request #224 from TurtIeSocks/exclude-api-args
Browse files Browse the repository at this point in the history
feat: exclude query args
  • Loading branch information
TurtIeSocks authored Jun 21, 2024
2 parents 9fcfaa0 + 3f5ec6c commit 194d18e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/src/pages/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ const PARAMS = {
underscore: 'string',
dash: 'string',
space: 'string',
exclude: 'string',
excludeparents: 'string',
excludeproperties: 'string',
unpolish: 'boolean',
ignoremanualparent: 'boolean',
}
Expand Down
10 changes: 10 additions & 0 deletions docs/pages/api-reference/params.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
/// Exclude areas with the specified properties, no matter the value or type.
///
/// Property keys separated by a comma
pub excludeproperties: Option<String>,
/// 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<String>,
/// A list of names separated by a comma that are to be excluded by the API query
pub exclude: Option<String>,
}
```

Expand Down
13 changes: 13 additions & 0 deletions server/model/src/api/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
/// Exclude areas with the specified properties, no matter the value or type.
///
/// Property keys separated by a comma
pub excludeproperties: Option<String>,
/// 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<String>,
/// A list of names separated by a comma that are to be excluded by the API query
pub exclude: Option<String>,
}

impl Default for ApiQueryArgs {
Expand Down Expand Up @@ -128,6 +138,9 @@ impl Default for ApiQueryArgs {
unpolish: None,
ignoremanualparent: None,
alphanumeric: None,
excludeproperties: None,
excludeparents: None,
exclude: None,
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion server/model/src/db/geofence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -126,6 +126,7 @@ impl Model {
args: &ApiQueryArgs,
) -> Result<Feature, ModelError> {
let mut has_manual_parent = String::from("");

let mut properties = if let Some(properties) = property_map.get(&self.id) {
properties
.into_iter()
Expand All @@ -143,6 +144,19 @@ impl Model {
vec![]
};

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) {
Expand All @@ -156,6 +170,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",
Expand Down
8 changes: 8 additions & 0 deletions server/model/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ pub fn json_related_sort(json: &mut Vec<serde_json::Value>, sort_by: &String, or
});
}

pub fn separate_by_comma(param: &Option<String>) -> Vec<String> {
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'),
Expand Down

0 comments on commit 194d18e

Please sign in to comment.