chore: normalize code #2

This commit is contained in:
Maulana Sodiqin
2025-05-18 03:12:20 +07:00
parent a43ef8ec04
commit e48507901e
15 changed files with 603 additions and 365 deletions
+4
View File
@@ -3,3 +3,7 @@ use surrealdb::sql::Thing;
pub fn make_thing(table: &str, id: &str) -> Thing {
Thing::from((table, id))
}
pub fn make_thing_str(table: &str, id: &str) -> String {
format!("{}:⟨{}", table, id)
}
+106
View File
@@ -1,4 +1,7 @@
use imphnen_libs::MetaRequestDto;
use surrealdb::engine::remote::ws::Client;
use surrealdb::method::Query;
use surrealdb::sql::Thing;
pub struct ListQueryBuilder {
resource: String,
@@ -135,3 +138,106 @@ impl ListQueryBuilder {
)
}
}
pub struct DetailQueryBuilder {
resource: String,
id: Option<String>,
thing: Option<String>,
where_field: Option<String>,
where_value: Option<String>,
select_fields: Vec<String>,
fetch_fields: Vec<String>,
}
impl DetailQueryBuilder {
pub fn new(resource: impl Into<String>) -> Self {
Self {
resource: resource.into(),
id: None,
thing: None,
where_field: None,
where_value: None,
select_fields: vec![],
fetch_fields: vec![],
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
if self.where_field.is_some() || self.thing.is_some() {
panic!("Cannot use with_id() after with_where() or with_thing()");
}
self.id = Some(id.into());
self
}
pub fn with_thing(mut self, thing: &Thing) -> Self {
if self.id.is_some() || self.where_field.is_some() {
panic!("Cannot use with_thing() after with_id() or with_where()");
}
self.thing = Some(thing.to_string()); // app_users:uuid
self.resource = thing.tb.clone(); // update resource dari thing
self
}
pub fn with_where(mut self, field: impl Into<String>) -> Self {
if self.id.is_some() || self.thing.is_some() {
panic!("Cannot use with_where() after with_id() or with_thing()");
}
self.where_field = Some(field.into());
self
}
pub fn where_value(mut self, value: impl Into<String>) -> Self {
self.where_value = Some(value.into());
self
}
pub fn with_select_fields(mut self, fields: Vec<&str>) -> Self {
self.select_fields = fields.into_iter().map(String::from).collect();
self
}
pub fn with_fetch(mut self, field: impl Into<String>) -> Self {
self.fetch_fields.push(field.into());
self
}
pub fn build(&self) -> String {
let select_clause = if self.select_fields.is_empty() {
"*".to_string()
} else {
self.select_fields.join(", ")
};
let fetch_clause = if self.fetch_fields.is_empty() {
String::new()
} else {
format!("FETCH {}", self.fetch_fields.join(", "))
};
let from_clause = if let Some(thing) = &self.thing {
thing.to_string()
} else if let Some(id) = &self.id {
format!("{}:⟨{}", self.resource, id)
} else if let (Some(field), Some(_)) = (&self.where_field, &self.where_value) {
format!("{} WHERE {} = $value", self.resource, field)
} else {
panic!(
"You must set one of with_id(), with_thing(), or with_where()+where_value()"
);
};
format!(
"SELECT {} FROM {} {}",
select_clause, from_clause, fetch_clause
)
}
pub fn apply_bindings<'q>(&self, query: Query<'q, Client>) -> Query<'q, Client> {
if let (Some(_), Some(value)) = (&self.where_field, &self.where_value) {
query.bind(("value", value.clone()))
} else {
query
}
}
}