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 now = Instant::now();
|
||||||
let db = &self.state.surrealdb_ws;
|
let db = &self.state.surrealdb_ws;
|
||||||
|
|
||||||
let condition = format!("{} AND is_active = true", build_thing_condition("team_id", team_id));
|
let builder = DetailQueryBuilder::new(ResourceEnum::TeamMembers.to_string())
|
||||||
let sql = format!("SELECT * FROM {} WHERE {}", ResourceEnum::TeamMembers, condition);
|
.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 mut result = db.query(sql).await?;
|
||||||
|
|
||||||
let members: Vec<TeamMembersQueryDto> = match result.take(0) {
|
let members: Vec<TeamMembersQueryDto> = match result.take(0) {
|
||||||
@@ -248,7 +252,7 @@ impl<'a> TeamsRepository<'a> {
|
|||||||
|
|
||||||
let member_count = execute_safe_count_query(
|
let member_count = execute_safe_count_query(
|
||||||
db,
|
db,
|
||||||
&ResourceEnum::TeamMembers.to_string(),
|
ResourceEnum::TeamMembers.to_string(),
|
||||||
&conditions,
|
&conditions,
|
||||||
).await.unwrap_or(0);
|
).await.unwrap_or(0);
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ pub mod response_format;
|
|||||||
pub mod serde_helpers;
|
pub mod serde_helpers;
|
||||||
pub mod validator;
|
pub mod validator;
|
||||||
pub mod csrf_token;
|
pub mod csrf_token;
|
||||||
pub mod surrealdb_helpers;
|
|
||||||
|
|
||||||
pub use logger::init_logger;
|
pub use logger::init_logger;
|
||||||
pub use bind_filter::*;
|
pub use bind_filter::*;
|
||||||
@@ -22,8 +21,15 @@ pub use get_id::*;
|
|||||||
pub use imphnen_entities::*;
|
pub use imphnen_entities::*;
|
||||||
pub use imphnen_libs::*;
|
pub use imphnen_libs::*;
|
||||||
pub use make_thing::*;
|
pub use make_thing::*;
|
||||||
pub use query_builder::*;
|
pub use query_builder::{
|
||||||
pub use query_list::*;
|
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 response_format::*;
|
||||||
pub use serde_helpers::{
|
pub use serde_helpers::{
|
||||||
option_thing_or_string, serialize_option_thing, serialize_thing,
|
option_thing_or_string, serialize_option_thing, serialize_thing,
|
||||||
|
|||||||
@@ -230,16 +230,16 @@ impl DetailQueryBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_thing_condition(mut self, field: &str, thing: &Thing) -> Self {
|
pub fn with_thing_equals(mut self, field: &str, thing: &Thing) -> Self {
|
||||||
let condition = build_thing_condition(field, thing);
|
let condition = build_thing_condition(field, thing);
|
||||||
self.conditions.push(condition);
|
self.conditions.push(condition);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_multi_thing_condition(mut self, conditions: &[(&str, &Thing)]) -> Self {
|
pub fn with_things_equals(mut self, conditions: &[(&str, &Thing)]) -> Self {
|
||||||
let condition = build_multi_thing_condition(conditions);
|
let condition = build_multi_thing_condition(conditions);
|
||||||
self.conditions.push(condition);
|
self.conditions.push(condition);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_select_fields(mut self, fields: Vec<&str>) -> 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(
|
pub async fn execute_safe_count_query(
|
||||||
db: &Surreal<surrealdb::engine::any::Any>,
|
db: &Surreal<surrealdb::engine::any::Any>,
|
||||||
table: &str,
|
resource: String,
|
||||||
conditions: &str,
|
conditions: &str,
|
||||||
) -> Result<u64> {
|
) -> 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 mut result = db.query(query).await?;
|
||||||
|
|
||||||
let count_result: Vec<serde_json::Value> = result.take(0).unwrap_or_default();
|
// Extract the count from the result
|
||||||
|
let response: Vec<surrealdb::Value> = result.take(0)?;
|
||||||
let count = if let Some(first_result) = count_result.first() {
|
let count = response.get(0).and_then(|v| v.to_string().parse::<u64>().ok())
|
||||||
if let Some(count_val) = first_result.get("member_count") {
|
.ok_or_else(|| anyhow::anyhow!("No count found in response"))?;
|
||||||
count_val.as_u64().unwrap_or(0)
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(count)
|
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