diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index b826503..7680cdd 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -28,10 +28,10 @@ "extensions": [ "donjayamanne.githistory", "eamodio.gitlens", - "matklad.rust-analyzer", "mhutchie.git-graph", "ms-azuretools.vscode-docker", "mutantdino.resourcemonitor", + "rust-lang.rust-analyzer", "serayuzgur.crates", "tamasfe.even-better-toml", "vadimcn.vscode-lldb" diff --git a/Cargo.toml b/Cargo.toml index a772be2..c8fd461 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ path = "./src/main.rs" [dependencies] +anyhow = { version = "=1.0.59" } captcha = { version = "0" } chrono = { version = "0" } diesel = { version = "1", features = ["chrono", "r2d2", "uuidv07", "postgres"] } @@ -30,7 +31,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.43-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.26-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.56-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.40-snapshot" } [build-dependencies] diff --git a/migrations/202206201240_member/up.sql b/migrations/202206201240_member/up.sql index c1dddf0..c4ddd48 100644 --- a/migrations/202206201240_member/up.sql +++ b/migrations/202206201240_member/up.sql @@ -12,10 +12,10 @@ CREATE TABLE IF NOT EXISTS members ( mobile_phone_number TEXT, state MEMBER_STATE DEFAULT 'normal', state_changed_at BIGINT, - last_signined_ip TEXT, - last_signined_at BIGINT, referrer_member_id UUID, referred_count BIGINT NOT NULL DEFAULT 0, + last_signined_ip TEXT, + last_signined_at BIGINT, 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, diff --git a/migrations/202207011110_member_bank_deposit/up.sql b/migrations/202207011110_member_bank_deposit/up.sql index c020752..385ea70 100644 --- a/migrations/202207011110_member_bank_deposit/up.sql +++ b/migrations/202207011110_member_bank_deposit/up.sql @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS member_bank_deposits ( amount INTEGER NOT NULL, memo TEXT, state MEMBER_BANK_DEPOSIT_STATE DEFAULT 'application', - state_changed_at BIGINT, + state_changed_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), PRIMARY KEY (id), diff --git a/migrations/202207011120_member_bank_withdraw/up.sql b/migrations/202207011120_member_bank_withdraw/up.sql index 26b828b..e941bd3 100644 --- a/migrations/202207011120_member_bank_withdraw/up.sql +++ b/migrations/202207011120_member_bank_withdraw/up.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS member_bank_withdraws ( password TEXT NOT NULL, memo TEXT, state MEMBER_BANK_WITHDRAW_STATE DEFAULT 'application', - state_changed_at BIGINT, + state_changed_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), PRIMARY KEY (id), diff --git a/src/compositions/member/composition.rs b/src/compositions/member/composition.rs index ffd0525..4838c67 100644 --- a/src/compositions/member/composition.rs +++ b/src/compositions/member/composition.rs @@ -39,11 +39,13 @@ SELECT mc.id as mc_id, mc.parent_id as mc_parent_id, mc.name as mc_name, + mc.show as mc_show, mc.created_at as mc_created_at, mc.updated_at as mc_updated_at, ml.id as ml_id, ml.name as ml_name, + ml.show as ml_show, ml.sort_order as ml_sort_order, ml.created_at as ml_created_at, ml.updated_at as ml_updated_at, @@ -187,81 +189,83 @@ impl Composition { let mut query_where = String::new(); - if let Some(sp) = find_all.site_id { - if !query_where.is_empty() { - write!(&mut query_where, " AND ") + if let Some(s) = &find_all.search { + if let Some(sp) = s.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()))?; } - 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 ") + if let Some(sp) = s.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()))?; } - 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 ") + if let Some(sp) = s.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()))?; } - 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 ") + if let Some(sp) = &s.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()))?; } - 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 ") + if let Some(sp) = &s.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()))?; } - 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 ") + if let Some(sp) = &s.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()))?; } - 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 ") + if let Some(sp) = &s.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()))?; } - 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 ") + if let Some(sp) = s.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()))?; } - 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_where, " AND ") + .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; + } + if s.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() { diff --git a/src/compositions/member/models.rs b/src/compositions/member/models.rs index a2fdd2c..4a881b9 100644 --- a/src/compositions/member/models.rs +++ b/src/compositions/member/models.rs @@ -124,12 +124,14 @@ impl QueryableByName for MemberModel { id: row.get("mc_id")?, parent_id: row.get("mc_parent_id")?, name: row.get("mc_name")?, + show: row.get("mc_show")?, created_at: row.get("mc_created_at")?, updated_at: row.get("mc_updated_at")?, }, member_level: _MemberLevel { id: row.get("ml_id")?, name: row.get("ml_name")?, + show: row.get("ml_show")?, sort_order: row.get("ml_sort_order")?, created_at: row.get("ml_created_at")?, updated_at: row.get("ml_updated_at")?, diff --git a/src/main.rs b/src/main.rs index 1892060..37bd224 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ async fn main() -> Result<(), Box> { 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( + let bank_service = services::bank::service::Service::new( connection_server_broker.clone(), queue_server_broker.clone(), pool.clone(), @@ -76,15 +76,52 @@ async fn main() -> Result<(), Box> { connection_server_broker.clone(), queue_server_broker.clone(), pool.clone(), - password_salt, + password_salt.clone(), + ); + let member_bank_account_service = services::member_bank_account::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), + ); + let member_bank_deposit_service = services::member_bank_deposit::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), + ); + let member_bank_withdraw_service = services::member_bank_withdraw::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), + ); + let member_class_service = services::member_class::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), + ); + let member_level_service = services::member_level::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), + ); + + let site_service = services::site::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + pool.clone(), ); println!("Server service [beteran-server-service] is started"); futures::try_join!( - site_service.subscribe(), + bank_service.subscribe(), identity_service.subscribe(), - member_service.subscribe() + member_service.subscribe(), + member_bank_account_service.subscribe(), + member_bank_deposit_service.subscribe(), + member_bank_withdraw_service.subscribe(), + member_class_service.subscribe(), + member_level_service.subscribe(), + site_service.subscribe(), )?; Ok(()) diff --git a/src/repositories/bank/models.rs b/src/repositories/bank/models.rs index 01cf2ba..95adb7a 100644 --- a/src/repositories/bank/models.rs +++ b/src/repositories/bank/models.rs @@ -57,7 +57,7 @@ pub struct ModifyBank { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub name_like: Option, /// @@ -66,6 +66,11 @@ pub struct FindAll { pub can_use: Option, /// pub memo_like: Option, +} +/// +#[derive(Debug, Clone)] +pub struct FindAll { + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/bank/repository.rs b/src/repositories/bank/repository.rs index 2879f2b..bd21f10 100644 --- a/src/repositories/bank/repository.rs +++ b/src/repositories/bank/repository.rs @@ -62,17 +62,19 @@ impl Repository { ) -> Result { let mut q = banks::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(banks::dsl::name.like(sp)); - } - if let Some(sp) = find_all.show { - q = q.filter(banks::dsl::show.eq(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(banks::dsl::can_use.eq(sp)); - } - if let Some(sp) = &find_all.memo_like { - q = q.filter(banks::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(banks::dsl::name.like(sp)); + } + if let Some(sp) = s.show { + q = q.filter(banks::dsl::show.eq(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(banks::dsl::can_use.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(banks::dsl::memo.like(sp)); + } } q.count().get_result(conn) @@ -86,17 +88,19 @@ impl Repository { ) -> Result, Error> { let mut q = banks::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(banks::dsl::name.like(sp)); - } - if let Some(sp) = find_all.show { - q = q.filter(banks::dsl::show.eq(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(banks::dsl::can_use.eq(sp)); - } - if let Some(sp) = &find_all.memo_like { - q = q.filter(banks::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(banks::dsl::name.like(sp)); + } + if let Some(sp) = s.show { + q = q.filter(banks::dsl::show.eq(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(banks::dsl::can_use.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(banks::dsl::memo.like(sp)); + } } if let Some(p) = &find_all.pagination { diff --git a/src/repositories/member/models.rs b/src/repositories/member/models.rs index 98187d6..63ceef0 100644 --- a/src/repositories/member/models.rs +++ b/src/repositories/member/models.rs @@ -75,8 +75,14 @@ pub struct ModifyMember { pub password: Option, /// pub mobile_phone_number: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "members"] +pub struct ModifyMemberForState { /// - pub state: Option, + pub state: MemberState, } /// @@ -99,7 +105,7 @@ pub struct ModifyMember4DeletedAt { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub site_id: Option, /// @@ -120,6 +126,13 @@ pub struct FindAll { pub state: Option, /// pub deleted_at: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member/repository.rs b/src/repositories/member/repository.rs index 88d75d6..f4eec64 100644 --- a/src/repositories/member/repository.rs +++ b/src/repositories/member/repository.rs @@ -105,34 +105,36 @@ impl Repository { let mut q = members::table.into_boxed(); - if let Some(sp) = find_all.site_id { - q = q.filter(dsl::site_id.eq(sp)); - } - if let Some(sp) = find_all.member_class_id { - q = q.filter(dsl::member_class_id.eq(sp)); - } - 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 { - q = q.filter(dsl::username.like(sp)); - } - 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 { - q = q.filter(dsl::mobile_phone_number.like(sp)); - } - 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 { - q = q.filter(dsl::state.eq(sp)); - } - if find_all.deleted_at.is_some() { - q = q.filter(dsl::deleted_at.is_not_null()); - } else { - q = q.filter(dsl::deleted_at.is_null()); + if let Some(s) = &find_all.search { + if let Some(sp) = s.site_id { + q = q.filter(dsl::site_id.eq(sp)); + } + if let Some(sp) = s.member_class_id { + q = q.filter(dsl::member_class_id.eq(sp)); + } + if let Some(sp) = s.member_level_id { + q = q.filter(dsl::member_level_id.eq(sp)); + } + if let Some(sp) = &s.username_like { + q = q.filter(dsl::username.like(sp)); + } + if let Some(sp) = &s.nickname_like { + q = q.filter(dsl::nickname.like(sp)); + } + if let Some(sp) = &s.mobile_phone_number_like { + q = q.filter(dsl::mobile_phone_number.like(sp)); + } + if let Some(sp) = &s.last_signined_ip { + q = q.filter(dsl::last_signined_ip.eq(sp)); + } + if let Some(sp) = s.state { + q = q.filter(dsl::state.eq(sp)); + } + if s.deleted_at.is_some() { + q = q.filter(dsl::deleted_at.is_not_null()); + } else { + q = q.filter(dsl::deleted_at.is_null()); + } } q.count().get_result(conn) @@ -148,34 +150,36 @@ impl Repository { let mut q = members::table.into_boxed(); - if let Some(sp) = find_all.site_id { - q = q.filter(dsl::site_id.eq(sp)); - } - if let Some(sp) = find_all.member_class_id { - q = q.filter(dsl::member_class_id.eq(sp)); - } - 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 { - q = q.filter(dsl::username.like(sp)); - } - 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 { - q = q.filter(dsl::mobile_phone_number.like(sp)); - } - 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 { - q = q.filter(dsl::state.eq(sp)); - } - if find_all.deleted_at.is_some() { - q = q.filter(dsl::deleted_at.is_not_null()); - } else { - q = q.filter(dsl::deleted_at.is_null()); + if let Some(s) = &find_all.search { + if let Some(sp) = s.site_id { + q = q.filter(dsl::site_id.eq(sp)); + } + if let Some(sp) = s.member_class_id { + q = q.filter(dsl::member_class_id.eq(sp)); + } + if let Some(sp) = s.member_level_id { + q = q.filter(dsl::member_level_id.eq(sp)); + } + if let Some(sp) = &s.username_like { + q = q.filter(dsl::username.like(sp)); + } + if let Some(sp) = &s.nickname_like { + q = q.filter(dsl::nickname.like(sp)); + } + if let Some(sp) = &s.mobile_phone_number_like { + q = q.filter(dsl::mobile_phone_number.like(sp)); + } + if let Some(sp) = &s.last_signined_ip { + q = q.filter(dsl::last_signined_ip.eq(sp)); + } + if let Some(sp) = s.state { + q = q.filter(dsl::state.eq(sp)); + } + if s.deleted_at.is_some() { + q = q.filter(dsl::deleted_at.is_not_null()); + } else { + q = q.filter(dsl::deleted_at.is_null()); + } } if let Some(p) = &find_all.pagination { @@ -271,6 +275,21 @@ impl Repository { .map(|c| c as u64) } + /// + pub fn update_state( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberForState, + ) -> Result { + use members::dsl; + + diesel::update(dsl::members.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + /// pub fn update_increase_referred_count( &self, diff --git a/src/repositories/member_bank_account/models.rs b/src/repositories/member_bank_account/models.rs index 38ca605..a502ef1 100644 --- a/src/repositories/member_bank_account/models.rs +++ b/src/repositories/member_bank_account/models.rs @@ -55,7 +55,7 @@ pub struct ModifyMemberBankAccount { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub member_id: Option, /// @@ -66,6 +66,13 @@ pub struct FindAll { pub account_number_like: Option, /// pub memo_like: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_bank_account/repository.rs b/src/repositories/member_bank_account/repository.rs index 5cce517..af31c11 100644 --- a/src/repositories/member_bank_account/repository.rs +++ b/src/repositories/member_bank_account/repository.rs @@ -45,34 +45,43 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - member_bank_accounts::table + ) -> Result, Error> { + match member_bank_accounts::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_bank_accounts::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.bank_id { - q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_accounts::dsl::name.like(sp)); - } - if let Some(sp) = find_all.account_number_like { - q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_accounts::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); + } + if let Some(sp) = s.bank_id { + q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_accounts::dsl::name.like(sp)); + } + if let Some(sp) = &s.account_number_like { + q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_accounts::dsl::memo.like(sp)); + } } q.count().get_result(conn) @@ -82,27 +91,29 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_bank_accounts::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.bank_id { - q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_accounts::dsl::name.like(sp)); - } - if let Some(sp) = find_all.account_number_like { - q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_accounts::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); + } + if let Some(sp) = s.bank_id { + q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_accounts::dsl::name.like(sp)); + } + if let Some(sp) = &s.account_number_like { + q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_accounts::dsl::memo.like(sp)); + } } - 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 { @@ -110,7 +121,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() { @@ -165,4 +176,13 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use member_bank_accounts::dsl; + + diesel::delete(member_bank_accounts::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/member_bank_deposit/models.rs b/src/repositories/member_bank_deposit/models.rs index 4d3779a..d7ee701 100644 --- a/src/repositories/member_bank_deposit/models.rs +++ b/src/repositories/member_bank_deposit/models.rs @@ -14,11 +14,11 @@ pub struct MemberBankDeposit { /// pub amount: i32, /// + pub memo: Option, + /// pub state: MemberBankDepositState, /// - pub state_changed_at: Option, - /// - pub memo: Option, + pub state_changed_at: i64, /// pub created_at: i64, /// @@ -49,13 +49,19 @@ pub struct ModifyMemberBankDeposit { pub amount: i32, /// pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_bank_deposits"] +pub struct ModifyMemberBankDepositForState { /// - pub state: Option, + pub state: MemberBankDepositState, } /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub member_id: Option, /// @@ -66,6 +72,13 @@ pub struct FindAll { pub memo_like: Option, /// pub state: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_bank_deposit/repository.rs b/src/repositories/member_bank_deposit/repository.rs index ce882a8..fe84c13 100644 --- a/src/repositories/member_bank_deposit/repository.rs +++ b/src/repositories/member_bank_deposit/repository.rs @@ -45,34 +45,43 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - member_bank_deposits::table + ) -> Result, Error> { + match member_bank_deposits::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_bank_deposits::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_deposits::dsl::name.like(sp)); - } - if let Some(sp) = find_all.amount { - q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_deposits::dsl::memo.like(sp)); - } - if let Some(sp) = find_all.state { - q = q.filter(member_bank_deposits::dsl::state.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_deposits::dsl::name.like(sp)); + } + if let Some(sp) = s.amount { + q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_deposits::dsl::memo.like(sp)); + } + if let Some(sp) = s.state { + q = q.filter(member_bank_deposits::dsl::state.eq(sp)); + } } q.count().get_result(conn) @@ -82,27 +91,29 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_bank_deposits::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_deposits::dsl::name.like(sp)); - } - if let Some(sp) = find_all.amount { - q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_deposits::dsl::memo.like(sp)); - } - if let Some(sp) = find_all.state { - q = q.filter(member_bank_deposits::dsl::state.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_deposits::dsl::name.like(sp)); + } + if let Some(sp) = s.amount { + q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_deposits::dsl::memo.like(sp)); + } + if let Some(sp) = s.state { + q = q.filter(member_bank_deposits::dsl::state.eq(sp)); + } } - 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 { @@ -110,7 +121,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() { @@ -171,4 +182,28 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn update_state( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberBankDepositForState, + ) -> Result { + use member_bank_deposits::dsl; + + diesel::update(dsl::member_bank_deposits.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use member_bank_deposits::dsl; + + diesel::delete(member_bank_deposits::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/member_bank_deposit/schema.rs b/src/repositories/member_bank_deposit/schema.rs index 3e21bd6..e6a0fa1 100644 --- a/src/repositories/member_bank_deposit/schema.rs +++ b/src/repositories/member_bank_deposit/schema.rs @@ -23,11 +23,11 @@ table! { /// amount -> Integer, /// + memo -> Nullable, + /// state -> MemberBankDepositStateMapping, /// - state_changed_at -> Nullable, - /// - memo -> Nullable, + state_changed_at -> BigInt, /// created_at -> BigInt, /// diff --git a/src/repositories/member_bank_withdraw/models.rs b/src/repositories/member_bank_withdraw/models.rs index 9d55118..5faeac7 100644 --- a/src/repositories/member_bank_withdraw/models.rs +++ b/src/repositories/member_bank_withdraw/models.rs @@ -20,11 +20,11 @@ pub struct MemberBankWithdraw { /// pub password: String, /// + pub memo: Option, + /// pub state: MemberBankWithdrawState, /// - pub state_changed_at: Option, - /// - pub memo: Option, + pub state_changed_at: i64, /// pub created_at: i64, /// @@ -67,13 +67,19 @@ pub struct ModifyMemberBankWithdraw { pub password: String, /// pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_bank_withdraws"] +pub struct ModifyMemberBankWithdrawForState { /// - pub state: Option, + pub state: MemberBankWithdrawState, } /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub member_id: Option, /// @@ -88,6 +94,13 @@ pub struct FindAll { pub memo_like: Option, /// pub state: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_bank_withdraw/repository.rs b/src/repositories/member_bank_withdraw/repository.rs index fce34b0..ad04c48 100644 --- a/src/repositories/member_bank_withdraw/repository.rs +++ b/src/repositories/member_bank_withdraw/repository.rs @@ -45,40 +45,49 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - member_bank_withdraws::table + ) -> Result, Error> { + match member_bank_withdraws::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_bank_withdraws::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.bank_name_like { - q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_withdraws::dsl::name.like(sp)); - } - if let Some(sp) = find_all.account_number_like { - q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); - } - if let Some(sp) = find_all.amount { - q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); - } - if let Some(sp) = find_all.state { - q = q.filter(member_bank_withdraws::dsl::state.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); + } + if let Some(sp) = &s.bank_name_like { + q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_withdraws::dsl::name.like(sp)); + } + if let Some(sp) = &s.account_number_like { + q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); + } + if let Some(sp) = s.amount { + q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); + } + if let Some(sp) = s.state { + q = q.filter(member_bank_withdraws::dsl::state.eq(sp)); + } } q.count().get_result(conn) @@ -88,33 +97,35 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_bank_withdraws::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.bank_name_like { - q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(member_bank_withdraws::dsl::name.like(sp)); - } - if let Some(sp) = find_all.account_number_like { - q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); - } - if let Some(sp) = find_all.amount { - q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); - } - if let Some(sp) = find_all.memo_like { - q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); - } - if let Some(sp) = find_all.state { - q = q.filter(member_bank_withdraws::dsl::state.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); + } + if let Some(sp) = &s.bank_name_like { + q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(member_bank_withdraws::dsl::name.like(sp)); + } + if let Some(sp) = &s.account_number_like { + q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); + } + if let Some(sp) = s.amount { + q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); + } + if let Some(sp) = s.state { + q = q.filter(member_bank_withdraws::dsl::state.eq(sp)); + } } - 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 { @@ -122,7 +133,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() { @@ -201,4 +212,28 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn update_state( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberBankWithdrawForState, + ) -> Result { + use member_bank_withdraws::dsl; + + diesel::update(dsl::member_bank_withdraws.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use member_bank_withdraws::dsl; + + diesel::delete(member_bank_withdraws::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/member_bank_withdraw/schema.rs b/src/repositories/member_bank_withdraw/schema.rs index 18672ae..b624f64 100644 --- a/src/repositories/member_bank_withdraw/schema.rs +++ b/src/repositories/member_bank_withdraw/schema.rs @@ -29,11 +29,11 @@ table! { /// password -> Text, /// + memo -> Nullable, + /// state -> MemberBankWithdrawStateMapping, /// - state_changed_at -> Nullable, - /// - memo -> Nullable, + state_changed_at -> BigInt, /// created_at -> BigInt, /// diff --git a/src/repositories/member_class/models.rs b/src/repositories/member_class/models.rs index 392f299..e7a5092 100644 --- a/src/repositories/member_class/models.rs +++ b/src/repositories/member_class/models.rs @@ -12,6 +12,8 @@ pub struct MemberClass { /// pub name: String, /// + pub show: bool, + /// pub created_at: i64, /// pub updated_at: i64, @@ -25,6 +27,8 @@ pub struct NewMemberClass { pub parent_id: Option, /// pub name: String, + /// + pub show: bool, } /// @@ -35,13 +39,24 @@ pub struct ModifyMemberClass { pub parent_id: Option, /// pub name: String, + /// + pub show: bool, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAllSearch { + /// + pub name_like: Option, + /// + pub show: Option, } /// #[derive(Debug, Clone)] pub struct FindAll { /// - pub name_like: Option, + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_class/repository.rs b/src/repositories/member_class/repository.rs index de5fb37..eda64ab 100644 --- a/src/repositories/member_class/repository.rs +++ b/src/repositories/member_class/repository.rs @@ -44,10 +44,17 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - member_classes::table + ) -> Result, Error> { + match member_classes::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// @@ -58,8 +65,10 @@ impl Repository { ) -> Result { let mut q = member_classes::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(member_classes::dsl::name.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(member_classes::dsl::name.like(sp)); + } } q.count().get_result(conn) @@ -73,8 +82,10 @@ impl Repository { ) -> Result, Error> { let mut q = member_classes::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(member_classes::dsl::name.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(member_classes::dsl::name.like(sp)); + } } if let Some(p) = &find_all.pagination { diff --git a/src/repositories/member_class/schema.rs b/src/repositories/member_class/schema.rs index 60b9a25..a69ae10 100644 --- a/src/repositories/member_class/schema.rs +++ b/src/repositories/member_class/schema.rs @@ -11,6 +11,8 @@ table! { /// name -> Text, /// + show -> Bool, + /// created_at -> BigInt, /// updated_at -> BigInt, diff --git a/src/repositories/member_level/models.rs b/src/repositories/member_level/models.rs index 114d076..e58f93a 100644 --- a/src/repositories/member_level/models.rs +++ b/src/repositories/member_level/models.rs @@ -10,6 +10,8 @@ pub struct MemberLevel { /// pub name: String, /// + pub show: bool, + /// pub sort_order: i32, /// pub created_at: i64, @@ -24,6 +26,8 @@ pub struct NewMemberLevel { /// pub name: String, /// + pub show: bool, + /// pub sort_order: i32, } @@ -34,14 +38,25 @@ pub struct ModifyMemberLevel { /// pub name: String, /// + pub show: bool, + /// pub sort_order: i32, } +/// +#[derive(Debug, Clone)] +pub struct FindAllSearch { + /// + pub name_like: Option, + /// + pub show: Option, +} + /// #[derive(Debug, Clone)] pub struct FindAll { /// - pub name_like: Option, + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_level/repository.rs b/src/repositories/member_level/repository.rs index 4700de4..f058adb 100644 --- a/src/repositories/member_level/repository.rs +++ b/src/repositories/member_level/repository.rs @@ -44,10 +44,17 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - member_levels::table + ) -> Result, Error> { + match member_levels::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// @@ -58,8 +65,10 @@ impl Repository { ) -> Result { let mut q = member_levels::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(member_levels::dsl::name.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(member_levels::dsl::name.like(sp)); + } } q.count().get_result(conn) @@ -73,8 +82,10 @@ impl Repository { ) -> Result, Error> { let mut q = member_levels::table.into_boxed(); - if let Some(sp) = &find_all.name_like { - q = q.filter(member_levels::dsl::name.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.name_like { + q = q.filter(member_levels::dsl::name.like(sp)); + } } if let Some(p) = &find_all.pagination { diff --git a/src/repositories/member_level/schema.rs b/src/repositories/member_level/schema.rs index 4c67312..65aa78e 100644 --- a/src/repositories/member_level/schema.rs +++ b/src/repositories/member_level/schema.rs @@ -9,6 +9,8 @@ table! { /// name -> Text, /// + show -> Bool, + /// sort_order -> Integer, /// created_at -> BigInt, diff --git a/src/repositories/member_permission/models.rs b/src/repositories/member_permission/models.rs index 8998f1d..21cd6ea 100644 --- a/src/repositories/member_permission/models.rs +++ b/src/repositories/member_permission/models.rs @@ -26,11 +26,18 @@ pub struct NewMemberPermission { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub role_resource_action_id: Option, /// pub member_id: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_permission/repository.rs b/src/repositories/member_permission/repository.rs index 347d28b..2a6d288 100644 --- a/src/repositories/member_permission/repository.rs +++ b/src/repositories/member_permission/repository.rs @@ -58,31 +58,40 @@ impl Repository { conn: &diesel::PgConnection, member_id: uuid::Uuid, role_resource_action_id: uuid::Uuid, - ) -> Result { + ) -> Result, Error> { use member_permissions::dsl; - member_permissions::table + match member_permissions::table .filter( dsl::member_id .eq(member_id) .and(dsl::role_resource_action_id.eq(role_resource_action_id)), ) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_permissions::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_permissions::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.role_resource_action_id { - q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_permissions::dsl::member_id.eq(sp)); + } + if let Some(sp) = s.role_resource_action_id { + q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + } } q.count().get_result(conn) @@ -92,18 +101,20 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_permissions::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_permissions::dsl::member_id.eq(sp)); - } - if let Some(sp) = find_all.role_resource_action_id { - q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_permissions::dsl::member_id.eq(sp)); + } + if let Some(sp) = s.role_resource_action_id { + q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + } } - 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 { @@ -111,7 +122,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() { diff --git a/src/repositories/member_role/models.rs b/src/repositories/member_role/models.rs index 1d65f0c..71c8eb7 100644 --- a/src/repositories/member_role/models.rs +++ b/src/repositories/member_role/models.rs @@ -26,11 +26,18 @@ pub struct NewMemberRole { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub role_id: Option, /// pub member_id: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_role/repository.rs b/src/repositories/member_role/repository.rs index 8aaf129..a11e30a 100644 --- a/src/repositories/member_role/repository.rs +++ b/src/repositories/member_role/repository.rs @@ -58,27 +58,36 @@ impl Repository { conn: &diesel::PgConnection, member_id: uuid::Uuid, role_id: uuid::Uuid, - ) -> Result { + ) -> Result, Error> { use member_roles::dsl; - member_roles::table + match member_roles::table .filter(dsl::member_id.eq(member_id).and(dsl::role_id.eq(role_id))) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_roles::table.into_boxed(); - if let Some(sp) = find_all.role_id { - q = q.filter(member_roles::dsl::role_id.eq(sp)); - } - if let Some(sp) = find_all.member_id { - q = q.filter(member_roles::dsl::member_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.role_id { + q = q.filter(member_roles::dsl::role_id.eq(sp)); + } + if let Some(sp) = s.member_id { + q = q.filter(member_roles::dsl::member_id.eq(sp)); + } } q.count().get_result(conn) @@ -88,18 +97,20 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_roles::table.into_boxed(); - if let Some(sp) = find_all.role_id { - q = q.filter(member_roles::dsl::role_id.eq(sp)); - } - if let Some(sp) = find_all.member_id { - q = q.filter(member_roles::dsl::member_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.role_id { + q = q.filter(member_roles::dsl::role_id.eq(sp)); + } + if let Some(sp) = s.member_id { + q = q.filter(member_roles::dsl::member_id.eq(sp)); + } } - 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 { @@ -107,7 +118,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() { diff --git a/src/repositories/member_session/models.rs b/src/repositories/member_session/models.rs index d9a3840..99277f7 100644 --- a/src/repositories/member_session/models.rs +++ b/src/repositories/member_session/models.rs @@ -43,9 +43,16 @@ pub struct ModifyMemberSessionForLastAccess { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub member_id: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/member_session/repository.rs b/src/repositories/member_session/repository.rs index 326df49..3fb0ccc 100644 --- a/src/repositories/member_session/repository.rs +++ b/src/repositories/member_session/repository.rs @@ -81,12 +81,14 @@ impl Repository { pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_sessions::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_sessions::dsl::member_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_sessions::dsl::member_id.eq(sp)); + } } q.count().get_result(conn) @@ -96,15 +98,17 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_sessions::table.into_boxed(); - if let Some(sp) = find_all.member_id { - q = q.filter(member_sessions::dsl::member_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.member_id { + q = q.filter(member_sessions::dsl::member_id.eq(sp)); + } } - 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 { @@ -112,7 +116,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() { @@ -167,6 +171,15 @@ impl Repository { .map(|c| c as u64) } + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use member_sessions::dsl; + + diesel::delete(member_sessions::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } + /// pub fn delete_expired(&self, conn: &diesel::PgConnection) -> Result { use member_sessions::dsl; diff --git a/src/repositories/resource/models.rs b/src/repositories/resource/models.rs index 2896e3f..cbdc806 100644 --- a/src/repositories/resource/models.rs +++ b/src/repositories/resource/models.rs @@ -65,7 +65,7 @@ pub struct ModifyResource4CanUse { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub parent_id: Option, /// @@ -76,6 +76,13 @@ pub struct FindAll { pub description_like: Option, /// pub can_use: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/resource/repository.rs b/src/repositories/resource/repository.rs index e2fe17d..ffc787b 100644 --- a/src/repositories/resource/repository.rs +++ b/src/repositories/resource/repository.rs @@ -44,34 +44,43 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - resources::table + ) -> Result, Error> { + match resources::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = resources::table.into_boxed(); - if let Some(sp) = find_all.parent_id { - q = q.filter(resources::dsl::parent_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(resources::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(resources::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(resources::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(resources::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.parent_id { + q = q.filter(resources::dsl::parent_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(resources::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(resources::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(resources::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(resources::dsl::can_use.eq(sp)); + } } q.count().get_result(conn) @@ -81,27 +90,29 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = resources::table.into_boxed(); - if let Some(sp) = find_all.parent_id { - q = q.filter(resources::dsl::parent_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(resources::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(resources::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(resources::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(resources::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.parent_id { + q = q.filter(resources::dsl::parent_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(resources::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(resources::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(resources::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(resources::dsl::can_use.eq(sp)); + } } - 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 { @@ -109,7 +120,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() { @@ -185,4 +196,13 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use resources::dsl; + + diesel::delete(resources::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/resource_action/models.rs b/src/repositories/resource_action/models.rs index cd965c6..1b7078a 100644 --- a/src/repositories/resource_action/models.rs +++ b/src/repositories/resource_action/models.rs @@ -65,7 +65,7 @@ pub struct ModifyResourceAction4CanUse { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub resource_id: Option, /// @@ -76,6 +76,13 @@ pub struct FindAll { pub description_like: Option, /// pub can_use: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/resource_action/repository.rs b/src/repositories/resource_action/repository.rs index aed23dc..9c14c68 100644 --- a/src/repositories/resource_action/repository.rs +++ b/src/repositories/resource_action/repository.rs @@ -44,34 +44,43 @@ impl Repository { &self, conn: &diesel::PgConnection, id: uuid::Uuid, - ) -> Result { - resource_actions::table + ) -> Result, Error> { + match resource_actions::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = resource_actions::table.into_boxed(); - if let Some(sp) = find_all.resource_id { - q = q.filter(resource_actions::dsl::resource_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(resource_actions::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(resource_actions::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(resource_actions::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(resource_actions::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.resource_id { + q = q.filter(resource_actions::dsl::resource_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(resource_actions::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(resource_actions::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(resource_actions::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(resource_actions::dsl::can_use.eq(sp)); + } } q.count().get_result(conn) @@ -81,27 +90,29 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = resource_actions::table.into_boxed(); - if let Some(sp) = find_all.resource_id { - q = q.filter(resource_actions::dsl::resource_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(resource_actions::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(resource_actions::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(resource_actions::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(resource_actions::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.resource_id { + q = q.filter(resource_actions::dsl::resource_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(resource_actions::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(resource_actions::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(resource_actions::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(resource_actions::dsl::can_use.eq(sp)); + } } - 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 { @@ -109,7 +120,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() { @@ -185,4 +196,13 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use resource_actions::dsl; + + diesel::delete(resource_actions::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/role/models.rs b/src/repositories/role/models.rs index 809d8e9..50f47dd 100644 --- a/src/repositories/role/models.rs +++ b/src/repositories/role/models.rs @@ -65,7 +65,7 @@ pub struct ModifyRole4CanUse { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub parent_id: Option, /// @@ -76,6 +76,13 @@ pub struct FindAll { pub description_like: Option, /// pub can_use: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/role/repository.rs b/src/repositories/role/repository.rs index 0dc1c20..9169dc9 100644 --- a/src/repositories/role/repository.rs +++ b/src/repositories/role/repository.rs @@ -40,34 +40,47 @@ impl Repository { } /// - pub fn select(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { - roles::table + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result, Error> { + match roles::table .find(id as uuid::Uuid) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = roles::table.into_boxed(); - if let Some(sp) = find_all.parent_id { - q = q.filter(roles::dsl::parent_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(roles::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(roles::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(roles::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(roles::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.parent_id { + q = q.filter(roles::dsl::parent_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(roles::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(roles::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(roles::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(roles::dsl::can_use.eq(sp)); + } } q.count().get_result(conn) @@ -81,23 +94,25 @@ impl Repository { ) -> Result, Error> { let mut q = roles::table.into_boxed(); - if let Some(sp) = find_all.parent_id { - q = q.filter(roles::dsl::parent_id.eq(sp)); - } - if let Some(sp) = find_all.name_like { - q = q.filter(roles::dsl::name.like(sp)); - } - if let Some(sp) = find_all.key_like { - q = q.filter(roles::dsl::key.like(sp)); - } - if let Some(sp) = find_all.description_like { - q = q.filter(roles::dsl::description.like(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(roles::dsl::can_use.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.parent_id { + q = q.filter(roles::dsl::parent_id.eq(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(roles::dsl::name.like(sp)); + } + if let Some(sp) = &s.key_like { + q = q.filter(roles::dsl::key.like(sp)); + } + if let Some(sp) = &s.description_like { + q = q.filter(roles::dsl::description.like(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(roles::dsl::can_use.eq(sp)); + } } - 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 { @@ -105,7 +120,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() { @@ -181,4 +196,13 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use roles::dsl; + + diesel::delete(roles::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/role_resource_action/models.rs b/src/repositories/role_resource_action/models.rs index 5f4a898..95681f8 100644 --- a/src/repositories/role_resource_action/models.rs +++ b/src/repositories/role_resource_action/models.rs @@ -26,11 +26,18 @@ pub struct NewRoleResourceAction { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub role_id: Option, /// pub resource_action_id: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/role_resource_action/repository.rs b/src/repositories/role_resource_action/repository.rs index b234641..e55bb4a 100644 --- a/src/repositories/role_resource_action/repository.rs +++ b/src/repositories/role_resource_action/repository.rs @@ -59,31 +59,40 @@ impl Repository { conn: &diesel::PgConnection, role_id: uuid::Uuid, resource_action_id: uuid::Uuid, - ) -> Result { + ) -> Result, Error> { use role_resource_actions::dsl; - role_resource_actions::table + match role_resource_actions::table .filter( dsl::role_id .eq(role_id) .and(dsl::resource_action_id.eq(resource_action_id)), ) .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } } /// pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = role_resource_actions::table.into_boxed(); - if let Some(sp) = find_all.role_id { - q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); - } - if let Some(sp) = find_all.resource_action_id { - q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.role_id { + q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); + } + if let Some(sp) = s.resource_action_id { + q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + } } q.count().get_result(conn) @@ -93,18 +102,20 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = role_resource_actions::table.into_boxed(); - if let Some(sp) = find_all.role_id { - q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); - } - if let Some(sp) = find_all.resource_action_id { - q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = s.role_id { + q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); + } + if let Some(sp) = s.resource_action_id { + q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + } } - 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 { @@ -112,7 +123,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() { diff --git a/src/repositories/site/models.rs b/src/repositories/site/models.rs index b62c3a6..b53080c 100644 --- a/src/repositories/site/models.rs +++ b/src/repositories/site/models.rs @@ -69,7 +69,7 @@ pub struct ModifySite { /// #[derive(Debug, Clone)] -pub struct FindAll { +pub struct FindAllSearch { /// pub url_like: Option, /// @@ -82,6 +82,13 @@ pub struct FindAll { pub can_use: Option, /// pub memo_like: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub search: Option, /// pub pagination: Option, /// diff --git a/src/repositories/site/repository.rs b/src/repositories/site/repository.rs index 5e1d170..031b0a8 100644 --- a/src/repositories/site/repository.rs +++ b/src/repositories/site/repository.rs @@ -82,23 +82,25 @@ impl Repository { ) -> Result { let mut q = sites::table.into_boxed(); - if let Some(sp) = &find_all.url_like { - q = q.filter(sites::dsl::url.like(sp)); - } - if let Some(sp) = &find_all.name_like { - q = q.filter(sites::dsl::name.like(sp)); - } - if let Some(sp) = &find_all.path_like { - q = q.filter(sites::dsl::path.like(sp)); - } - if let Some(sp) = find_all.show { - q = q.filter(sites::dsl::show.eq(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(sites::dsl::can_use.eq(sp)); - } - if let Some(sp) = &find_all.memo_like { - q = q.filter(sites::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.url_like { + q = q.filter(sites::dsl::url.like(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(sites::dsl::name.like(sp)); + } + if let Some(sp) = &s.path_like { + q = q.filter(sites::dsl::path.like(sp)); + } + if let Some(sp) = s.show { + q = q.filter(sites::dsl::show.eq(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(sites::dsl::can_use.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(sites::dsl::memo.like(sp)); + } } q.count().get_result(conn) @@ -112,23 +114,25 @@ impl Repository { ) -> Result, Error> { let mut q = sites::table.into_boxed(); - if let Some(sp) = &find_all.url_like { - q = q.filter(sites::dsl::url.like(sp)); - } - if let Some(sp) = &find_all.name_like { - q = q.filter(sites::dsl::name.like(sp)); - } - if let Some(sp) = &find_all.path_like { - q = q.filter(sites::dsl::path.like(sp)); - } - if let Some(sp) = find_all.show { - q = q.filter(sites::dsl::show.eq(sp)); - } - if let Some(sp) = find_all.can_use { - q = q.filter(sites::dsl::can_use.eq(sp)); - } - if let Some(sp) = &find_all.memo_like { - q = q.filter(sites::dsl::memo.like(sp)); + if let Some(s) = &find_all.search { + if let Some(sp) = &s.url_like { + q = q.filter(sites::dsl::url.like(sp)); + } + if let Some(sp) = &s.name_like { + q = q.filter(sites::dsl::name.like(sp)); + } + if let Some(sp) = &s.path_like { + q = q.filter(sites::dsl::path.like(sp)); + } + if let Some(sp) = s.show { + q = q.filter(sites::dsl::show.eq(sp)); + } + if let Some(sp) = s.can_use { + q = q.filter(sites::dsl::can_use.eq(sp)); + } + if let Some(sp) = &s.memo_like { + q = q.filter(sites::dsl::memo.like(sp)); + } } if let Some(p) = &find_all.pagination { diff --git a/src/services/bank/models.rs b/src/services/bank/models.rs index b875dec..872b433 100644 --- a/src/services/bank/models.rs +++ b/src/services/bank/models.rs @@ -1,17 +1,15 @@ use crate::repositories; use beteran_protobuf_rust as bpr; -impl From<&repositories::site::models::Site> for bpr::models::domain::Site { - fn from(d: &repositories::site::models::Site) -> Self { - bpr::models::domain::Site { +impl From<&repositories::bank::models::Bank> for bpr::models::bank::Bank { + fn from(d: &repositories::bank::models::Bank) -> Self { + bpr::models::bank::Bank { id: d.id.to_string(), - url: d.url.clone(), name: d.name.clone(), - path: d.path.clone(), show: d.show, can_use: d.can_use, memo: d.memo.clone(), - expires_at: d.expires_at.map(|d| d as u64), + sort_order: d.sort_order as u32, created_at: d.created_at as u64, updated_at: d.updated_at as u64, } diff --git a/src/services/bank/service.rs b/src/services/bank/service.rs index 84d3af4..e865ea3 100644 --- a/src/services/bank/service.rs +++ b/src/services/bank/service.rs @@ -21,6 +21,7 @@ pub struct Service { pool: Pool>, site_repository: repositories::site::repository::Repository, site_composition: compositions::site::composition::Composition, + bank_repository: repositories::bank::repository::Repository, } impl std::fmt::Debug for Service { @@ -43,19 +44,20 @@ impl Service { pool, site_repository: repositories::site::repository::Repository::new(), site_composition: compositions::site::composition::Composition::new(), + bank_repository: repositories::bank::repository::Repository::new(), } } pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( - self.list_sites(), - self.create_site(), - self.update_site(), - self.delete_site(), + self.create_bank(), + self.list_banks(), + self.get_bank(), + self.update_bank(), + self.delete_bank(), ) .map(|_| ()) } - fn check_site( &self, conn: &diesel::PgConnection, @@ -88,19 +90,19 @@ impl Service { } } - async fn list_sites(&self) -> Result<(), Box> { + async fn create_bank(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_LIST_SITES, + bpr::ss::bank::SUBJECT_CREATE_BANK, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::ListSitesRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::bank::CreateBankRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -123,6 +125,23 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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 { @@ -132,19 +151,141 @@ impl Service { }) })?; - let find_all = repositories::site::models::FindAll { - url_like: None, - name_like: None, - path_like: None, - memo_like: None, - show: None, - can_use: None, - pagination: req + let s = self + .bank_repository + .insert( + &conn, + &repositories::bank::models::NewBank { + name: request.name, + sort_order: request.sort_order as i32, + show: request.show, + can_use: request.can_use, + memo: request.memo, + }, + ) + .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::bank::CreateBankResponse { + error: None, + result: Some(bpr::ss::bank::create_bank_response::Result { + bank: Some(bpr::models::bank::Bank::from(&s)), + }), + } + .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::bank::CreateBankResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_banks(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::bank::SUBJECT_LIST_BANKS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::bank::ListBanksRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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::bank::models::FindAll { + search: match request.search { + Some(s) => Some(repositories::bank::models::FindAllSearch { + name_like: s.name_like, + memo_like: s.memo_like, + show: s.show, + can_use: s.can_use, + }), + None => None, + }, + pagination: request .pagination .as_ref() .map(|d| bcr::models::pagination::Pagination::from(d)), sorts: Some( - req + request .sorts .iter() .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) @@ -153,7 +294,7 @@ impl Service { }; let count = self - .site_repository + .bank_repository .select_all_count(&conn, &find_all) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -164,7 +305,7 @@ impl Service { })?; let list = self - .site_repository + .bank_repository .select_all(&conn, &find_all) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -176,12 +317,12 @@ impl Service { message .respond( - bpr::ss::domain::site::ListSitesResponse { + bpr::ss::bank::ListBanksResponse { error: None, - result: Some(bpr::ss::domain::site::list_sites_response::Result { - sites: list + result: Some(bpr::ss::bank::list_banks_response::Result { + banks: list .iter() - .map(|d| bpr::models::domain::Site::from(d)) + .map(|d| bpr::models::bank::Bank::from(d)) .collect(), }), } @@ -202,7 +343,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::ListSitesResponse { + bpr::ss::bank::ListBanksResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -215,23 +356,19 @@ impl Service { Ok(()) } - async fn create_site(&self) -> Result<(), Box> { + async fn get_bank(&self) -> Result<(), Box> { let s = self .connection_broker - .queue_subscribe( - bpr::ss::domain::site::SUBJECT_CREATE_SITE, - self.queue_broker.as_str(), - ) + .queue_subscribe(bpr::ss::bank::SUBJECT_GET_BANK, self.queue_broker.as_str()) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::CreateSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; + let req = bpr::ss::bank::GetBankRequest::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, @@ -250,6 +387,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -259,34 +425,20 @@ impl Service { }) })?; - let s = self - .site_repository - .insert( - &conn, - &repositories::site::models::NewSite { - url: req.url, - name: req.name, - path: req.path, - show: req.show, - can_use: req.can_use, - memo: req.memo, - expires_at: req.expires_at.map(|d| d as i64), - }, - ) - .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 s = self.bank_repository.select(&conn, 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, + }) + })?; message .respond( - bpr::ss::domain::site::CreateSiteResponse { + bpr::ss::bank::GetBankResponse { error: None, - result: Some(bpr::ss::domain::site::create_site_response::Result { - site: Some(bpr::models::domain::Site::from(&s)), + result: Some(bpr::ss::bank::get_bank_response::Result { + bank: s.map(|d| bpr::models::bank::Bank::from(&d)), }), } .encode_to_vec(), @@ -306,7 +458,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::CreateSiteResponse { + bpr::ss::bank::GetBankResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -319,37 +471,24 @@ impl Service { Ok(()) } - async fn update_site(&self) -> Result<(), Box> { + async fn update_bank(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_UPDATE_SITE, + bpr::ss::bank::SUBJECT_UPDATE_BANK, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::UpdateSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::bank::UpdateBankRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -367,7 +506,35 @@ impl Service { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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, @@ -377,18 +544,16 @@ impl Service { })?; let _affected = self - .site_repository + .bank_repository .update( &conn, id, - &repositories::site::models::ModifySite { - url: req.url, - name: req.name, - path: req.path, - show: req.show, - can_use: req.can_use, - memo: req.memo, - expires_at: req.expires_at.map(|d| d as i64), + &repositories::bank::models::ModifyBank { + name: request.name, + sort_order: request.sort_order as i32, + show: request.show, + can_use: request.can_use, + memo: request.memo, }, ) .map_err(|e| { @@ -399,7 +564,7 @@ impl Service { }) })?; - let s = self.site_repository.select(&conn, id).map_err(|e| { + let s = self.bank_repository.select(&conn, id).map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, message: format!("server {}", e), @@ -409,10 +574,10 @@ impl Service { message .respond( - bpr::ss::domain::site::UpdateSiteResponse { + bpr::ss::bank::UpdateBankResponse { error: None, - result: Some(bpr::ss::domain::site::update_site_response::Result { - site: s.map(|d| bpr::models::domain::Site::from(&d)), + result: Some(bpr::ss::bank::update_bank_response::Result { + bank: s.map(|d| bpr::models::bank::Bank::from(&d)), }), } .encode_to_vec(), @@ -432,7 +597,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::UpdateSiteResponse { + bpr::ss::bank::UpdateBankResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -445,37 +610,24 @@ impl Service { Ok(()) } - async fn delete_site(&self) -> Result<(), Box> { + async fn delete_bank(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_DELETE_SITE, + bpr::ss::bank::SUBJECT_DELETE_BANK, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::DeleteSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::bank::DeleteBankRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -493,6 +645,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -502,7 +683,7 @@ impl Service { }) })?; - let _affected = self.site_repository.delete(&conn, id).map_err(|e| { + let _affected = self.bank_repository.delete(&conn, id).map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, message: format!("server {}", e), @@ -512,9 +693,9 @@ impl Service { message .respond( - bpr::ss::domain::site::DeleteSiteResponse { + bpr::ss::bank::DeleteBankResponse { error: None, - result: Some(bpr::ss::domain::site::delete_site_response::Result {}), + result: Some(bpr::ss::bank::delete_bank_response::Result {}), } .encode_to_vec(), ) @@ -533,7 +714,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::DeleteSiteResponse { + bpr::ss::bank::DeleteBankResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } diff --git a/src/services/identity/service.rs b/src/services/identity/service.rs index 85161b5..979f9ec 100644 --- a/src/services/identity/service.rs +++ b/src/services/identity/service.rs @@ -104,21 +104,20 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::identity::SUBJECT_CHECK_USERNAME_FOR_DUPLICATION, + bpr::ss::identity::SUBJECT_CHECK_USERNAME_FOR_DUPLICATION, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::identity::CheckUsernameForDuplicationRequest::decode( - message.data.as_slice(), - ) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; + let req = + bpr::ss::identity::CheckUsernameForDuplicationRequest::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, @@ -137,7 +136,23 @@ impl Service<'_> { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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, @@ -148,7 +163,7 @@ impl Service<'_> { let m = self .member_repository - .select_by_username(&conn, &req.username) + .select_by_username(&conn, &request.username) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -159,10 +174,10 @@ impl Service<'_> { message .respond( - bpr::ss::member::identity::CheckUsernameForDuplicationResponse { + bpr::ss::identity::CheckUsernameForDuplicationResponse { error: None, result: Some( - bpr::ss::member::identity::check_username_for_duplication_response::Result { + bpr::ss::identity::check_username_for_duplication_response::Result { duplicated: m.is_some(), }, ), @@ -184,7 +199,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::identity::CheckUsernameForDuplicationResponse { + bpr::ss::identity::CheckUsernameForDuplicationResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -201,21 +216,20 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::identity::SUBJECT_CHECK_NICKNAME_FOR_DUPLICATION, + bpr::ss::identity::SUBJECT_CHECK_NICKNAME_FOR_DUPLICATION, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::identity::CheckNicknameForDuplicationRequest::decode( - message.data.as_slice(), - ) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; + let req = + bpr::ss::identity::CheckNicknameForDuplicationRequest::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, @@ -234,7 +248,23 @@ impl Service<'_> { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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, @@ -245,7 +275,7 @@ impl Service<'_> { let m = self .member_repository - .select_by_nickname(&conn, &req.nickname) + .select_by_nickname(&conn, &request.nickname) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -256,10 +286,10 @@ impl Service<'_> { message .respond( - bpr::ss::member::identity::CheckNicknameForDuplicationResponse { + bpr::ss::identity::CheckNicknameForDuplicationResponse { error: None, result: Some( - bpr::ss::member::identity::check_nickname_for_duplication_response::Result { + bpr::ss::identity::check_nickname_for_duplication_response::Result { duplicated: m.is_some(), }, ), @@ -281,7 +311,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::identity::CheckNicknameForDuplicationResponse { + bpr::ss::identity::CheckNicknameForDuplicationResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -298,15 +328,15 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::identity::SUBJECT_CAPTCHA, + bpr::ss::identity::SUBJECT_CAPTCHA, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::identity::CaptchaRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::identity::CaptchaRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -329,7 +359,23 @@ impl Service<'_> { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; let mut c = captcha::Captcha::new(); let c = c @@ -358,9 +404,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::identity::CaptchaResponse { + bpr::ss::identity::CaptchaResponse { error: None, - result: Some(bpr::ss::member::identity::captcha_response::Result { + result: Some(bpr::ss::identity::captcha_response::Result { security_code_hash, image: image_as_base64, }), @@ -382,7 +428,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::identity::CaptchaResponse { + bpr::ss::identity::CaptchaResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -399,15 +445,15 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::identity::SUBJECT_SIGNIN, + bpr::ss::identity::SUBJECT_SIGNIN, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::identity::SigninRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::identity::SigninRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -430,7 +476,23 @@ impl Service<'_> { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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, @@ -439,10 +501,10 @@ impl Service<'_> { }) })?; - let security_code_hash = req.security_code_hash; - let security_code = req.security_code; - let username = req.username; - let password = req.password; + let security_code_hash = request.security_code_hash; + let security_code = request.security_code; + let username = request.username; + let password = request.password; let security_code_matches = argon2::verify_encoded(security_code_hash.as_str(), security_code.as_bytes()).map_err( @@ -563,10 +625,12 @@ impl Service<'_> { self .connection_broker .publish( - bpr::ss::member::identity::EVENT_SUBJECT_AFTER_SIGNIN, - bpr::ss::member::identity::AfterSigninEvent { + bpr::ss::identity::EVENT_SUBJECT_AFTER_SIGNIN, + bpr::ss::identity::AfterSigninEvent { client: Some(client), - member: Some(bpr::models::member::Member::from(&m)), + event: Some(bpr::ss::identity::after_signin_event::Event { + member: Some(bpr::models::member::Member::from(&m)), + }), } .encode_to_vec(), ) @@ -581,9 +645,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::identity::SigninResponse { + bpr::ss::identity::SigninResponse { error: None, - result: Some(bpr::ss::member::identity::signin_response::Result { access_token }), + result: Some(bpr::ss::identity::signin_response::Result { access_token }), } .encode_to_vec(), ) @@ -602,7 +666,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::identity::SigninResponse { + bpr::ss::identity::SigninResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } diff --git a/src/services/member/models.rs b/src/services/member/models.rs index 21b4d0f..8965a87 100644 --- a/src/services/member/models.rs +++ b/src/services/member/models.rs @@ -28,30 +28,6 @@ impl From for repositories::member::schema::MemberState { } } -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, - } - } -} - -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, - } - } -} - impl From<&repositories::member::models::Member> for bpr::models::member::Member { fn from(d: &repositories::member::models::Member) -> Self { bpr::models::member::Member { @@ -79,9 +55,13 @@ impl From<&compositions::member::models::MemberModel> for bpr::models::member::M 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)), + site: Some(bpr::models::site::Site::from(&d.site)), + member_class: Some(bpr::models::member_class::MemberClass::from( + &d.member_class, + )), + member_level: Some(bpr::models::member_level::MemberLevel::from( + &d.member_level, + )), referrer_member: d .referrer_member .as_ref() diff --git a/src/services/member/service.rs b/src/services/member/service.rs index 4cf0015..8c7867c 100644 --- a/src/services/member/service.rs +++ b/src/services/member/service.rs @@ -61,6 +61,7 @@ impl Service<'_> { self.get_member(), self.get_member_by_username(), self.update_member(), + self.update_member_for_state(), self.delete_member(), self.event_after_signin() ) @@ -103,57 +104,20 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_CREATE_MEMBER, + bpr::ss::member::SUBJECT_CREATE_MEMBER, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::member::CreateMemberRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::member::CreateMemberRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let site_id = uuid::Uuid::from_str(req.site_id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid site_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "site_id".to_string(), - value: req.site_id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let member_class_id = uuid::Uuid::from_str(req.member_class_id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid member_class_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "member_class_id".to_string(), - value: req.member_class_id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let member_level_id = uuid::Uuid::from_str(req.member_level_id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid member_level_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "member_level_id".to_string(), - value: req.member_level_id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -171,6 +135,61 @@ impl Service<'_> { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let site_id = uuid::Uuid::from_str(request.site_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid site_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "site_id".to_string(), + value: request.site_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; + let member_class_id = + uuid::Uuid::from_str(request.member_class_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid member_class_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_class_id".to_string(), + value: request.member_class_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; + let member_level_id = + uuid::Uuid::from_str(request.member_level_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid member_level_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_level_id".to_string(), + value: request.member_level_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -180,7 +199,7 @@ impl Service<'_> { }) })?; - let referrer_member_id = match req.referrer_member_username { + let referrer_member_id = match request.referrer_member_username { Some(referrer_member_username) => { match self .member_repository @@ -214,7 +233,7 @@ impl Service<'_> { match self .member_repository - .select_by_username(&conn, &req.username) + .select_by_username(&conn, &request.username) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -229,7 +248,7 @@ impl Service<'_> { detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "username".to_string(), - value: req.username.to_string(), + value: request.username.to_string(), error_type: bcr::error::rpc::InvalidParamsType::None, message: "duplicated".to_string(), }, @@ -241,7 +260,7 @@ impl Service<'_> { match self .member_repository - .select_by_nickname(&conn, &req.nickname) + .select_by_nickname(&conn, &request.nickname) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -256,7 +275,7 @@ impl Service<'_> { detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "nickname".to_string(), - value: req.nickname.to_string(), + value: request.nickname.to_string(), error_type: bcr::error::rpc::InvalidParamsType::None, message: "duplicated".to_string(), }, @@ -267,7 +286,7 @@ impl Service<'_> { }; let password_hash = argon2::hash_encoded( - req.password.as_bytes(), + request.password.as_bytes(), self.password_salt.as_bytes(), &self.argon2_config, ) @@ -284,10 +303,10 @@ impl Service<'_> { member_class_id, member_level_id, referrer_member_id, - username: req.username, + username: request.username, password: password_hash, - nickname: req.nickname, - mobile_phone_number: req.mobile_phone_number, + nickname: request.nickname, + mobile_phone_number: request.mobile_phone_number, }; let m = self @@ -311,9 +330,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::CreateMemberResponse { + bpr::ss::member::CreateMemberResponse { error: None, - result: Some(bpr::ss::member::member::create_member_response::Result { + result: Some(bpr::ss::member::create_member_response::Result { member: member.map(|d| bpr::models::member::MemberModel::from(&d)), }), } @@ -334,7 +353,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::CreateMemberResponse { + bpr::ss::member::CreateMemberResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -351,15 +370,15 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_LIST_MEMBERS, + bpr::ss::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| { + let req = + bpr::ss::member::ListMembersRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -382,86 +401,132 @@ impl Service<'_> { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; - let site_id = match req.site_id { - Some(d) => match uuid::Uuid::from_str(d.as_str()) { - Ok(dd) => Some(dd), - Err(e) => { - return Err(bcr::error::rpc::Error::InvalidParams( - bcr::error::rpc::InvalidParams { - message: "invalid site_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "site_id".to_string(), - value: d.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }, - )); - } - }, - None => None, - }; - let member_class_id = match req.member_class_id { - Some(d) => match uuid::Uuid::from_str(d.as_str()) { - Ok(dd) => Some(dd), - Err(e) => { - return Err(bcr::error::rpc::Error::InvalidParams( - bcr::error::rpc::InvalidParams { - message: "invalid member_class_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "member_class_id".to_string(), - value: d.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }, - )); - } - }, - None => None, - }; - let member_level_id = match req.member_level_id { - Some(d) => match uuid::Uuid::from_str(d.as_str()) { - Ok(dd) => Some(dd), - Err(e) => { - return Err(bcr::error::rpc::Error::InvalidParams( - bcr::error::rpc::InvalidParams { - message: "invalid member_level_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "member_level_id".to_string(), - value: d.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }, - )); - } - }, - None => None, - }; - let referrer_member_id = match req.referrer_member_id { - Some(d) => match uuid::Uuid::from_str(d.as_str()) { - Ok(dd) => Some(dd), - Err(e) => { - return Err(bcr::error::rpc::Error::InvalidParams( - bcr::error::rpc::InvalidParams { - message: "invalid referrer_member_id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "referrer_member_id".to_string(), - value: d.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }, - )); - } - }, - None => None, + let ( + site_id, + member_class_id, + member_level_id, + referrer_member_id, + username_like, + nickname_like, + mobile_phone_number_like, + last_signined_ip, + state, + deleted_at, + ) = match request.search { + Some(s) => { + let site_id = match s.site_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid site_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "site_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + let member_class_id = match s.member_class_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_class_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_class_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + let member_level_id = match s.member_level_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_level_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_level_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + let referrer_member_id = match s.referrer_member_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid referrer_member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "referrer_member_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + + ( + site_id, + member_class_id, + member_level_id, + referrer_member_id, + s.username_like, + s.nickname_like, + s.mobile_phone_number_like, + s.last_signined_ip, + s.state.map(repositories::member::schema::MemberState::from), + s.deleted_at.map(|d| d > 0), + ) + } + None => (None, None, None, None, None, None, None, None, None, None), }; let conn = self.pool.get().map_err(|e| { @@ -473,24 +538,24 @@ impl Service<'_> { })?; let find_all = repositories::member::models::FindAll { - site_id, - member_class_id, - member_level_id, - referrer_member_id, - username_like: req.username_like, - nickname_like: req.nickname_like, - mobile_phone_number_like: req.mobile_phone_number_like, - last_signined_ip: req.last_signined_ip, - state: req - .state - .map(repositories::member::schema::MemberState::from), - deleted_at: req.deleted_at.map(|d| d > 0), - pagination: req + search: Some(repositories::member::models::FindAllSearch { + site_id, + member_class_id, + member_level_id, + referrer_member_id, + username_like, + nickname_like, + mobile_phone_number_like, + last_signined_ip, + state, + deleted_at, + }), + pagination: request .pagination .as_ref() .map(bcr::models::pagination::Pagination::from), sorts: Some( - req + request .sorts .iter() .map(beteran_common_rust::models::pagination::Sort::from) @@ -522,9 +587,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::ListMembersResponse { + bpr::ss::member::ListMembersResponse { error: None, - result: Some(bpr::ss::member::member::list_members_response::Result { + result: Some(bpr::ss::member::list_members_response::Result { members: member_list .iter() .map(bpr::models::member::MemberModel::from) @@ -548,7 +613,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::ListMembersResponse { + bpr::ss::member::ListMembersResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -565,33 +630,20 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_GET_MEMBER, + bpr::ss::member::SUBJECT_GET_MEMBER, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::member::GetMemberRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::member::GetMemberRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -609,6 +661,35 @@ impl Service<'_> { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -628,9 +709,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::GetMemberResponse { + bpr::ss::member::GetMemberResponse { error: None, - result: Some(bpr::ss::member::member::get_member_response::Result { + result: Some(bpr::ss::member::get_member_response::Result { member: m.map(|d| bpr::models::member::MemberModel::from(&d)), }), } @@ -651,7 +732,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::GetMemberResponse { + bpr::ss::member::GetMemberResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -668,20 +749,19 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_GET_MEMBER_BY_USERNAME, + bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member::GetMemberByUsernameRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; + let req = bpr::ss::member::GetMemberByUsernameRequest::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, @@ -700,7 +780,23 @@ impl Service<'_> { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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, @@ -711,7 +807,7 @@ impl Service<'_> { let m = self .member_composition - .select_by_username(&conn, req.username.as_str()) + .select_by_username(&conn, request.username.as_str()) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -722,13 +818,11 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::GetMemberByUsernameResponse { + bpr::ss::member::GetMemberByUsernameResponse { error: None, - result: Some( - bpr::ss::member::member::get_member_by_username_response::Result { - member: m.map(|d| bpr::models::member::MemberModel::from(&d)), - }, - ), + result: Some(bpr::ss::member::get_member_by_username_response::Result { + member: m.map(|d| bpr::models::member::MemberModel::from(&d)), + }), } .encode_to_vec(), ) @@ -747,7 +841,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::GetMemberByUsernameResponse { + bpr::ss::member::GetMemberByUsernameResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -764,34 +858,69 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_UPDATE_MEMBER, + bpr::ss::member::SUBJECT_UPDATE_MEMBER, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::member::UpdateMemberRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::member::UpdateMemberRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "id".to_string(), - value: req.id.clone(), + value: request.id.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }) })?; - let site_id = match req.site_id { + let site_id = match request.site_id { Some(d) => match uuid::Uuid::from_str(d.as_str()) { Ok(dd) => Some(dd), Err(e) => { @@ -812,7 +941,7 @@ impl Service<'_> { None => None, }; - let member_level_id = match req.member_level_id { + let member_level_id = match request.member_level_id { Some(d) => match uuid::Uuid::from_str(d.as_str()) { Ok(dd) => Some(dd), Err(e) => { @@ -833,24 +962,6 @@ impl Service<'_> { None => None, }; - 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, @@ -862,11 +973,8 @@ impl Service<'_> { let modify_member = repositories::member::models::ModifyMember { site_id, member_level_id, - password: req.password, - mobile_phone_number: req.mobile_phone_number, - state: req - .state - .map(repositories::member::schema::MemberState::from), + password: request.password, + mobile_phone_number: request.mobile_phone_number, }; let _affected = self @@ -890,9 +998,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::UpdateMemberResponse { + bpr::ss::member::UpdateMemberResponse { error: None, - result: Some(bpr::ss::member::member::update_member_response::Result { + result: Some(bpr::ss::member::update_member_response::Result { member: m.map(|d| bpr::models::member::MemberModel::from(&d)), }), } @@ -913,7 +1021,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::UpdateMemberResponse { + bpr::ss::member::UpdateMemberResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -926,37 +1034,24 @@ impl Service<'_> { Ok(()) } - async fn delete_member(&self) -> Result<(), Box> { + async fn update_member_for_state(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member::SUBJECT_DELETE_MEMBER, + bpr::ss::member::SUBJECT_UPDATE_MEMBER_FOR_STATE, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::member::member::DeleteMemberRequest::decode(message.data.as_slice()) + let req = bpr::ss::member::UpdateMemberForStateRequest::decode(message.data.as_slice()) .map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -974,6 +1069,170 @@ impl Service<'_> { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_for_state = repositories::member::models::ModifyMemberForState { + state: repositories::member::schema::MemberState::from(request.state), + }; + + let _affected = self + .member_repository + .update_state(&conn, id, &modify_member_for_state) + .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 m = self.member_composition.select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member::UpdateMemberForStateResponse { + error: None, + result: Some(bpr::ss::member::update_member_for_state_response::Result { + member: m.map(|d| bpr::models::member::MemberModel::from(&d)), + }), + } + .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::UpdateMemberForStateResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::SUBJECT_DELETE_MEMBER, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::member::DeleteMemberRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -1002,9 +1261,9 @@ impl Service<'_> { message .respond( - bpr::ss::member::member::DeleteMemberResponse { + bpr::ss::member::DeleteMemberResponse { error: None, - result: Some(bpr::ss::member::member::delete_member_response::Result {}), + result: Some(bpr::ss::member::delete_member_response::Result {}), } .encode_to_vec(), ) @@ -1023,7 +1282,7 @@ impl Service<'_> { { message .respond( - bpr::ss::member::member::DeleteMemberResponse { + bpr::ss::member::DeleteMemberResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -1040,21 +1299,21 @@ impl Service<'_> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::identity::EVENT_SUBJECT_AFTER_SIGNIN, + bpr::ss::identity::EVENT_SUBJECT_AFTER_SIGNIN, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let event = bpr::ss::member::identity::AfterSigninEvent::decode(message.data.as_slice()) - .map_err(|e| { + let eve = + bpr::ss::identity::AfterSigninEvent::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 event.client { + let client = match eve.client { Some(c) => c, None => { return Err(bcr::error::rpc::Error::InvalidParams( @@ -1071,7 +1330,23 @@ impl Service<'_> { )); } }; - + let event = match eve.event { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid event information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "event".to_string(), + param: "event".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; let member = match event.member { Some(c) => c, None => { diff --git a/src/services/member_bank_account/mod.rs b/src/services/member_bank_account/mod.rs new file mode 100644 index 0000000..b9e52db --- /dev/null +++ b/src/services/member_bank_account/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod service; diff --git a/src/services/member_bank_account/models.rs b/src/services/member_bank_account/models.rs new file mode 100644 index 0000000..6e83660 --- /dev/null +++ b/src/services/member_bank_account/models.rs @@ -0,0 +1,19 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; + +impl From<&repositories::member_bank_account::models::MemberBankAccount> + for bpr::models::member_bank_account::MemberBankAccount +{ + fn from(d: &repositories::member_bank_account::models::MemberBankAccount) -> Self { + bpr::models::member_bank_account::MemberBankAccount { + id: d.id.to_string(), + member_id: d.member_id.to_string(), + bank_id: d.bank_id.to_string(), + name: d.name.clone(), + account_number: d.account_number.to_string(), + memo: d.memo.clone(), + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} diff --git a/src/services/member_bank_account/service.rs b/src/services/member_bank_account/service.rs new file mode 100644 index 0000000..a2f4dec --- /dev/null +++ b/src/services/member_bank_account/service.rs @@ -0,0 +1,850 @@ +//! +//! + +use std::str::FromStr; + +use super::models; +use crate::compositions; +use crate::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>, + site_repository: repositories::site::repository::Repository, + site_composition: compositions::site::composition::Composition, + member_bank_account_repository: repositories::member_bank_account::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>, + ) -> Service { + Service { + connection_broker, + queue_broker, + pool, + site_repository: repositories::site::repository::Repository::new(), + site_composition: compositions::site::composition::Composition::new(), + member_bank_account_repository: + repositories::member_bank_account::repository::Repository::new(), + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.create_member_bank_account(), + self.list_member_bank_accounts(), + self.get_member_bank_account(), + self.update_member_bank_account(), + self.delete_member_bank_account(), + ) + .map(|_| ()) + } + fn check_site( + &self, + conn: &diesel::PgConnection, + url: Option, + site_id: uuid::Uuid, + ) -> Result { + 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 create_member_bank_account(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_account::SUBJECT_CREATE_MEMBER_BANK_ACCOUNT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_account::CreateMemberBankAccountRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let member_id = uuid::Uuid::from_str(request.member_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: request.member_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; + let bank_id = uuid::Uuid::from_str(request.bank_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid bank_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "bank_id".to_string(), + value: request.bank_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_bank_account_repository + .insert( + &conn, + &repositories::member_bank_account::models::NewMemberBankAccount { + member_id, + bank_id, + name: request.name, + account_number: request.account_number, + memo: request.memo, + }, + ) + .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_bank_account::CreateMemberBankAccountResponse { + error: None, + result: Some( + bpr::ss::member_bank_account::create_member_bank_account_response::Result { + member_bank_account: Some( + bpr::models::member_bank_account::MemberBankAccount::from(&s), + ), + }, + ), + } + .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_bank_account::CreateMemberBankAccountResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_bank_accounts(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_account::SUBJECT_LIST_MEMBER_BANK_ACCOUNTS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_account::ListMemberBankAccountsRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let search = match request.search { + Some(s) => { + let member_id = match s.member_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + let bank_id = match s.bank_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid bank_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "bank_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + + Some(repositories::member_bank_account::models::FindAllSearch { + member_id, + bank_id, + name_like: s.name_like, + account_number_like: s.account_number_like, + memo_like: s.memo_like, + }) + } + None => None, + }; + + 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_bank_account::models::FindAll { + search, + pagination: request + .pagination + .as_ref() + .map(|d| bcr::models::pagination::Pagination::from(d)), + sorts: Some( + request + .sorts + .iter() + .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) + .collect(), + ), + }; + + let count = self + .member_bank_account_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 list = self + .member_bank_account_repository + .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_bank_account::ListMemberBankAccountsResponse { + error: None, + result: Some( + bpr::ss::member_bank_account::list_member_bank_accounts_response::Result { + member_bank_accounts: list + .iter() + .map(|d| bpr::models::member_bank_account::MemberBankAccount::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_bank_account::ListMemberBankAccountsResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_bank_account(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_account::SUBJECT_GET_MEMBER_BANK_ACCOUNT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_account::GetMemberBankAccountRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_bank_account_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_account::GetMemberBankAccountResponse { + error: None, + result: Some( + bpr::ss::member_bank_account::get_member_bank_account_response::Result { + member_bank_account: s + .map(|d| bpr::models::member_bank_account::MemberBankAccount::from(&d)), + }, + ), + } + .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_bank_account::GetMemberBankAccountResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_bank_account(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_account::SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_account::UpdateMemberBankAccountRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; + let bank_id = uuid::Uuid::from_str(request.bank_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "bank_id".to_string(), + value: request.bank_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 _affected = self + .member_bank_account_repository + .update( + &conn, + id, + &repositories::member_bank_account::models::ModifyMemberBankAccount { + name: request.name, + memo: request.memo, + bank_id, + account_number: request.account_number, + }, + ) + .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 s = self + .member_bank_account_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_account::UpdateMemberBankAccountResponse { + error: None, + result: Some( + bpr::ss::member_bank_account::update_member_bank_account_response::Result { + member_bank_account: s + .map(|d| bpr::models::member_bank_account::MemberBankAccount::from(&d)), + }, + ), + } + .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_bank_account::UpdateMemberBankAccountResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_bank_account(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_account::SUBJECT_DELETE_MEMBER_BANK_ACCOUNT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_account::DeleteMemberBankAccountRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 _affected = self + .member_bank_account_repository + .delete(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_account::DeleteMemberBankAccountResponse { + error: None, + result: Some( + bpr::ss::member_bank_account::delete_member_bank_account_response::Result {}, + ), + } + .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_bank_account::DeleteMemberBankAccountResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/services/member_bank_deposit/mod.rs b/src/services/member_bank_deposit/mod.rs new file mode 100644 index 0000000..b9e52db --- /dev/null +++ b/src/services/member_bank_deposit/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod service; diff --git a/src/services/member_bank_deposit/models.rs b/src/services/member_bank_deposit/models.rs new file mode 100644 index 0000000..83381d8 --- /dev/null +++ b/src/services/member_bank_deposit/models.rs @@ -0,0 +1,33 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; + +impl From<&repositories::member_bank_deposit::models::MemberBankDeposit> + for bpr::models::member_bank_deposit::MemberBankDeposit +{ + fn from(d: &repositories::member_bank_deposit::models::MemberBankDeposit) -> Self { + bpr::models::member_bank_deposit::MemberBankDeposit { + id: d.id.to_string(), + member_id: d.member_id.to_string(), + name: d.name.clone(), + amount: d.amount as u32, + memo: d.memo.clone(), + state: d.state as i32, + state_changed_at: d.state_changed_at as u64, + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} + +impl From for repositories::member_bank_deposit::schema::MemberBankDepositState { + fn from(s: i32) -> Self { + match s { + s if s + == repositories::member_bank_deposit::schema::MemberBankDepositState::Complete as i32 => + { + repositories::member_bank_deposit::schema::MemberBankDepositState::Complete + } + _ => repositories::member_bank_deposit::schema::MemberBankDepositState::Application, + } + } +} diff --git a/src/services/member_bank_deposit/service.rs b/src/services/member_bank_deposit/service.rs new file mode 100644 index 0000000..2c38708 --- /dev/null +++ b/src/services/member_bank_deposit/service.rs @@ -0,0 +1,946 @@ +//! +//! + +use std::str::FromStr; + +use super::models; +use crate::compositions; +use crate::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>, + site_repository: repositories::site::repository::Repository, + site_composition: compositions::site::composition::Composition, + member_bank_deposit_repository: repositories::member_bank_deposit::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>, + ) -> Service { + Service { + connection_broker, + queue_broker, + pool, + site_repository: repositories::site::repository::Repository::new(), + site_composition: compositions::site::composition::Composition::new(), + member_bank_deposit_repository: + repositories::member_bank_deposit::repository::Repository::new(), + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.create_member_bank_deposit(), + self.list_member_bank_deposits(), + self.get_member_bank_deposit(), + self.update_member_bank_deposit(), + self.update_member_bank_deposit_for_state(), + self.delete_member_bank_deposit(), + ) + .map(|_| ()) + } + fn check_site( + &self, + conn: &diesel::PgConnection, + url: Option, + site_id: uuid::Uuid, + ) -> Result { + 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 create_member_bank_deposit(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_CREATE_MEMBER_BANK_DEPOSIT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::CreateMemberBankDepositRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let member_id = uuid::Uuid::from_str(request.member_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: request.member_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_bank_deposit_repository + .insert( + &conn, + &repositories::member_bank_deposit::models::NewMemberBankDeposit { + member_id, + name: request.name, + amount: request.amount as i32, + memo: request.memo, + }, + ) + .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_bank_deposit::CreateMemberBankDepositResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::create_member_bank_deposit_response::Result { + member_bank_deposit: Some( + bpr::models::member_bank_deposit::MemberBankDeposit::from(&s), + ), + }, + ), + } + .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_bank_deposit::CreateMemberBankDepositResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_bank_deposits(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_LIST_MEMBER_BANK_DEPOSITS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::ListMemberBankDepositsRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let search = match request.search { + Some(s) => { + let member_id = match s.member_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + + Some(repositories::member_bank_deposit::models::FindAllSearch { + member_id, + name_like: s.name_like, + amount: s.amount.map(|d| d as i32), + memo_like: s.memo_like, + state: s.state.map(|d| { + repositories::member_bank_deposit::schema::MemberBankDepositState::from(d) + }), + }) + } + None => None, + }; + + 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_bank_deposit::models::FindAll { + search, + pagination: request + .pagination + .as_ref() + .map(|d| bcr::models::pagination::Pagination::from(d)), + sorts: Some( + request + .sorts + .iter() + .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) + .collect(), + ), + }; + + let count = self + .member_bank_deposit_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 list = self + .member_bank_deposit_repository + .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_bank_deposit::ListMemberBankDepositsResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::list_member_bank_deposits_response::Result { + member_bank_deposits: list + .iter() + .map(|d| bpr::models::member_bank_deposit::MemberBankDeposit::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_bank_deposit::ListMemberBankDepositsResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_bank_deposit(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_GET_MEMBER_BANK_DEPOSIT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::GetMemberBankDepositRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_bank_deposit_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_deposit::GetMemberBankDepositResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::get_member_bank_deposit_response::Result { + member_bank_deposit: s + .map(|d| bpr::models::member_bank_deposit::MemberBankDeposit::from(&d)), + }, + ), + } + .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_bank_deposit::GetMemberBankDepositResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_bank_deposit(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::UpdateMemberBankDepositRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_bank_deposit = + repositories::member_bank_deposit::models::ModifyMemberBankDeposit { + name: request.name, + amount: request.amount as i32, + memo: request.memo, + }; + + let _affected = self + .member_bank_deposit_repository + .update(&conn, id, &modify_member_bank_deposit) + .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 s = self + .member_bank_deposit_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_deposit::UpdateMemberBankDepositResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::update_member_bank_deposit_response::Result { + member_bank_deposit: s + .map(|d| bpr::models::member_bank_deposit::MemberBankDeposit::from(&d)), + }, + ), + } + .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_bank_deposit::UpdateMemberBankDepositResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_bank_deposit_for_state(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT_FOR_STATE, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::UpdateMemberBankDepositForStateRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_bank_deposit_for_state = + repositories::member_bank_deposit::models::ModifyMemberBankDepositForState { + state: repositories::member_bank_deposit::schema::MemberBankDepositState::from( + request.state, + ), + }; + + let _affected = self + .member_bank_deposit_repository + .update_state(&conn, id, &modify_member_bank_deposit_for_state) + .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 s = self + .member_bank_deposit_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_deposit::UpdateMemberBankDepositResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::update_member_bank_deposit_response::Result { + member_bank_deposit: s + .map(|d| bpr::models::member_bank_deposit::MemberBankDeposit::from(&d)), + }, + ), + } + .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_bank_deposit::UpdateMemberBankDepositResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_bank_deposit(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_deposit::SUBJECT_DELETE_MEMBER_BANK_DEPOSIT, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_deposit::DeleteMemberBankDepositRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 _affected = self + .member_bank_deposit_repository + .delete(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_deposit::DeleteMemberBankDepositResponse { + error: None, + result: Some( + bpr::ss::member_bank_deposit::delete_member_bank_deposit_response::Result {}, + ), + } + .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_bank_deposit::DeleteMemberBankDepositResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/services/member_bank_withdraw/mod.rs b/src/services/member_bank_withdraw/mod.rs new file mode 100644 index 0000000..b9e52db --- /dev/null +++ b/src/services/member_bank_withdraw/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod service; diff --git a/src/services/member_bank_withdraw/models.rs b/src/services/member_bank_withdraw/models.rs new file mode 100644 index 0000000..b80e5e6 --- /dev/null +++ b/src/services/member_bank_withdraw/models.rs @@ -0,0 +1,36 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; + +impl From<&repositories::member_bank_withdraw::models::MemberBankWithdraw> + for bpr::models::member_bank_withdraw::MemberBankWithdraw +{ + fn from(d: &repositories::member_bank_withdraw::models::MemberBankWithdraw) -> Self { + bpr::models::member_bank_withdraw::MemberBankWithdraw { + id: d.id.to_string(), + member_id: d.member_id.to_string(), + bank_name: d.bank_name.clone(), + account_number: d.account_number.clone(), + password: d.password.clone(), + name: d.name.clone(), + amount: d.amount as u32, + memo: d.memo.clone(), + state: d.state as i32, + state_changed_at: d.state_changed_at as u64, + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} + +impl From for repositories::member_bank_withdraw::schema::MemberBankWithdrawState { + fn from(s: i32) -> Self { + match s { + s if s + == repositories::member_bank_withdraw::schema::MemberBankWithdrawState::Complete as i32 => + { + repositories::member_bank_withdraw::schema::MemberBankWithdrawState::Complete + } + _ => repositories::member_bank_withdraw::schema::MemberBankWithdrawState::Application, + } + } +} diff --git a/src/services/member_bank_withdraw/service.rs b/src/services/member_bank_withdraw/service.rs new file mode 100644 index 0000000..c0d02bf --- /dev/null +++ b/src/services/member_bank_withdraw/service.rs @@ -0,0 +1,952 @@ +//! +//! + +use std::str::FromStr; + +use super::models; +use crate::compositions; +use crate::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>, + site_repository: repositories::site::repository::Repository, + site_composition: compositions::site::composition::Composition, + member_bank_withdraw_repository: repositories::member_bank_withdraw::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>, + ) -> Service { + Service { + connection_broker, + queue_broker, + pool, + site_repository: repositories::site::repository::Repository::new(), + site_composition: compositions::site::composition::Composition::new(), + member_bank_withdraw_repository: + repositories::member_bank_withdraw::repository::Repository::new(), + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.create_member_bank_withdraw(), + self.list_member_bank_withdraws(), + self.get_member_bank_withdraw(), + self.update_member_bank_withdraw(), + self.update_member_bank_withdraw_for_state(), + self.delete_member_bank_withdraw(), + ) + .map(|_| ()) + } + fn check_site( + &self, + conn: &diesel::PgConnection, + url: Option, + site_id: uuid::Uuid, + ) -> Result { + 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 create_member_bank_withdraw(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_CREATE_MEMBER_BANK_WITHDRAW, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::CreateMemberBankWithdrawRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let member_id = uuid::Uuid::from_str(request.member_id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: request.member_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 new_member_bank_withdraw = + repositories::member_bank_withdraw::models::NewMemberBankWithdraw { + member_id, + bank_name: request.bank_name, + name: request.name, + account_number: request.account_number, + amount: request.amount as i32, + password: request.password, + memo: request.memo, + }; + + let s = self + .member_bank_withdraw_repository + .insert(&conn, &new_member_bank_withdraw) + .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_bank_withdraw::CreateMemberBankWithdrawResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::create_member_bank_withdraw_response::Result { + member_bank_withdraw: Some( + bpr::models::member_bank_withdraw::MemberBankWithdraw::from(&s), + ), + }, + ), + } + .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_bank_withdraw::CreateMemberBankWithdrawResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_bank_withdraws(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_LIST_MEMBER_BANK_WITHDRAWS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::ListMemberBankWithdrawsRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let search = match request.search { + Some(s) => { + let member_id = match s.member_id { + Some(d) => match uuid::Uuid::from_str(d.as_str()) { + Ok(dd) => Some(dd), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_id".to_string(), + value: d.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + None => None, + }; + + Some(repositories::member_bank_withdraw::models::FindAllSearch { + member_id, + bank_name_like: s.bank_name_like, + name_like: s.name_like, + account_number_like: s.account_number_like, + amount: s.amount.map(|d| d as i32), + memo_like: s.memo_like, + state: s.state.map(|d| { + repositories::member_bank_withdraw::schema::MemberBankWithdrawState::from(d) + }), + }) + } + None => None, + }; + + 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_bank_withdraw::models::FindAll { + search, + pagination: request + .pagination + .as_ref() + .map(|d| bcr::models::pagination::Pagination::from(d)), + sorts: Some( + request + .sorts + .iter() + .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) + .collect(), + ), + }; + + let count = self + .member_bank_withdraw_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 list = self + .member_bank_withdraw_repository + .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_bank_withdraw::ListMemberBankWithdrawsResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::list_member_bank_withdraws_response::Result { + member_bank_withdraws: list + .iter() + .map(|d| bpr::models::member_bank_withdraw::MemberBankWithdraw::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_bank_withdraw::ListMemberBankWithdrawsResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_bank_withdraw(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_GET_MEMBER_BANK_WITHDRAW, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::GetMemberBankWithdrawRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_bank_withdraw_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_withdraw::GetMemberBankWithdrawResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::get_member_bank_withdraw_response::Result { + member_bank_withdraw: s + .map(|d| bpr::models::member_bank_withdraw::MemberBankWithdraw::from(&d)), + }, + ), + } + .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_bank_withdraw::GetMemberBankWithdrawResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_bank_withdraw(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_UPDATE_MEMBER_BANK_WITHDRAW, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_bank_withdraw = + repositories::member_bank_withdraw::models::ModifyMemberBankWithdraw { + bank_name: request.bank_name, + name: request.name, + account_number: request.account_number, + amount: request.amount as i32, + password: request.password, + memo: request.memo, + }; + + let _affected = self + .member_bank_withdraw_repository + .update(&conn, id, &modify_member_bank_withdraw) + .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 s = self + .member_bank_withdraw_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::update_member_bank_withdraw_response::Result { + member_bank_withdraw: s + .map(|d| bpr::models::member_bank_withdraw::MemberBankWithdraw::from(&d)), + }, + ), + } + .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_bank_withdraw::UpdateMemberBankWithdrawResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_bank_withdraw_for_state(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_UPDATE_MEMBER_BANK_WITHDRAW_FOR_STATE, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawForStateRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_bank_withdraw_for_state = + repositories::member_bank_withdraw::models::ModifyMemberBankWithdrawForState { + state: repositories::member_bank_withdraw::schema::MemberBankWithdrawState::from( + request.state, + ), + }; + + let _affected = self + .member_bank_withdraw_repository + .update_state(&conn, id, &modify_member_bank_withdraw_for_state) + .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 s = self + .member_bank_withdraw_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawForStateResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::update_member_bank_withdraw_for_state_response::Result { + member_bank_withdraw: s + .map(|d| bpr::models::member_bank_withdraw::MemberBankWithdraw::from(&d)), + }, + ), + } + .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_bank_withdraw::UpdateMemberBankWithdrawResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_bank_withdraw(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_bank_withdraw::SUBJECT_DELETE_MEMBER_BANK_WITHDRAW, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_bank_withdraw::DeleteMemberBankWithdrawRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 _affected = self + .member_bank_withdraw_repository + .delete(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_bank_withdraw::DeleteMemberBankWithdrawResponse { + error: None, + result: Some( + bpr::ss::member_bank_withdraw::delete_member_bank_withdraw_response::Result {}, + ), + } + .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_bank_withdraw::DeleteMemberBankWithdrawResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/services/member_class/models.rs b/src/services/member_class/models.rs index 8b13789..521a484 100644 --- a/src/services/member_class/models.rs +++ b/src/services/member_class/models.rs @@ -1 +1,17 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; +impl From<&repositories::member_class::models::MemberClass> + for bpr::models::member_class::MemberClass +{ + fn from(d: &repositories::member_class::models::MemberClass) -> Self { + bpr::models::member_class::MemberClass { + id: d.id.to_string(), + parent_id: d.parent_id.map(|d| d.to_string()), + name: d.name.clone(), + show: d.show, + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} diff --git a/src/services/member_class/service.rs b/src/services/member_class/service.rs index 80e7479..deccd45 100644 --- a/src/services/member_class/service.rs +++ b/src/services/member_class/service.rs @@ -48,8 +48,9 @@ impl Service { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( - self.list_member_classes(), self.create_member_class(), + self.list_member_classes(), + self.get_member_class(), self.update_member_class(), self.delete_member_class(), ) @@ -92,22 +93,54 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_class::SUBJECT_CREATE_MEMBER_CLASS, + bpr::ss::member_class::SUBJECT_CREATE_MEMBER_CLASS, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_class::CreateMemberClassRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let parent_id = match req.parent_id { + let req = bpr::ss::member_class::CreateMemberClassRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let parent_id = match request.parent_id { Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) { Ok(parent_id) => Some(parent_id), Err(e) => { @@ -128,24 +161,6 @@ impl Service { None => None, }; - 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, @@ -154,15 +169,15 @@ impl Service { }) })?; + let new_member_class = repositories::member_class::models::NewMemberClass { + parent_id, + name: request.name, + show: request.show, + }; + let s = self .member_class_repository - .insert( - &conn, - &repositories::member_class::models::NewMemberClass { - parent_id, - name: req.name, - }, - ) + .insert(&conn, &new_member_class) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -173,11 +188,11 @@ impl Service { message .respond( - bpr::ss::member::member_class::CreateMemberClassResponse { + bpr::ss::member_class::CreateMemberClassResponse { error: None, result: Some( - bpr::ss::member::member_class::create_member_class_response::Result { - member_class: Some(bpr::models::member::MemberClass::from(&s)), + bpr::ss::member_class::create_member_class_response::Result { + member_class: Some(bpr::models::member_class::MemberClass::from(&s)), }, ), } @@ -198,7 +213,7 @@ impl Service { { message .respond( - bpr::ss::member::member_class::CreateMemberClassResponse { + bpr::ss::member_class::CreateMemberClassResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -215,20 +230,19 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_class::SUBJECT_LIST_MEMBER_CLASSES, + bpr::ss::member_class::SUBJECT_LIST_MEMBER_CLASSES, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_class::ListMemberClassesRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; + let req = bpr::ss::member_class::ListMemberClassesRequest::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, @@ -247,6 +261,30 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let search = match request.search { + Some(s) => Some(repositories::member_class::models::FindAllSearch { + name_like: s.name_like, + show: s.show, + }), + None => None, + }; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -257,13 +295,13 @@ impl Service { })?; let find_all = repositories::member_class::models::FindAll { - name_like: None, - pagination: req + search, + pagination: request .pagination .as_ref() .map(|d| bcr::models::pagination::Pagination::from(d)), sorts: Some( - req + request .sorts .iter() .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) @@ -295,13 +333,13 @@ impl Service { message .respond( - bpr::ss::member::member_class::ListMemberClassesResponse { + bpr::ss::member_class::ListMemberClassesResponse { error: None, result: Some( - bpr::ss::member::member_class::list_member_classes_response::Result { + bpr::ss::member_class::list_member_classes_response::Result { member_classes: list .iter() - .map(|d| bpr::models::member::MemberClass::from(d)) + .map(|d| bpr::models::member_class::MemberClass::from(d)) .collect(), }, ), @@ -323,7 +361,128 @@ impl Service { { message .respond( - bpr::ss::member::member_class::ListMemberClassesResponse { + bpr::ss::member_class::ListMemberClassesResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_class(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_class::SUBJECT_GET_MEMBER_CLASS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_class::GetMemberClassRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 s = self + .member_class_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_class::GetMemberClassResponse { + error: None, + result: Some(bpr::ss::member_class::get_member_class_response::Result { + member_class: s.map(|d| bpr::models::member_class::MemberClass::from(&d)), + }), + } + .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_class::GetMemberClassResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -340,35 +499,67 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_class::SUBJECT_UPDATE_MEMBER_CLASS, + bpr::ss::member_class::SUBJECT_UPDATE_MEMBER_CLASS, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_class::UpdateMemberClassRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { + let req = bpr::ss::member_class::UpdateMemberClassRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "id".to_string(), - value: req.id.clone(), + value: request.id.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }) })?; - let parent_id = match req.parent_id { + let parent_id = match request.parent_id { Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) { Ok(parent_id) => Some(parent_id), Err(e) => { @@ -389,24 +580,6 @@ impl Service { None => None, }; - 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, @@ -422,7 +595,8 @@ impl Service { id, &repositories::member_class::models::ModifyMemberClass { parent_id, - name: req.name, + name: request.name, + show: request.show, }, ) .map_err(|e| { @@ -446,11 +620,11 @@ impl Service { message .respond( - bpr::ss::member::member_class::UpdateMemberClassResponse { + bpr::ss::member_class::UpdateMemberClassResponse { error: None, result: Some( - bpr::ss::member::member_class::update_member_class_response::Result { - member_class: Some(bpr::models::member::MemberClass::from(&s)), + bpr::ss::member_class::update_member_class_response::Result { + member_class: s.map(|d| bpr::models::member_class::MemberClass::from(&d)), }, ), } @@ -471,7 +645,7 @@ impl Service { { message .respond( - bpr::ss::member::member_class::UpdateMemberClassResponse { + bpr::ss::member_class::UpdateMemberClassResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -488,34 +662,19 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_class::SUBJECT_DELETE_MEMBER_CLASS, + bpr::ss::member_class::SUBJECT_DELETE_MEMBER_CLASS, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_class::DeleteMemberClassRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - + let req = bpr::ss::member_class::DeleteMemberClassRequest::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 => { @@ -533,6 +692,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -555,9 +743,9 @@ impl Service { message .respond( - bpr::ss::member::member_class::DeleteMemberClassResponse { + bpr::ss::member_class::DeleteMemberClassResponse { error: None, - result: Some(bpr::ss::member::member_class::delete_member_class_response::Result {}), + result: Some(bpr::ss::member_class::delete_member_class_response::Result {}), } .encode_to_vec(), ) @@ -576,7 +764,7 @@ impl Service { { message .respond( - bpr::ss::member::member_class::DeleteMemberClassResponse { + bpr::ss::member_class::DeleteMemberClassResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } diff --git a/src/services/member_level/models.rs b/src/services/member_level/models.rs index 8b13789..27f3df6 100644 --- a/src/services/member_level/models.rs +++ b/src/services/member_level/models.rs @@ -1 +1,17 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; +impl From<&repositories::member_level::models::MemberLevel> + for bpr::models::member_level::MemberLevel +{ + fn from(d: &repositories::member_level::models::MemberLevel) -> Self { + bpr::models::member_level::MemberLevel { + id: d.id.to_string(), + name: d.name.clone(), + sort_order: d.sort_order as u32, + show: d.show, + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} diff --git a/src/services/member_level/service.rs b/src/services/member_level/service.rs index 8f43986..83617e7 100644 --- a/src/services/member_level/service.rs +++ b/src/services/member_level/service.rs @@ -48,8 +48,9 @@ impl Service { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( - self.list_member_levels(), self.create_member_level(), + self.list_member_levels(), + self.get_member_level(), self.update_member_level(), self.delete_member_level(), ) @@ -92,118 +93,15 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_level::SUBJECT_CREATE_MEMBER_LEVEL, + bpr::ss::member_level::SUBJECT_CREATE_MEMBER_LEVEL, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_level::CreateMemberLevelRequest::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 s = self - .member_level_repository - .insert( - &conn, - &repositories::member_level::models::NewMemberLevel { - name: req.name, - sort_order: req.sort_order as i32, - }, - ) + let req = bpr::ss::member_level::CreateMemberLevelRequest::decode(message.data.as_slice()) .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_level::CreateMemberLevelResponse { - error: None, - result: Some( - bpr::ss::member::member_level::create_member_level_response::Result { - member_level: Some(bpr::models::member::MemberLevel::from(&s)), - }, - ), - } - .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_level::CreateMemberLevelResponse { - error: Some(bpr::protobuf::rpc::Error::from(e)), - result: None, - } - .encode_to_vec(), - ) - .await?; - } - } - - Ok(()) - } - - async fn list_member_levels(&self) -> Result<(), Box> { - let s = self - .connection_broker - .queue_subscribe( - bpr::ss::member::member_level::SUBJECT_LIST_MEMBER_LEVELS, - self.queue_broker.as_str(), - ) - .await?; - - while let Some(message) = s.next().await { - if let Err(e) = async { - let req = - bpr::ss::member::member_level::ListMemberLevelsRequest::decode(message.data.as_slice()) - .map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -226,6 +124,146 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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 new_member_level = repositories::member_level::models::NewMemberLevel { + name: request.name, + sort_order: request.sort_order as i32, + show: request.show, + }; + let s = self + .member_level_repository + .insert(&conn, &new_member_level) + .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_level::CreateMemberLevelResponse { + error: None, + result: Some( + bpr::ss::member_level::create_member_level_response::Result { + member_level: Some(bpr::models::member_level::MemberLevel::from(&s)), + }, + ), + } + .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_level::CreateMemberLevelResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_levels(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_level::SUBJECT_LIST_MEMBER_LEVELS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_level::ListMemberLevelsRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let search = match request.search { + Some(s) => Some(repositories::member_level::models::FindAllSearch { + name_like: s.name_like, + show: s.show, + }), + None => None, + }; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -236,13 +274,13 @@ impl Service { })?; let find_all = repositories::member_level::models::FindAll { - name_like: None, - pagination: req + search, + pagination: request .pagination .as_ref() .map(|d| bcr::models::pagination::Pagination::from(d)), sorts: Some( - req + request .sorts .iter() .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) @@ -274,16 +312,14 @@ impl Service { message .respond( - bpr::ss::member::member_level::ListMemberLevelsResponse { + bpr::ss::member_level::ListMemberLevelsResponse { error: None, - result: Some( - bpr::ss::member::member_level::list_member_levels_response::Result { - member_levels: list - .iter() - .map(|d| bpr::models::member::MemberLevel::from(d)) - .collect(), - }, - ), + result: Some(bpr::ss::member_level::list_member_levels_response::Result { + member_levels: list + .iter() + .map(|d| bpr::models::member_level::MemberLevel::from(d)) + .collect(), + }), } .encode_to_vec(), ) @@ -302,7 +338,7 @@ impl Service { { message .respond( - bpr::ss::member::member_level::ListMemberLevelsResponse { + bpr::ss::member_level::ListMemberLevelsResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -315,38 +351,23 @@ impl Service { Ok(()) } - async fn update_member_level(&self) -> Result<(), Box> { + async fn get_member_level(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_level::SUBJECT_UPDATE_MEMBER_LEVEL, + bpr::ss::member_level::SUBJECT_GET_MEMBER_LEVEL, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_level::UpdateMemberLevelRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - + let req = bpr::ss::member_level::GetMemberLevelRequest::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 => { @@ -364,6 +385,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -373,16 +423,136 @@ impl Service { }) })?; + let s = self + .member_level_repository + .select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::member_level::GetMemberLevelResponse { + error: None, + result: Some(bpr::ss::member_level::get_member_level_response::Result { + member_level: s.map(|d| bpr::models::member_level::MemberLevel::from(&d)), + }), + } + .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_level::GetMemberLevelResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_level(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_level::SUBJECT_UPDATE_MEMBER_LEVEL, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::member_level::UpdateMemberLevelRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_member_level = repositories::member_level::models::ModifyMemberLevel { + name: request.name, + sort_order: request.sort_order as i32, + show: request.show, + }; + let _affected = self .member_level_repository - .update( - &conn, - id, - &repositories::member_level::models::ModifyMemberLevel { - name: req.name, - sort_order: req.sort_order as i32, - }, - ) + .update(&conn, id, &modify_member_level) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -404,11 +574,11 @@ impl Service { message .respond( - bpr::ss::member::member_level::UpdateMemberLevelResponse { + bpr::ss::member_level::UpdateMemberLevelResponse { error: None, result: Some( - bpr::ss::member::member_level::update_member_level_response::Result { - member_level: Some(bpr::models::member::MemberLevel::from(&s)), + bpr::ss::member_level::update_member_level_response::Result { + member_level: s.map(|d| bpr::models::member_level::MemberLevel::from(&d)), }, ), } @@ -429,7 +599,7 @@ impl Service { { message .respond( - bpr::ss::member::member_level::UpdateMemberLevelResponse { + bpr::ss::member_level::UpdateMemberLevelResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -446,34 +616,19 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::member::member_level::SUBJECT_DELETE_MEMBER_LEVEL, + bpr::ss::member_level::SUBJECT_DELETE_MEMBER_LEVEL, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = - bpr::ss::member::member_level::DeleteMemberLevelRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - + let req = bpr::ss::member_level::DeleteMemberLevelRequest::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 => { @@ -491,6 +646,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -513,9 +697,9 @@ impl Service { message .respond( - bpr::ss::member::member_level::DeleteMemberLevelResponse { + bpr::ss::member_level::DeleteMemberLevelResponse { error: None, - result: Some(bpr::ss::member::member_level::delete_member_level_response::Result {}), + result: Some(bpr::ss::member_level::delete_member_level_response::Result {}), } .encode_to_vec(), ) @@ -534,7 +718,7 @@ impl Service { { message .respond( - bpr::ss::member::member_level::DeleteMemberLevelResponse { + bpr::ss::member_level::DeleteMemberLevelResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } diff --git a/src/services/mod.rs b/src/services/mod.rs index 40bda1d..0d66f24 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,5 +1,9 @@ +pub mod bank; pub mod identity; pub mod member; +pub mod member_bank_account; +pub mod member_bank_deposit; +pub mod member_bank_withdraw; pub mod member_class; pub mod member_level; pub mod site; diff --git a/src/services/site/models.rs b/src/services/site/models.rs index b875dec..636ef07 100644 --- a/src/services/site/models.rs +++ b/src/services/site/models.rs @@ -1,9 +1,9 @@ use crate::repositories; use beteran_protobuf_rust as bpr; -impl From<&repositories::site::models::Site> for bpr::models::domain::Site { +impl From<&repositories::site::models::Site> for bpr::models::site::Site { fn from(d: &repositories::site::models::Site) -> Self { - bpr::models::domain::Site { + bpr::models::site::Site { id: d.id.to_string(), url: d.url.clone(), name: d.name.clone(), diff --git a/src/services/site/service.rs b/src/services/site/service.rs index 84d3af4..8c7618f 100644 --- a/src/services/site/service.rs +++ b/src/services/site/service.rs @@ -48,8 +48,9 @@ impl Service { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( - self.list_sites(), self.create_site(), + self.list_sites(), + self.get_site(), self.update_site(), self.delete_site(), ) @@ -92,15 +93,15 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_LIST_SITES, + bpr::ss::site::SUBJECT_LIST_SITES, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::ListSitesRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::site::ListSitesRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -123,7 +124,34 @@ impl Service { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let search = match request.search { + Some(s) => Some(repositories::site::models::FindAllSearch { + url_like: s.url_like, + name_like: s.name_like, + path_like: s.path_like, + show: s.show, + can_use: s.can_use, + memo_like: s.memo_like, + }), + None => None, + }; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -133,18 +161,13 @@ impl Service { })?; let find_all = repositories::site::models::FindAll { - url_like: None, - name_like: None, - path_like: None, - memo_like: None, - show: None, - can_use: None, - pagination: req + search, + pagination: request .pagination .as_ref() .map(|d| bcr::models::pagination::Pagination::from(d)), sorts: Some( - req + request .sorts .iter() .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) @@ -176,12 +199,12 @@ impl Service { message .respond( - bpr::ss::domain::site::ListSitesResponse { + bpr::ss::site::ListSitesResponse { error: None, - result: Some(bpr::ss::domain::site::list_sites_response::Result { + result: Some(bpr::ss::site::list_sites_response::Result { sites: list .iter() - .map(|d| bpr::models::domain::Site::from(d)) + .map(|d| bpr::models::site::Site::from(d)) .collect(), }), } @@ -202,7 +225,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::ListSitesResponse { + bpr::ss::site::ListSitesResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -219,15 +242,15 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_CREATE_SITE, + bpr::ss::site::SUBJECT_CREATE_SITE, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::CreateSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::site::CreateSiteRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) @@ -250,7 +273,23 @@ impl Service { )); } }; - + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".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, @@ -259,34 +298,30 @@ impl Service { }) })?; - let s = self - .site_repository - .insert( - &conn, - &repositories::site::models::NewSite { - url: req.url, - name: req.name, - path: req.path, - show: req.show, - can_use: req.can_use, - memo: req.memo, - expires_at: req.expires_at.map(|d| d as i64), - }, - ) - .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 new_site = repositories::site::models::NewSite { + url: request.url, + name: request.name, + path: request.path, + show: request.show, + can_use: request.can_use, + memo: request.memo, + expires_at: request.expires_at.map(|d| d as i64), + }; + + let s = self.site_repository.insert(&conn, &new_site).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::domain::site::CreateSiteResponse { + bpr::ss::site::CreateSiteResponse { error: None, - result: Some(bpr::ss::domain::site::create_site_response::Result { - site: Some(bpr::models::domain::Site::from(&s)), + result: Some(bpr::ss::site::create_site_response::Result { + site: Some(bpr::models::site::Site::from(&s)), }), } .encode_to_vec(), @@ -306,7 +341,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::CreateSiteResponse { + bpr::ss::site::CreateSiteResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -319,37 +354,19 @@ impl Service { Ok(()) } - async fn update_site(&self) -> Result<(), Box> { + async fn get_site(&self) -> Result<(), Box> { let s = self .connection_broker - .queue_subscribe( - bpr::ss::domain::site::SUBJECT_UPDATE_SITE, - self.queue_broker.as_str(), - ) + .queue_subscribe(bpr::ss::site::SUBJECT_GET_SITE, self.queue_broker.as_str()) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::UpdateSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { - bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { - message: format!("invalid request: {}", e), - }) - })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, + let req = bpr::ss::site::GetSiteRequest::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 => { @@ -367,6 +384,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -376,21 +422,137 @@ impl Service { }) })?; + let s = self.site_repository.select(&conn, 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, + }) + })?; + + message + .respond( + bpr::ss::site::GetSiteResponse { + error: None, + result: Some(bpr::ss::site::get_site_response::Result { + site: s.map(|d| bpr::models::site::Site::from(&d)), + }), + } + .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::site::GetSiteResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_site(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::site::SUBJECT_UPDATE_SITE, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::site::UpdateSiteRequest::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 request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.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 modify_site = repositories::site::models::ModifySite { + url: request.url, + name: request.name, + path: request.path, + show: request.show, + can_use: request.can_use, + memo: request.memo, + expires_at: request.expires_at.map(|d| d as i64), + }; + let _affected = self .site_repository - .update( - &conn, - id, - &repositories::site::models::ModifySite { - url: req.url, - name: req.name, - path: req.path, - show: req.show, - can_use: req.can_use, - memo: req.memo, - expires_at: req.expires_at.map(|d| d as i64), - }, - ) + .update(&conn, id, &modify_site) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -409,10 +571,10 @@ impl Service { message .respond( - bpr::ss::domain::site::UpdateSiteResponse { + bpr::ss::site::UpdateSiteResponse { error: None, - result: Some(bpr::ss::domain::site::update_site_response::Result { - site: s.map(|d| bpr::models::domain::Site::from(&d)), + result: Some(bpr::ss::site::update_site_response::Result { + site: s.map(|d| bpr::models::site::Site::from(&d)), }), } .encode_to_vec(), @@ -432,7 +594,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::UpdateSiteResponse { + bpr::ss::site::UpdateSiteResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } @@ -449,33 +611,19 @@ impl Service { let s = self .connection_broker .queue_subscribe( - bpr::ss::domain::site::SUBJECT_DELETE_SITE, + bpr::ss::site::SUBJECT_DELETE_SITE, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { - let req = bpr::ss::domain::site::DeleteSiteRequest::decode(message.data.as_slice()) - .map_err(|e| { + let req = + bpr::ss::site::DeleteSiteRequest::decode(message.data.as_slice()).map_err(|e| { bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { message: format!("invalid request: {}", e), }) })?; - - let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| { - bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { - message: "invalid id param".to_string(), - detail: bcr::error::rpc::InvalidParamsDetail { - location: "request".to_string(), - param: "id".to_string(), - value: req.id.clone(), - error_type: bcr::error::rpc::InvalidParamsType::Required, - message: e.to_string(), - }, - }) - })?; - let client = match req.client { Some(c) => c, None => { @@ -493,6 +641,35 @@ impl Service { )); } }; + let request = match req.request { + Some(r) => r, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid request information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "request".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { + bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { + message: "invalid id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "id".to_string(), + value: request.id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }) + })?; let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { @@ -512,9 +689,9 @@ impl Service { message .respond( - bpr::ss::domain::site::DeleteSiteResponse { + bpr::ss::site::DeleteSiteResponse { error: None, - result: Some(bpr::ss::domain::site::delete_site_response::Result {}), + result: Some(bpr::ss::site::delete_site_response::Result {}), } .encode_to_vec(), ) @@ -533,7 +710,7 @@ impl Service { { message .respond( - bpr::ss::domain::site::DeleteSiteResponse { + bpr::ss::site::DeleteSiteResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, }