refactoring
This commit is contained in:
parent
0cd715ddb9
commit
5515841d49
|
@ -30,7 +30,7 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
|||
tokio-cron-scheduler = { version = "0" }
|
||||
uuid = { version = "0", features = ["serde", "v4", "v5"] }
|
||||
|
||||
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.29-snapshot" }
|
||||
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.12-snapshot" }
|
||||
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.32-snapshot" }
|
||||
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.15-snapshot" }
|
||||
|
||||
[build-dependencies]
|
||||
|
|
|
@ -2,7 +2,7 @@ CREATE TABLE IF NOT EXISTS member_levels (
|
|||
id UUID DEFAULT uuid_generate_v4(),
|
||||
name TEXT NOT NULL,
|
||||
show BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
sort_order SMALLINT NOT NULL,
|
||||
sort_order INTEGER NOT NULL,
|
||||
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
||||
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
||||
deleted_at BIGINT,
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//!
|
||||
//!
|
||||
|
||||
use diesel::{result::Error, sql_query, RunQueryDsl};
|
||||
|
||||
use super::models;
|
||||
use crate::repositories;
|
||||
use diesel::{result::Error, sql_query, RunQueryDsl};
|
||||
|
||||
pub struct Composition {}
|
||||
|
||||
|
@ -30,7 +30,7 @@ impl Composition {
|
|||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
id: uuid::Uuid,
|
||||
) -> Result<Option<models::Member>, Error> {
|
||||
) -> Result<Option<models::MemberModel>, Error> {
|
||||
match sql_query(
|
||||
"
|
||||
SELECT
|
||||
|
@ -109,7 +109,7 @@ impl Composition {
|
|||
",
|
||||
)
|
||||
.bind::<diesel::sql_types::Uuid, _>(id)
|
||||
.get_result::<models::Member>(conn)
|
||||
.get_result::<models::MemberModel>(conn)
|
||||
{
|
||||
Ok(m) => Ok(Some(m)),
|
||||
Err(e) => match e {
|
||||
|
@ -118,4 +118,184 @@ impl Composition {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn select_all(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
find_all: &repositories::member::models::FindAll,
|
||||
) -> Result<Vec<models::MemberModel>, Error> {
|
||||
use std::fmt::Write;
|
||||
|
||||
let mut query = String::new();
|
||||
write!(
|
||||
&mut query,
|
||||
"
|
||||
SELECT
|
||||
m.id as m_id,
|
||||
m.site_id as m_site_id,
|
||||
m.member_class_id as m_member_class_id,
|
||||
m.member_level_id as m_member_level_id,
|
||||
m.username as m_username,
|
||||
m.password as m_password,
|
||||
m.nickname as m_nickname,
|
||||
m.mobile_phone_number as m_mobile_phone_number,
|
||||
m.state as m_state,
|
||||
m.state_changed_at as m_state_changed_at,
|
||||
m.referrer_member_id as m_referrer_member_id,
|
||||
m.referred_count as m_referred_count,
|
||||
m.last_signined_ip as m_last_signined_ip,
|
||||
m.last_signined_at as m_last_signined_at,
|
||||
m.created_at as m_created_at,
|
||||
m.updated_at as m_updated_at,
|
||||
m.deleted_at as m_deleted_at,
|
||||
|
||||
s.id as s_id,
|
||||
s.url as s_url,
|
||||
s.name as s_name,
|
||||
s.path as s_path,
|
||||
s.show as s_show,
|
||||
s.can_use as s_can_use,
|
||||
s.memo as s_memo,
|
||||
s.expires_at as s_expires_at,
|
||||
s.created_at as s_created_at,
|
||||
s.updated_at as s_updated_at,
|
||||
|
||||
mc.id as mc_id,
|
||||
mc.parent_id as mc_parent_id,
|
||||
mc.name as mc_name,
|
||||
mc.created_at as mc_created_at,
|
||||
mc.updated_at as mc_updated_at,
|
||||
mc.deleted_at as mc_deleted_at,
|
||||
|
||||
ml.id as ml_id,
|
||||
ml.name as ml_name,
|
||||
ml.sort_order as ml_sort_order,
|
||||
ml.created_at as ml_created_at,
|
||||
ml.updated_at as ml_updated_at,
|
||||
ml.deleted_at as ml_deleted_at,
|
||||
|
||||
_m.id as _m_id,
|
||||
_m.member_site_id as _m_member_site_id,
|
||||
_m.member_class_id as _m_member_class_id,
|
||||
_m.member_level_id as _m_member_level_id,
|
||||
_m.username as _m_username,
|
||||
_m.password as _m_password,
|
||||
_m.nickname as _m_nickname,
|
||||
_m.mobile_phone_number as _m_mobile_phone_number,
|
||||
_m.state as _m_state,
|
||||
_m.state_changed_at as _m_state_changed_at,
|
||||
_m.referrer_member_id as _m_referrer_member_id,
|
||||
_m.referred_count as _m_referred_count,
|
||||
_m.last_signined_ip as _m_last_signined_ip,
|
||||
_m.last_signined_at as _m_last_signined_at,
|
||||
_m.created_at as _m_created_at,
|
||||
_m.updated_at as _m_updated_at,
|
||||
_m.deleted_at as _m_deleted_at
|
||||
|
||||
FROM members as m
|
||||
JOIN sites s
|
||||
ON s.id = m.site_id
|
||||
JOIN member_classes mc
|
||||
ON mc.id = m.member_class_id
|
||||
JOIN member_levels ml
|
||||
ON ml.id = m.member_level_id
|
||||
JOIN members _m
|
||||
ON _m.id = m.referrer_member_id
|
||||
"
|
||||
)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
|
||||
let mut query_where = String::new();
|
||||
|
||||
if let Some(sp) = find_all.site_id {
|
||||
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.site_id = '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = find_all.member_class_id {
|
||||
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.member_class_id = '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = find_all.member_level_id {
|
||||
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.member_level_id = '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = &find_all.username_like {
|
||||
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 like '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = &find_all.nickname_like {
|
||||
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 like '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = &find_all.mobile_phone_number_like {
|
||||
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.mobile_phone_number like '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = &find_all.last_signined_ip {
|
||||
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.last_signined_ip = '{}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if let Some(sp) = find_all.state {
|
||||
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.state = '{:?}'", sp)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
|
||||
if !query_where.is_empty() {
|
||||
write!(&mut query_where, " and ")
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
if find_all.deleted_at.is_some() {
|
||||
write!(&mut query_where, "m.deleted_at is NOT NULL")
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
} else {
|
||||
write!(&mut query_where, "m.deleted_at is NULL")
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
|
||||
if !query_where.is_empty() {
|
||||
write!(&mut query, "{}", query_where)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
|
||||
match sql_query(query).get_results::<models::MemberModel>(conn) {
|
||||
Ok(m) => Ok(m),
|
||||
Err(e) => match e {
|
||||
diesel::result::Error::NotFound => Ok(vec![]),
|
||||
_ => Err(e),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ use diesel::deserialize::QueryableByName;
|
|||
|
||||
///
|
||||
#[derive(Eq, Hash, PartialEq, Debug, Clone)]
|
||||
pub struct Member {
|
||||
pub struct MemberModel {
|
||||
///
|
||||
pub id: uuid::Uuid,
|
||||
///
|
||||
|
@ -46,9 +46,15 @@ pub struct Member {
|
|||
pub deleted_at: Option<i64>,
|
||||
}
|
||||
|
||||
impl QueryableByName<diesel::pg::Pg> for Member {
|
||||
impl QueryableByName<diesel::pg::Pg> for MemberModel {
|
||||
fn build<R: diesel::row::NamedRow<diesel::pg::Pg>>(row: &R) -> diesel::deserialize::Result<Self> {
|
||||
let referrer_member = _Member {
|
||||
let referrer_member_id = row
|
||||
.get::<diesel::sql_types::Nullable<diesel::sql_types::Uuid>, Option<uuid::Uuid>>(
|
||||
"m_referrer_member_id",
|
||||
)?;
|
||||
|
||||
let referrer_member = match referrer_member_id {
|
||||
Some(_) => Some(_Member {
|
||||
id: row.get("_m_id")?,
|
||||
site_id: row.get("_m_site_id")?,
|
||||
member_class_id: row.get("_m_member_class_id")?,
|
||||
|
@ -66,9 +72,11 @@ impl QueryableByName<diesel::pg::Pg> for Member {
|
|||
created_at: row.get("_m_created_at")?,
|
||||
updated_at: row.get("_m_updated_at")?,
|
||||
deleted_at: row.get("_m_deleted_at")?,
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(Member {
|
||||
Ok(MemberModel {
|
||||
id: row.get("m_id")?,
|
||||
site: _Site {
|
||||
id: row.get("s_id")?,
|
||||
|
@ -104,7 +112,7 @@ impl QueryableByName<diesel::pg::Pg> for Member {
|
|||
mobile_phone_number: row.get("m_mobile_phone_number")?,
|
||||
state: row.get("m_state")?,
|
||||
state_changed_at: row.get("m_state_changed_at")?,
|
||||
referrer_member: None,
|
||||
referrer_member,
|
||||
referred_count: row.get("m_referred_count")?,
|
||||
last_signined_ip: row.get("m_last_signined_ip")?,
|
||||
last_signined_at: row.get("m_last_signined_at")?,
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -59,6 +59,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let server_broker_opts = nats::asynk::Options::new();
|
||||
let connection_server_broker = server_broker_opts.connect(url_server_broker).await?;
|
||||
|
||||
let site_service = services::site::service::Service::new(
|
||||
connection_server_broker.clone(),
|
||||
queue_server_broker.clone(),
|
||||
pool.clone(),
|
||||
);
|
||||
let identity_service = services::identity::service::Service::new(
|
||||
connection_server_broker.clone(),
|
||||
queue_server_broker.clone(),
|
||||
|
@ -67,7 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
password_salt.clone(),
|
||||
jwt_secret.clone(),
|
||||
);
|
||||
let site_service = services::site::service::Service::new(
|
||||
let member_service = services::member::service::Service::new(
|
||||
connection_server_broker.clone(),
|
||||
queue_server_broker.clone(),
|
||||
pool.clone(),
|
||||
|
@ -75,7 +80,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
println!("Server service [beteran-server-service] is started");
|
||||
|
||||
futures::try_join!(site_service.subscribe(), identity_service.subscribe(),)?;
|
||||
futures::try_join!(
|
||||
site_service.subscribe(),
|
||||
identity_service.subscribe(),
|
||||
member_service.subscribe()
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ impl Repository {
|
|||
pub fn select_all_count(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
find_all: models::FindAll,
|
||||
find_all: &models::FindAll,
|
||||
) -> Result<i64, Error> {
|
||||
use members::dsl;
|
||||
|
||||
|
@ -114,16 +114,16 @@ impl Repository {
|
|||
if let Some(sp) = find_all.member_level_id {
|
||||
q = q.filter(dsl::member_level_id.eq(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.username_like {
|
||||
if let Some(sp) = &find_all.username_like {
|
||||
q = q.filter(dsl::username.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.nickname_like {
|
||||
if let Some(sp) = &find_all.nickname_like {
|
||||
q = q.filter(dsl::nickname.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.mobile_phone_number_like {
|
||||
if let Some(sp) = &find_all.mobile_phone_number_like {
|
||||
q = q.filter(dsl::mobile_phone_number.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.last_signined_ip {
|
||||
if let Some(sp) = &find_all.last_signined_ip {
|
||||
q = q.filter(dsl::last_signined_ip.eq(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.state {
|
||||
|
@ -142,7 +142,7 @@ impl Repository {
|
|||
pub fn select_all(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
find_all: models::FindAll,
|
||||
find_all: &models::FindAll,
|
||||
) -> Result<Vec<models::Member>, Error> {
|
||||
use members::dsl;
|
||||
|
||||
|
@ -157,16 +157,16 @@ impl Repository {
|
|||
if let Some(sp) = find_all.member_level_id {
|
||||
q = q.filter(dsl::member_level_id.eq(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.username_like {
|
||||
if let Some(sp) = &find_all.username_like {
|
||||
q = q.filter(dsl::username.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.nickname_like {
|
||||
if let Some(sp) = &find_all.nickname_like {
|
||||
q = q.filter(dsl::nickname.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.mobile_phone_number_like {
|
||||
if let Some(sp) = &find_all.mobile_phone_number_like {
|
||||
q = q.filter(dsl::mobile_phone_number.like(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.last_signined_ip {
|
||||
if let Some(sp) = &find_all.last_signined_ip {
|
||||
q = q.filter(dsl::last_signined_ip.eq(sp));
|
||||
}
|
||||
if let Some(sp) = find_all.state {
|
||||
|
@ -178,7 +178,7 @@ impl Repository {
|
|||
q = q.filter(dsl::deleted_at.is_null());
|
||||
}
|
||||
|
||||
if let Some(p) = find_all.pagination {
|
||||
if let Some(p) = &find_all.pagination {
|
||||
let page = p.page.unwrap_or(1);
|
||||
|
||||
if let Some(page_size) = p.page_size {
|
||||
|
@ -186,7 +186,7 @@ impl Repository {
|
|||
q = q.limit(page_size as i64);
|
||||
}
|
||||
}
|
||||
if let Some(orderbys) = find_all.sorts {
|
||||
if let Some(orderbys) = &find_all.sorts {
|
||||
for s in orderbys {
|
||||
match s {
|
||||
bcr::models::pagination::Sort::ASC(property) => match property.as_str() {
|
||||
|
|
|
@ -10,7 +10,7 @@ pub struct MemberLevel {
|
|||
///
|
||||
pub name: String,
|
||||
///
|
||||
pub sort_order: i16,
|
||||
pub sort_order: i32,
|
||||
///
|
||||
pub created_at: i64,
|
||||
///
|
||||
|
|
|
@ -9,7 +9,7 @@ table! {
|
|||
///
|
||||
name -> Text,
|
||||
///
|
||||
sort_order -> SmallInt,
|
||||
sort_order -> Integer,
|
||||
///
|
||||
created_at -> BigInt,
|
||||
///
|
||||
|
|
2
src/services/member/mod.rs
Normal file
2
src/services/member/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod models;
|
||||
pub mod service;
|
104
src/services/member/models.rs
Normal file
104
src/services/member/models.rs
Normal file
|
@ -0,0 +1,104 @@
|
|||
use crate::compositions;
|
||||
use crate::repositories;
|
||||
use beteran_protobuf_rust as bpr;
|
||||
|
||||
impl From<i32> for repositories::member::schema::MemberState {
|
||||
fn from(s: i32) -> Self {
|
||||
match s {
|
||||
s if s == repositories::member::schema::MemberState::Normal as i32 => {
|
||||
repositories::member::schema::MemberState::Normal
|
||||
}
|
||||
s if s == repositories::member::schema::MemberState::Pending as i32 => {
|
||||
repositories::member::schema::MemberState::Pending
|
||||
}
|
||||
s if s == repositories::member::schema::MemberState::Withdrawal as i32 => {
|
||||
repositories::member::schema::MemberState::Withdrawal
|
||||
}
|
||||
s if s == repositories::member::schema::MemberState::Dormancy as i32 => {
|
||||
repositories::member::schema::MemberState::Dormancy
|
||||
}
|
||||
s if s == repositories::member::schema::MemberState::Blacklist as i32 => {
|
||||
repositories::member::schema::MemberState::Blacklist
|
||||
}
|
||||
s if s == repositories::member::schema::MemberState::Suspended as i32 => {
|
||||
repositories::member::schema::MemberState::Suspended
|
||||
}
|
||||
_ => repositories::member::schema::MemberState::Normal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&repositories::member_class::models::MemberClass> for bpr::models::member::MemberClass {
|
||||
fn from(d: &repositories::member_class::models::MemberClass) -> Self {
|
||||
bpr::models::member::MemberClass {
|
||||
id: d.id.to_string(),
|
||||
parent_id: d.parent_id.map(|d| d.to_string()),
|
||||
name: d.name.clone(),
|
||||
created_at: d.created_at as u64,
|
||||
updated_at: d.updated_at as u64,
|
||||
deleted_at: d.deleted_at.map(|d| d as u64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&repositories::member_level::models::MemberLevel> for bpr::models::member::MemberLevel {
|
||||
fn from(d: &repositories::member_level::models::MemberLevel) -> Self {
|
||||
bpr::models::member::MemberLevel {
|
||||
id: d.id.to_string(),
|
||||
name: d.name.clone(),
|
||||
sort_order: d.sort_order as u32,
|
||||
created_at: d.created_at as u64,
|
||||
updated_at: d.updated_at as u64,
|
||||
deleted_at: d.deleted_at.map(|d| d as u64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&repositories::member::models::Member> for bpr::models::member::Member {
|
||||
fn from(d: &repositories::member::models::Member) -> Self {
|
||||
bpr::models::member::Member {
|
||||
id: d.id.to_string(),
|
||||
site_id: d.site_id.to_string(),
|
||||
member_class_id: d.member_class_id.to_string(),
|
||||
member_level_id: d.member_level_id.to_string(),
|
||||
referrer_member_id: d.referrer_member_id.map(|d| d.to_string()),
|
||||
referred_count: d.referred_count as u64,
|
||||
username: d.username.clone(),
|
||||
nickname: d.nickname.clone(),
|
||||
mobile_phone_number: d.mobile_phone_number.clone(),
|
||||
state: d.state as i32,
|
||||
state_changed_at: d.state_changed_at.map(|d| d as u64),
|
||||
last_signined_ip: d.last_signined_ip.clone(),
|
||||
last_signined_at: d.last_signined_at.map(|d| d as u64),
|
||||
created_at: d.created_at as u64,
|
||||
updated_at: d.updated_at as u64,
|
||||
deleted_at: d.deleted_at.map(|d| d as u64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&compositions::member::models::MemberModel> for bpr::models::member::MemberModel {
|
||||
fn from(d: &compositions::member::models::MemberModel) -> Self {
|
||||
bpr::models::member::MemberModel {
|
||||
id: d.id.to_string(),
|
||||
site: Some(bpr::models::domain::Site::from(&d.site)),
|
||||
member_class: Some(bpr::models::member::MemberClass::from(&d.member_class)),
|
||||
member_level: Some(bpr::models::member::MemberLevel::from(&d.member_level)),
|
||||
referrer_member: d
|
||||
.referrer_member
|
||||
.as_ref()
|
||||
.map(bpr::models::member::Member::from),
|
||||
referred_count: d.referred_count as u64,
|
||||
username: d.username.clone(),
|
||||
nickname: d.nickname.clone(),
|
||||
mobile_phone_number: d.mobile_phone_number.clone(),
|
||||
state: d.state as i32,
|
||||
state_changed_at: d.state_changed_at.map(|d| d as u64),
|
||||
last_signined_ip: d.last_signined_ip.clone(),
|
||||
last_signined_at: d.last_signined_at.map(|d| d as u64),
|
||||
created_at: d.created_at as u64,
|
||||
updated_at: d.updated_at as u64,
|
||||
deleted_at: d.deleted_at.map(|d| d as u64),
|
||||
}
|
||||
}
|
||||
}
|
216
src/services/member/service.rs
Normal file
216
src/services/member/service.rs
Normal file
|
@ -0,0 +1,216 @@
|
|||
//!
|
||||
//!
|
||||
|
||||
use super::super::super::compositions;
|
||||
use super::super::super::repositories;
|
||||
use beteran_common_rust as bcr;
|
||||
use beteran_protobuf_rust as bpr;
|
||||
use diesel::{
|
||||
r2d2::{ConnectionManager, Pool},
|
||||
PgConnection,
|
||||
};
|
||||
use prost::Message;
|
||||
|
||||
///
|
||||
pub struct Service {
|
||||
connection_broker: nats::asynk::Connection,
|
||||
queue_broker: String,
|
||||
pool: Pool<ConnectionManager<PgConnection>>,
|
||||
site_composition: compositions::site::composition::Composition,
|
||||
member_composition: compositions::member::composition::Composition,
|
||||
member_repository: repositories::member::repository::Repository,
|
||||
member_session_repository: repositories::member_session::repository::Repository,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Service {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("Service of service.member.service.identity")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Service {
|
||||
///
|
||||
pub fn new(
|
||||
connection_broker: nats::asynk::Connection,
|
||||
queue_broker: String,
|
||||
pool: Pool<ConnectionManager<PgConnection>>,
|
||||
) -> Service {
|
||||
Service {
|
||||
connection_broker,
|
||||
queue_broker,
|
||||
pool,
|
||||
site_composition: compositions::site::composition::Composition::new(),
|
||||
member_composition: compositions::member::composition::Composition::new(),
|
||||
member_repository: repositories::member::repository::Repository::new(),
|
||||
member_session_repository: repositories::member_session::repository::Repository::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
|
||||
futures::try_join!(self.list_members(),).map(|_| ())
|
||||
}
|
||||
|
||||
fn check_site(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
url: Option<String>,
|
||||
site_id: uuid::Uuid,
|
||||
) -> Result<repositories::site::models::Site, bcr::error::rpc::Error> {
|
||||
match self
|
||||
.site_composition
|
||||
.select_by_url(conn, url, site_id)
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
message: format!("server {}", e),
|
||||
data: None,
|
||||
})
|
||||
})? {
|
||||
Some(s) => Ok(s),
|
||||
None => Err(bcr::error::rpc::Error::InvalidParams(
|
||||
bcr::error::rpc::InvalidParams {
|
||||
message: "invalid site_url information".to_string(),
|
||||
detail: bcr::error::rpc::InvalidParamsDetail {
|
||||
location: "request".to_string(),
|
||||
param: "client.site_url".to_string(),
|
||||
value: "".to_string(),
|
||||
error_type: bcr::error::rpc::InvalidParamsType::None,
|
||||
message: "".to_string(),
|
||||
},
|
||||
},
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn list_members(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
.queue_subscribe(
|
||||
bpr::ss::member::member::SUBJECT_LIST_MEMBERS,
|
||||
self.queue_broker.as_str(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
while let Some(message) = s.next().await {
|
||||
if let Err(e) = async {
|
||||
let req = bpr::ss::member::member::ListMembersRequest::decode(message.data.as_slice())
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
|
||||
message: format!("invalid request: {}", e),
|
||||
})
|
||||
})?;
|
||||
|
||||
let client = match req.client {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Err(bcr::error::rpc::Error::InvalidParams(
|
||||
bcr::error::rpc::InvalidParams {
|
||||
message: "invalid client information".to_string(),
|
||||
detail: bcr::error::rpc::InvalidParamsDetail {
|
||||
location: "request".to_string(),
|
||||
param: "client".to_string(),
|
||||
value: "".to_string(),
|
||||
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
||||
message: "".to_string(),
|
||||
},
|
||||
},
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let conn = self.pool.get().map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
message: format!("server {}", e),
|
||||
data: None,
|
||||
})
|
||||
})?;
|
||||
|
||||
let find_all = repositories::member::models::FindAll {
|
||||
site_id: None,
|
||||
member_class_id: None,
|
||||
member_level_id: None,
|
||||
referrer_member_id: None,
|
||||
username_like: None,
|
||||
nickname_like: None,
|
||||
mobile_phone_number_like: None,
|
||||
last_signined_ip: None,
|
||||
state: None,
|
||||
deleted_at: None,
|
||||
pagination: req
|
||||
.pagination
|
||||
.as_ref()
|
||||
.map(|d| bcr::models::pagination::Pagination::from(d)),
|
||||
sorts: Some(
|
||||
req
|
||||
.sorts
|
||||
.iter()
|
||||
.map(|d| beteran_common_rust::models::pagination::Sort::from(d))
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
|
||||
let m = self
|
||||
.member_repository
|
||||
.select_all_count(&conn, &find_all)
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
message: format!("server {}", e),
|
||||
data: None,
|
||||
})
|
||||
})?;
|
||||
|
||||
let member_list = self
|
||||
.member_composition
|
||||
.select_all(&conn, &find_all)
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
message: format!("server {}", e),
|
||||
data: None,
|
||||
})
|
||||
})?;
|
||||
|
||||
message
|
||||
.respond(
|
||||
bpr::ss::member::member::ListMembersResponse {
|
||||
error: None,
|
||||
result: Some(bpr::ss::member::member::list_members_response::Result {
|
||||
members: member_list
|
||||
.iter()
|
||||
.map(|d| bpr::models::member::MemberModel::from(d))
|
||||
.collect(),
|
||||
}),
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
message: format!("server {}", e),
|
||||
data: None,
|
||||
})
|
||||
})?;
|
||||
|
||||
Ok::<(), bcr::error::rpc::Error>(())
|
||||
}
|
||||
.await
|
||||
{
|
||||
message
|
||||
.respond(
|
||||
bpr::ss::member::member::ListMembersResponse {
|
||||
error: Some(bpr::protobuf::rpc::Error::from(e)),
|
||||
result: None,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
pub mod identity;
|
||||
pub mod member;
|
||||
pub mod site;
|
||||
|
|
Loading…
Reference in New Issue
Block a user