refactor: Replace surrealdb_helpers with integrated query builder methods for improved clarity and functionality
This commit is contained in:
@@ -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<TeamMembersQueryDto> = 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);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<surrealdb::engine::any::Any>,
|
||||
table: &str,
|
||||
resource: String,
|
||||
conditions: &str,
|
||||
) -> Result<u64> {
|
||||
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<serde_json::Value> = 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<surrealdb::Value> = result.take(0)?;
|
||||
let count = response.get(0).and_then(|v| v.to_string().parse::<u64>().ok())
|
||||
.ok_or_else(|| anyhow::anyhow!("No count found in response"))?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
@@ -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::<Vec<_>>()
|
||||
.join(" AND ")
|
||||
}
|
||||
|
||||
pub async fn execute_safe_update_query(
|
||||
db: &surrealdb::Surreal<surrealdb::engine::any::Any>,
|
||||
query: String,
|
||||
) -> Result<()> {
|
||||
let mut result = db.query(query).await?;
|
||||
let _: Result<Vec<serde_json::Value>, _> = result.take(0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn execute_safe_count_query(
|
||||
db: &surrealdb::Surreal<surrealdb::engine::any::Any>,
|
||||
table: &str,
|
||||
conditions: &str,
|
||||
) -> Result<u64> {
|
||||
let query = format!("SELECT COUNT() AS member_count FROM {} WHERE {}", table, conditions);
|
||||
let mut result = db.query(query).await?;
|
||||
|
||||
let count_result: Vec<serde_json::Value> = 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')"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user