Skip to content

Commit

Permalink
feat: adding $regex filter (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez authored Feb 11, 2025
1 parent 04585db commit 1ad244f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 17 additions & 6 deletions api/src/helper/shape_mongo_filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::extract::Query;
use entities::{
event_access::EventAccess, CONTAINS_FILTER, DELETED_FILTER, DUAL_ENVIRONMENT_HEADER,
ENVIRONMENT_FILTER, LIMIT_FILTER, OWNERSHIP_FILTER, SKIP_FILTER,
ENVIRONMENT_FILTER, LIMIT_FILTER, OPTIONS_FILTER, OWNERSHIP_FILTER, REGEX_FILTER, SKIP_FILTER,
};
use http::HeaderMap;
use mongodb::bson::{doc, Document};
Expand All @@ -19,11 +19,7 @@ pub fn shape_mongo_filter(
event_access: Option<Arc<EventAccess>>,
headers: Option<HeaderMap>,
) -> MongoQuery {
let mut filter = doc! {
// "tags" : {
// "$in" : ["featured"]
// }
};
let mut filter = doc! {};
let mut skip = 0;
let mut limit = 20;

Expand All @@ -43,6 +39,19 @@ pub fn shape_mongo_filter(
}
} else if key == SKIP_FILTER {
skip = value.parse().unwrap_or(0);
} else if key == REGEX_FILTER {
let values = string_to_vec(value);

let splitted = values.split_first();
let target = splitted.map(|a| a.0);
let rest = splitted.and_then(|a| a.1.first());

if let (Some(target), Some(element)) = (target, rest) {
filter.insert(
target,
doc! { REGEX_FILTER: element.to_string(), OPTIONS_FILTER: "i".to_string() },
);
}
} else {
match value.as_str() {
"true" => filter.insert(key, true),
Expand Down Expand Up @@ -74,6 +83,8 @@ pub fn shape_mongo_filter(
}
}

println!("{:#?}", filter);

MongoQuery {
filter,
limit,
Expand Down
2 changes: 2 additions & 0 deletions entities/src/domain/constant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub const ENVIRONMENT_FILTER: &str = "environment";
pub const DUAL_ENVIRONMENT_HEADER: &str = "x-pica-show-all-environments";
pub const LIMIT_FILTER: &str = "limit";
pub const CONTAINS_FILTER: &str = "$in";
pub const REGEX_FILTER: &str = "$regex";
pub const OPTIONS_FILTER: &str = "$options";
pub const SKIP_FILTER: &str = "skip";
pub const QUERY_BY_ID_PASSTHROUGH: &str = "x-pica-action-id";

Expand Down

0 comments on commit 1ad244f

Please sign in to comment.