diff --git a/imphnen-iam/src/v1/teams/teams_repository.rs b/imphnen-iam/src/v1/teams/teams_repository.rs index 5e69426..5b9a565 100644 --- a/imphnen-iam/src/v1/teams/teams_repository.rs +++ b/imphnen-iam/src/v1/teams/teams_repository.rs @@ -188,8 +188,12 @@ impl<'a> TeamsRepository<'a> { let now = Instant::now(); let db = &self.state.surrealdb_ws; - let condition = format!("{} AND is_active = true", build_thing_condition("team_id", team_id)); - let sql = format!("SELECT * FROM {} WHERE {}", ResourceEnum::TeamMembers, condition); + let builder = DetailQueryBuilder::new(ResourceEnum::TeamMembers.to_string()) + .with_thing_equals("team_id", team_id) + .with_condition("is_active = true") + .with_select_fields(vec!["*"]); + + let sql = builder.build(); let mut result = db.query(sql).await?; let members: Vec = match result.take(0) { @@ -248,7 +252,7 @@ impl<'a> TeamsRepository<'a> { let member_count = execute_safe_count_query( db, - &ResourceEnum::TeamMembers.to_string(), + ResourceEnum::TeamMembers.to_string(), &conditions, ).await.unwrap_or(0); diff --git a/imphnen-utils/src/lib.rs b/imphnen-utils/src/lib.rs index c386e8c..723c0a2 100644 --- a/imphnen-utils/src/lib.rs +++ b/imphnen-utils/src/lib.rs @@ -11,7 +11,6 @@ pub mod response_format; pub mod serde_helpers; pub mod validator; pub mod csrf_token; -pub mod surrealdb_helpers; pub use logger::init_logger; pub use bind_filter::*; @@ -22,8 +21,15 @@ pub use get_id::*; pub use imphnen_entities::*; pub use imphnen_libs::*; pub use make_thing::*; -pub use query_builder::*; -pub use query_list::*; +pub use query_builder::{ + build_thing_condition, + build_multi_thing_condition, + execute_safe_update_query, + execute_safe_count_query, + ListQueryBuilder, + DetailQueryBuilder, +}; +pub use query_list::QueryListBuilder; pub use response_format::*; pub use serde_helpers::{ option_thing_or_string, serialize_option_thing, serialize_thing, diff --git a/imphnen-utils/src/query_builder.rs b/imphnen-utils/src/query_builder.rs index 1949a14..ca07026 100644 --- a/imphnen-utils/src/query_builder.rs +++ b/imphnen-utils/src/query_builder.rs @@ -230,16 +230,16 @@ impl DetailQueryBuilder { self } - pub fn with_thing_condition(mut self, field: &str, thing: &Thing) -> Self { - let condition = build_thing_condition(field, thing); - self.conditions.push(condition); - self + pub fn with_thing_equals(mut self, field: &str, thing: &Thing) -> Self { + let condition = build_thing_condition(field, thing); + self.conditions.push(condition); + self } - pub fn with_multi_thing_condition(mut self, conditions: &[(&str, &Thing)]) -> Self { - let condition = build_multi_thing_condition(conditions); - self.conditions.push(condition); - self + pub fn with_things_equals(mut self, conditions: &[(&str, &Thing)]) -> Self { + let condition = build_multi_thing_condition(conditions); + self.conditions.push(condition); + self } pub fn with_select_fields(mut self, fields: Vec<&str>) -> Self { @@ -318,23 +318,16 @@ pub async fn execute_safe_update_query( pub async fn execute_safe_count_query( db: &Surreal, - table: &str, + resource: String, conditions: &str, ) -> Result { - let query = format!("SELECT COUNT() AS member_count FROM {} WHERE {}", table, conditions); + let query = format!("SELECT count() FROM {} WHERE {}", resource, conditions); let mut result = db.query(query).await?; - let count_result: Vec = result.take(0).unwrap_or_default(); - - let count = if let Some(first_result) = count_result.first() { - if let Some(count_val) = first_result.get("member_count") { - count_val.as_u64().unwrap_or(0) - } else { - 0 - } - } else { - 0 - }; + // Extract the count from the result + let response: Vec = result.take(0)?; + let count = response.get(0).and_then(|v| v.to_string().parse::().ok()) + .ok_or_else(|| anyhow::anyhow!("No count found in response"))?; Ok(count) } diff --git a/imphnen-utils/src/surrealdb_helpers.rs b/imphnen-utils/src/surrealdb_helpers.rs deleted file mode 100644 index f0d40e4..0000000 --- a/imphnen-utils/src/surrealdb_helpers.rs +++ /dev/null @@ -1,76 +0,0 @@ -use surrealdb::sql::Thing; -use anyhow::Result; - -pub fn build_thing_condition(field: &str, thing: &Thing) -> String { - format!("{} = type::thing('{}', '{}')", field, thing.tb, thing.id.to_raw()) -} - -pub fn build_multi_thing_condition(conditions: &[(&str, &Thing)]) -> String { - conditions - .iter() - .map(|(field, thing)| build_thing_condition(field, thing)) - .collect::>() - .join(" AND ") -} - -pub async fn execute_safe_update_query( - db: &surrealdb::Surreal, - query: String, -) -> Result<()> { - let mut result = db.query(query).await?; - let _: Result, _> = result.take(0); - Ok(()) -} - -pub async fn execute_safe_count_query( - db: &surrealdb::Surreal, - table: &str, - conditions: &str, -) -> Result { - let query = format!("SELECT COUNT() AS member_count FROM {} WHERE {}", table, conditions); - let mut result = db.query(query).await?; - - let count_result: Vec = result.take(0).unwrap_or_default(); - - let count = if let Some(first_result) = count_result.first() { - if let Some(count_val) = first_result.get("member_count") { - count_val.as_u64().unwrap_or(0) - } else { - 0 - } - } else { - 0 - }; - - Ok(count) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::make_thing_from_enum; - use imphnen_libs::ResourceEnum; - - #[test] - fn test_build_thing_condition() { - let team_thing = make_thing_from_enum(ResourceEnum::Teams, "test-id"); - let condition = build_thing_condition("team_id", &team_thing); - assert_eq!(condition, "team_id = type::thing('app_teams', 'test-id')"); - } - - #[test] - fn test_build_multi_thing_condition() { - let team_thing = make_thing_from_enum(ResourceEnum::Teams, "team-id"); - let user_thing = make_thing_from_enum(ResourceEnum::Users, "user-id"); - - let conditions = build_multi_thing_condition(&[ - ("team_id", &team_thing), - ("user_id", &user_thing), - ]); - - assert_eq!( - conditions, - "team_id = type::thing('app_teams', 'team-id') AND user_id = type::thing('app_users', 'user-id')" - ); - } -} \ No newline at end of file