bug fixed

This commit is contained in:
병준 박 2022-09-01 16:21:33 +00:00
parent a24d3dddb0
commit 41a53aeafb
3 changed files with 416 additions and 205 deletions

View File

@ -3,10 +3,48 @@
use super::models; use super::models;
use crate::repositories; use crate::repositories;
use beteran_common_rust as bcr;
use diesel::{result::Error, sql_query, RunQueryDsl}; use diesel::{result::Error, sql_query, RunQueryDsl};
use std::fmt::Write;
static MEMBER_COUNT_QUERY: &str = "
SELECT
COUNT(m.id)
FROM members as m
INNER JOIN sites m_s
ON m_s.id = m.site_id
INNER JOIN member_classes m_mc
ON m_mc.id = m.member_class_id
INNER JOIN member_levels m_ml
ON m_ml.id = m.member_level_id
LEFT OUTER JOIN member_settlement_settings m_mss
ON m_mss.member_id = m.id
LEFT OUTER JOIN member_game_settings m_mgs
ON m_mgs.member_id = m.id
LEFT OUTER JOIN member_bank_accounts m_mba
ON m_mba.member_id = m.id
LEFT OUTER JOIN banks m_mba_b
ON m_mba_b.id = m_mba.bank_id
LEFT OUTER JOIN members pm
ON pm.id = m.parent_member_id
LEFT OUTER JOIN sites pm_s
ON pm_s.id = pm.site_id
LEFT OUTER JOIN member_classes pm_mc
ON pm_mc.id = pm.member_class_id
LEFT OUTER JOIN member_levels pm_ml
ON pm_ml.id = pm.member_level_id
LEFT OUTER JOIN member_settlement_settings pm_mss
ON pm_mss.member_id = pm.id
LEFT OUTER JOIN member_game_settings pm_mgs
ON pm_mgs.member_id = pm.id
LEFT OUTER JOIN member_bank_accounts pm_mba
ON pm_mba.member_id = pm.id
LEFT OUTER JOIN banks pm_mba_b
ON pm_mba_b.id = pm_mba.bank_id
";
static MEMBER_QUERY: &str = " static MEMBER_QUERY: &str = "
SELECT SELECT
m.id as m_id, m.id as m_id,
m.site_id as m_site_id, m.site_id as m_site_id,
m.member_class_id as m_member_class_id, m.member_class_id as m_member_class_id,
@ -203,7 +241,7 @@ SELECT
pm_mba_b.created_at as pm_mba_b_created_at, pm_mba_b.created_at as pm_mba_b_created_at,
pm_mba_b.updated_at as pm_mba_b_updated_at pm_mba_b.updated_at as pm_mba_b_updated_at
FROM members as m FROM members as m
INNER JOIN sites m_s INNER JOIN sites m_s
ON m_s.id = m.site_id ON m_s.id = m.site_id
INNER JOIN member_classes m_mc INNER JOIN member_classes m_mc
@ -310,18 +348,70 @@ impl Composition {
} }
} }
/// fn get_pagination(
pub fn select_all(
&self, &self,
conn: &diesel::PgConnection,
find_all: &repositories::member::models::FindAll, find_all: &repositories::member::models::FindAll,
) -> Result<Vec<models::MemberModel>, Error> { ) -> Result<String, Error> {
use std::fmt::Write; let mut query_pagination = String::new();
let mut query = String::new(); if let Some(pagination) = &find_all.pagination {
write!(&mut query, "{}", MEMBER_QUERY) let page = pagination.page.unwrap_or(1);
if let Some(page_size) = pagination.page_size {
write!(
&mut query_pagination,
" OFFSET {} LIMIT {} ",
((page - 1) * page_size) as i64,
page_size as i64
)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
}
Ok(query_pagination)
}
fn get_ordery_by(
&self,
find_all: &repositories::member::models::FindAll,
) -> Result<String, Error> {
let mut query_ordery_by = String::new();
if let Some(sorts) = &find_all.sorts {
for s in sorts {
if !query_ordery_by.is_empty() {
write!(&mut query_ordery_by, " , ")
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
let (property, sort) = match s {
bcr::pagination::Sort::ASC(property) => (property.clone(), "ASC".to_string()),
bcr::pagination::Sort::DESC(property) => (property.clone(), "DESC".to_string()),
};
let property = match property.as_str() {
"username" => "m.username".to_string(),
"nickname" => "m.nickname".to_string(),
"mobile_phone_number" => "m.mobile_phone_number".to_string(),
"last_signined_ip" => "m.last_signined_ip".to_string(),
"state" => "m.state".to_string(),
"state_changed_at" => "m.state_changed_at".to_string(),
"created_at" => "m.created_at".to_string(),
"updated_at" => "m.updated_at".to_string(),
"deleted_at" => "m.deleted_at".to_string(),
_ => "".to_string(),
};
if !property.is_empty() {
write!(&mut query_ordery_by, "{} {}", property, sort)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
}
}
Ok(query_ordery_by)
}
fn get_where(&self, find_all: &repositories::member::models::FindAll) -> Result<String, Error> {
let mut query_where = String::new(); let mut query_where = String::new();
if let Some(s) = &find_all.search { if let Some(s) = &find_all.search {
@ -349,6 +439,32 @@ impl Composition {
write!(&mut query_where, "m.member_level_id = '{}'", sp) write!(&mut query_where, "m.member_level_id = '{}'", sp)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
} }
if let Some(sp) = &s.username {
if !query_where.is_empty() {
write!(&mut query_where, " AND ")
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
write!(&mut query_where, "m.username = '{}'", sp)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
if !s.usernames.is_empty() {
if !query_where.is_empty() {
write!(&mut query_where, " AND ")
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
write!(
&mut query_where,
"m.username IN ({})",
s.usernames
.iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<String>>()
.join(",")
)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
if let Some(sp) = &s.username_like { if let Some(sp) = &s.username_like {
if !query_where.is_empty() { if !query_where.is_empty() {
write!(&mut query_where, " AND ") write!(&mut query_where, " AND ")
@ -357,6 +473,31 @@ impl Composition {
write!(&mut query_where, "m.username like '{}'", sp) write!(&mut query_where, "m.username like '{}'", sp)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
} }
if let Some(sp) = &s.nickname {
if !query_where.is_empty() {
write!(&mut query_where, " AND ")
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
write!(&mut query_where, "m.nickname = '{}'", sp)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
if !s.nicknames.is_empty() {
if !query_where.is_empty() {
write!(&mut query_where, " AND ")
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
write!(
&mut query_where,
"m.nickname IN ({})",
s.nicknames
.iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<String>>()
.join(",")
)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
if let Some(sp) = &s.nickname_like { if let Some(sp) = &s.nickname_like {
if !query_where.is_empty() { if !query_where.is_empty() {
write!(&mut query_where, " AND ") write!(&mut query_where, " AND ")
@ -402,7 +543,28 @@ impl Composition {
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
} }
} }
Ok(query_where)
}
///
pub fn select_all_count(
&self,
conn: &diesel::PgConnection,
find_all: &repositories::member::models::FindAll,
) -> Result<i64, Error> {
use diesel::sql_types::BigInt;
#[derive(QueryableByName)]
struct Count {
#[sql_type = "BigInt"]
count: i64,
}
let mut query = String::new();
write!(&mut query, "{}", MEMBER_COUNT_QUERY)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
let query_where = self.get_where(find_all)?;
if !query_where.is_empty() { if !query_where.is_empty() {
write!(&mut query, " WHERE {}", query_where) write!(&mut query, " WHERE {}", query_where)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
@ -410,6 +572,43 @@ impl Composition {
println!("query: {}", query); println!("query: {}", query);
match sql_query(query).get_result::<Count>(conn) {
Ok(m) => Ok(m.count),
Err(e) => match e {
diesel::result::Error::NotFound => Ok(0),
_ => Err(e),
},
}
}
///
pub fn select_all(
&self,
conn: &diesel::PgConnection,
find_all: &repositories::member::models::FindAll,
) -> Result<Vec<models::MemberModel>, Error> {
let mut query = String::new();
write!(&mut query, "{}", MEMBER_QUERY)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
let query_where = self.get_where(find_all)?;
if !query_where.is_empty() {
write!(&mut query, " WHERE {}", query_where)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
let query_ordery_by = self.get_ordery_by(find_all)?;
if !query_ordery_by.is_empty() {
write!(&mut query, " ORDER BY {}", query_ordery_by)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
let query_pagination = self.get_pagination(find_all)?;
if !query_pagination.is_empty() {
write!(&mut query, " {} ", query_pagination)
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
}
println!("query: {}", query);
match sql_query(query).get_results::<models::MemberModel>(conn) { match sql_query(query).get_results::<models::MemberModel>(conn) {
Ok(m) => Ok(m), Ok(m) => Ok(m),
Err(e) => match e { Err(e) => match e {

View File

@ -123,8 +123,16 @@ pub struct FindAllSearch {
/// ///
pub parent_member_id: Option<uuid::Uuid>, pub parent_member_id: Option<uuid::Uuid>,
/// ///
pub username: Option<String>,
///
pub usernames: Vec<String>,
///
pub username_like: Option<String>, pub username_like: Option<String>,
/// ///
pub nickname: Option<String>,
///
pub nicknames: Vec<String>,
///
pub nickname_like: Option<String>, pub nickname_like: Option<String>,
/// ///
pub mobile_phone_number_like: Option<String>, pub mobile_phone_number_like: Option<String>,

View File

@ -714,7 +714,11 @@ impl Service<'_> {
member_class_id, member_class_id,
member_level_id, member_level_id,
parent_member_id, parent_member_id,
username: s.username,
usernames: s.usernames,
username_like: s.username_like, username_like: s.username_like,
nickname: s.nickname,
nicknames: s.nicknames,
nickname_like: s.nickname_like, nickname_like: s.nickname_like,
mobile_phone_number_like: s.mobile_phone_number_like, mobile_phone_number_like: s.mobile_phone_number_like,
last_signined_ip: s.last_signined_ip, last_signined_ip: s.last_signined_ip,