Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding $regex filter #243

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading