Skip to content

Commit

Permalink
mod: custom sql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bennjii committed Feb 21, 2024
1 parent b08b3f7 commit a57eb22
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/methods/customer/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use sea_orm::{
JsonValue, QuerySelect, RuntimeErr, Set, Statement,
};
use sea_orm::{DbErr, DeleteResult, QueryOrder};
use sea_orm::DbErr::Query;
use serde::{Deserialize, Serialize};
use serde_json::json;
use validator::Validate;
Expand Down Expand Up @@ -139,14 +140,20 @@ impl Customer {
) -> Result<Vec<CustomerWithTransactionsOut>, Error> {
let as_str: Vec<CustomerWithTransactions> = CustomerWithTransactions::find_by_statement(Statement::from_sql_and_values(
DbBackend::MySql,
&format!("SELECT Customer.*, GROUP_CONCAT(`Transactions`.`id`) as transactions
r#"
SELECT
Customer.*, GROUP_CONCAT(`Transactions`.`id`) as transactions
FROM Customer
LEFT JOIN Transactions ON (REPLACE(JSON_EXTRACT(Transactions.customer, '$.customer_id'), '\"', '')) = Customer.id
WHERE (LOWER(Customer.name) LIKE '%{}%' OR Customer.contact LIKE '%{}%') AND Customer.tenant_id = '{}'
LEFT JOIN Transactions
ON
(REPLACE(JSON_EXTRACT(Transactions.customer, '$.customer_id'), '"', '')) = Customer.id
WHERE
(LOWER(Customer.name) LIKE '?1' OR Customer.contact LIKE '?1')
AND Customer.tenant_id = '?2'
GROUP BY Customer.id
LIMIT 25",
query, query, session.tenant_id),
vec![]
LIMIT 25
"#,
vec![query.to_string().into(), session.tenant_id.into()]
))
.all(db)
.await?;
Expand Down

0 comments on commit a57eb22

Please sign in to comment.