chore: cleaning code
This commit is contained in:
@@ -4,11 +4,5 @@ pub fn bind_filter_value(
|
||||
query: Query<'_, Client>,
|
||||
val: String,
|
||||
) -> Query<'_, Client> {
|
||||
if let Ok(b) = val.parse::<bool>() {
|
||||
query.bind(("filter", b))
|
||||
} else if let Ok(i) = val.parse::<i64>() {
|
||||
query.bind(("filter", i))
|
||||
} else {
|
||||
query.bind(("filter", val))
|
||||
}
|
||||
query.bind(("filter", val)) // langsung string aja, udah cukup
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
pub trait Crud<T, Args> {
|
||||
fn list(&self, _args: Args) -> T {
|
||||
unimplemented!("list() is not implemented for this type")
|
||||
}
|
||||
fn detail(&self, _args: Args) -> T {
|
||||
unimplemented!("detail() is not implemented for this type")
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ use imphnen_entities::*;
|
||||
use imphnen_libs::*;
|
||||
|
||||
pub mod bind_filter;
|
||||
pub mod crud;
|
||||
pub mod extract_email;
|
||||
pub mod generate_date;
|
||||
pub mod generate_otp;
|
||||
@@ -15,7 +14,6 @@ pub mod response_format;
|
||||
pub mod validator;
|
||||
|
||||
pub use bind_filter::*;
|
||||
pub use crud::*;
|
||||
pub use extract_email::*;
|
||||
pub use generate_date::*;
|
||||
pub use generate_otp::*;
|
||||
|
||||
@@ -20,11 +20,13 @@ impl ListQueryBuilder {
|
||||
meta: &MetaRequestDto,
|
||||
search_field: impl Into<String>,
|
||||
select_fields: Option<Vec<&str>>,
|
||||
fetch_fields: Option<Vec<&str>>,
|
||||
) -> Self {
|
||||
let mut builder = Self::new(resource)
|
||||
.with_search(meta.search.as_deref(), &search_field.into())
|
||||
.with_filter(meta.filter_by.as_deref(), meta.filter.as_deref())
|
||||
.with_sorting(meta.sort_by.as_deref(), meta.order.as_deref())
|
||||
.with_fetch(fetch_fields)
|
||||
.with_pagination(meta.page, meta.per_page);
|
||||
|
||||
if let Some(fields) = select_fields {
|
||||
@@ -54,9 +56,10 @@ impl ListQueryBuilder {
|
||||
pub fn with_search(mut self, search: Option<&str>, field: &str) -> Self {
|
||||
if let Some(search) = search {
|
||||
if !search.is_empty() {
|
||||
self
|
||||
.conditions
|
||||
.push(format!("string::contains({} ?? '', $search)", field));
|
||||
self.conditions.push(format!(
|
||||
"string::contains(string::lowercase({} ?? ''), string::lowercase($search))",
|
||||
field
|
||||
));
|
||||
}
|
||||
}
|
||||
self
|
||||
@@ -65,7 +68,10 @@ impl ListQueryBuilder {
|
||||
pub fn with_filter(mut self, field: Option<&str>, value: Option<&str>) -> Self {
|
||||
if let (Some(f), Some(v)) = (field, value) {
|
||||
if !v.is_empty() {
|
||||
self.conditions.push(format!("{} = $filter", f));
|
||||
self.conditions.push(format!(
|
||||
"string::contains(string::join('', [{}]), $filter)",
|
||||
f
|
||||
));
|
||||
}
|
||||
}
|
||||
self
|
||||
@@ -89,8 +95,10 @@ impl ListQueryBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_fetch(mut self, fetch: impl Into<String>) -> Self {
|
||||
self.fetch.push(fetch.into());
|
||||
pub fn with_fetch(mut self, fetches: Option<Vec<&str>>) -> Self {
|
||||
if let Some(items) = fetches {
|
||||
self.fetch.extend(items.into_iter().map(String::from));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ pub async fn query_list_with_meta<T>(
|
||||
custom_select: Option<String>,
|
||||
search_field: &str,
|
||||
select_fields: Option<Vec<&str>>,
|
||||
fetch_fields: Option<Vec<&str>>,
|
||||
) -> Result<ResponseListSuccessDto<Vec<T>>>
|
||||
where
|
||||
T: DeserializeOwned + Serialize,
|
||||
@@ -21,13 +22,20 @@ where
|
||||
let start = (page - 1) * per_page;
|
||||
|
||||
let sql = custom_select.unwrap_or_else(|| {
|
||||
ListQueryBuilder::from_meta(table, meta, search_field, select_fields).build()
|
||||
ListQueryBuilder::from_meta(
|
||||
table,
|
||||
meta,
|
||||
search_field,
|
||||
select_fields,
|
||||
fetch_fields,
|
||||
)
|
||||
.build()
|
||||
});
|
||||
|
||||
let mut query_exec = db.query(sql);
|
||||
if let Some(search) = &meta.search {
|
||||
if !search.is_empty() {
|
||||
query_exec = query_exec.bind(("search", search.clone()));
|
||||
query_exec = query_exec.bind(("search", search.to_lowercase().clone()));
|
||||
}
|
||||
}
|
||||
if let Some(filter_val) = &meta.filter {
|
||||
|
||||
Reference in New Issue
Block a user