From ba825a641a8507c4b059978b5114d10174fade95 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Fri, 12 Aug 2022 09:51:58 +0000 Subject: [PATCH] bug fixed --- Cargo.toml | 4 +- migrations/202206201220_member_level/up.sql | 1 - migrations/202206201230_member_class/up.sql | 1 - src/compositions/member/composition.rs | 2 - src/compositions/member/models.rs | 2 - src/repositories/member_class/models.rs | 20 +- src/repositories/member_class/repository.rs | 49 +- src/repositories/member_class/schema.rs | 2 - src/repositories/member_level/models.rs | 20 +- src/repositories/member_level/repository.rs | 47 +- src/repositories/member_level/schema.rs | 2 - src/services/member/models.rs | 2 - src/services/member_class/mod.rs | 2 + src/services/member_class/models.rs | 1 + src/services/member_class/service.rs | 591 ++++++++++++++++++++ src/services/member_level/mod.rs | 2 + src/services/member_level/models.rs | 1 + src/services/member_level/service.rs | 549 ++++++++++++++++++ src/services/mod.rs | 2 + 19 files changed, 1268 insertions(+), 32 deletions(-) create mode 100644 src/services/member_class/mod.rs create mode 100644 src/services/member_class/models.rs create mode 100644 src/services/member_class/service.rs create mode 100644 src/services/member_level/mod.rs create mode 100644 src/services/member_level/models.rs create mode 100644 src/services/member_level/service.rs diff --git a/Cargo.toml b/Cargo.toml index 1d8ad87..a772be2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] } tokio-cron-scheduler = { version = "0" } uuid = { version = "0", features = ["serde", "v4", "v5"] } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.40-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.23-snapshot" } +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" } [build-dependencies] diff --git a/migrations/202206201220_member_level/up.sql b/migrations/202206201220_member_level/up.sql index 0526406..a0a387f 100644 --- a/migrations/202206201220_member_level/up.sql +++ b/migrations/202206201220_member_level/up.sql @@ -5,7 +5,6 @@ CREATE TABLE IF NOT EXISTS member_levels ( sort_order INTEGER NOT NULL, created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), - deleted_at BIGINT, PRIMARY KEY (id), UNIQUE (name) ); diff --git a/migrations/202206201230_member_class/up.sql b/migrations/202206201230_member_class/up.sql index 1bab6e5..2b9ad91 100644 --- a/migrations/202206201230_member_class/up.sql +++ b/migrations/202206201230_member_class/up.sql @@ -5,7 +5,6 @@ CREATE TABLE IF NOT EXISTS member_classes ( parent_id UUID, 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, PRIMARY KEY (id), UNIQUE (name) ); diff --git a/src/compositions/member/composition.rs b/src/compositions/member/composition.rs index 8901e0f..ffd0525 100644 --- a/src/compositions/member/composition.rs +++ b/src/compositions/member/composition.rs @@ -41,14 +41,12 @@ SELECT mc.name as mc_name, mc.created_at as mc_created_at, mc.updated_at as mc_updated_at, - mc.deleted_at as mc_deleted_at, ml.id as ml_id, ml.name as ml_name, ml.sort_order as ml_sort_order, ml.created_at as ml_created_at, ml.updated_at as ml_updated_at, - ml.deleted_at as ml_deleted_at, _m.id as _m_id, _m.site_id as _m_site_id, diff --git a/src/compositions/member/models.rs b/src/compositions/member/models.rs index 29259e6..a2fdd2c 100644 --- a/src/compositions/member/models.rs +++ b/src/compositions/member/models.rs @@ -126,7 +126,6 @@ impl QueryableByName for MemberModel { name: row.get("mc_name")?, created_at: row.get("mc_created_at")?, updated_at: row.get("mc_updated_at")?, - deleted_at: row.get("mc_deleted_at")?, }, member_level: _MemberLevel { id: row.get("ml_id")?, @@ -134,7 +133,6 @@ impl QueryableByName for MemberModel { sort_order: row.get("ml_sort_order")?, created_at: row.get("ml_created_at")?, updated_at: row.get("ml_updated_at")?, - deleted_at: row.get("ml_deleted_at")?, }, username: row.get("m_username")?, password: row.get("m_password")?, diff --git a/src/repositories/member_class/models.rs b/src/repositories/member_class/models.rs index 0ea8c43..392f299 100644 --- a/src/repositories/member_class/models.rs +++ b/src/repositories/member_class/models.rs @@ -15,8 +15,26 @@ pub struct MemberClass { pub created_at: i64, /// pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_classes"] +pub struct NewMemberClass { /// - pub deleted_at: Option, + pub parent_id: Option, + /// + pub name: String, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_classes"] +pub struct ModifyMemberClass { + /// + pub parent_id: Option, + /// + pub name: String, } /// diff --git a/src/repositories/member_class/repository.rs b/src/repositories/member_class/repository.rs index 1f696b3..d90c2a3 100644 --- a/src/repositories/member_class/repository.rs +++ b/src/repositories/member_class/repository.rs @@ -26,6 +26,19 @@ impl Repository { Repository {} } + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_member_class: &models::NewMemberClass, + ) -> Result { + let inserted = diesel::insert_into(member_classes::table) + .values(new_member_class) + .get_result::(conn)?; + + Ok(inserted) + } + /// pub fn select( &self, @@ -41,7 +54,7 @@ impl Repository { pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_classes::table.into_boxed(); @@ -56,7 +69,7 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_classes::table.into_boxed(); @@ -85,9 +98,7 @@ impl Repository { "updated_at" => { q = q.order_by(member_classes::updated_at.asc()); } - "deleted_at" => { - q = q.order_by(member_classes::deleted_at.asc()); - } + _ => {} }, bcr::models::pagination::Sort::DESC(property) => match property.as_str() { @@ -100,9 +111,7 @@ impl Repository { "updated_at" => { q = q.order_by(member_classes::updated_at.desc()); } - "deleted_at" => { - q = q.order_by(member_classes::deleted_at.desc()); - } + _ => {} }, }; @@ -111,4 +120,28 @@ impl Repository { q.load::(conn) } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberClass, + ) -> Result { + use member_classes::dsl; + + diesel::update(dsl::member_classes.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_classes::dsl; + + diesel::delete(member_classes::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/member_class/schema.rs b/src/repositories/member_class/schema.rs index ff2c5fe..60b9a25 100644 --- a/src/repositories/member_class/schema.rs +++ b/src/repositories/member_class/schema.rs @@ -14,7 +14,5 @@ table! { created_at -> BigInt, /// updated_at -> BigInt, - /// - deleted_at -> Nullable, } } diff --git a/src/repositories/member_level/models.rs b/src/repositories/member_level/models.rs index 328e094..114d076 100644 --- a/src/repositories/member_level/models.rs +++ b/src/repositories/member_level/models.rs @@ -15,8 +15,26 @@ pub struct MemberLevel { pub created_at: i64, /// pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_levels"] +pub struct NewMemberLevel { /// - pub deleted_at: Option, + pub name: String, + /// + pub sort_order: i32, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_levels"] +pub struct ModifyMemberLevel { + /// + pub name: String, + /// + pub sort_order: i32, } /// diff --git a/src/repositories/member_level/repository.rs b/src/repositories/member_level/repository.rs index 3d69815..059ae19 100644 --- a/src/repositories/member_level/repository.rs +++ b/src/repositories/member_level/repository.rs @@ -26,6 +26,19 @@ impl Repository { Repository {} } + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_member_level: &models::NewMemberLevel, + ) -> Result { + let inserted = diesel::insert_into(member_levels::table) + .values(new_member_level) + .get_result::(conn)?; + + Ok(inserted) + } + /// pub fn select( &self, @@ -41,7 +54,7 @@ impl Repository { pub fn select_all_count( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result { let mut q = member_levels::table.into_boxed(); @@ -56,7 +69,7 @@ impl Repository { pub fn select_all( &self, conn: &diesel::PgConnection, - find_all: models::FindAll, + find_all: &models::FindAll, ) -> Result, Error> { let mut q = member_levels::table.into_boxed(); @@ -88,9 +101,6 @@ impl Repository { "updated_at" => { q = q.order_by(member_levels::updated_at.asc()); } - "deleted_at" => { - q = q.order_by(member_levels::deleted_at.asc()); - } _ => {} }, bcr::models::pagination::Sort::DESC(property) => match property.as_str() { @@ -106,9 +116,6 @@ impl Repository { "updated_at" => { q = q.order_by(member_levels::updated_at.desc()); } - "deleted_at" => { - q = q.order_by(member_levels::deleted_at.desc()); - } _ => {} }, }; @@ -117,4 +124,28 @@ impl Repository { q.load::(conn) } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberLevel, + ) -> Result { + use member_levels::dsl; + + diesel::update(dsl::member_levels.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_levels::dsl; + + diesel::delete(member_levels::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/repositories/member_level/schema.rs b/src/repositories/member_level/schema.rs index 5d0e18d..4c67312 100644 --- a/src/repositories/member_level/schema.rs +++ b/src/repositories/member_level/schema.rs @@ -14,7 +14,5 @@ table! { created_at -> BigInt, /// updated_at -> BigInt, - /// - deleted_at -> Nullable, } } diff --git a/src/services/member/models.rs b/src/services/member/models.rs index ee84561..21b4d0f 100644 --- a/src/services/member/models.rs +++ b/src/services/member/models.rs @@ -36,7 +36,6 @@ impl From<&repositories::member_class::models::MemberClass> for bpr::models::mem name: d.name.clone(), created_at: d.created_at as u64, updated_at: d.updated_at as u64, - deleted_at: d.deleted_at.map(|d| d as u64), } } } @@ -49,7 +48,6 @@ impl From<&repositories::member_level::models::MemberLevel> for bpr::models::mem sort_order: d.sort_order as u32, created_at: d.created_at as u64, updated_at: d.updated_at as u64, - deleted_at: d.deleted_at.map(|d| d as u64), } } } diff --git a/src/services/member_class/mod.rs b/src/services/member_class/mod.rs new file mode 100644 index 0000000..b9e52db --- /dev/null +++ b/src/services/member_class/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod service; diff --git a/src/services/member_class/models.rs b/src/services/member_class/models.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/services/member_class/models.rs @@ -0,0 +1 @@ + diff --git a/src/services/member_class/service.rs b/src/services/member_class/service.rs new file mode 100644 index 0000000..80e7479 --- /dev/null +++ b/src/services/member_class/service.rs @@ -0,0 +1,591 @@ +//! +//! + +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_composition: compositions::site::composition::Composition, + member_class_repository: repositories::member_class::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_composition: compositions::site::composition::Composition::new(), + member_class_repository: repositories::member_class::repository::Repository::new(), + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.list_member_classes(), + self.create_member_class(), + self.update_member_class(), + self.delete_member_class(), + ) + .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_class(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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 { + Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) { + Ok(parent_id) => Some(parent_id), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid parent_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "parent_id".to_string(), + value: parent_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + 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, + message: format!("server {}", e), + data: None, + }) + })?; + + let s = self + .member_class_repository + .insert( + &conn, + &repositories::member_class::models::NewMemberClass { + parent_id, + name: req.name, + }, + ) + .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_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)), + }, + ), + } + .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_class::CreateMemberClassResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_classes(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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 client = match req.client { + Some(c) => c, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid client information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "client".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let conn = self.pool.get().map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + let find_all = repositories::member_class::models::FindAll { + name_like: None, + pagination: req + .pagination + .as_ref() + .map(|d| bcr::models::pagination::Pagination::from(d)), + sorts: Some( + req + .sorts + .iter() + .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) + .collect(), + ), + }; + + let count = self + .member_class_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_class_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::member_class::ListMemberClassesResponse { + error: None, + result: Some( + bpr::ss::member::member_class::list_member_classes_response::Result { + member_classes: list + .iter() + .map(|d| bpr::models::member::MemberClass::from(d)) + .collect(), + }, + ), + } + .encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + Ok::<(), bcr::error::rpc::Error>(()) + } + .await + { + message + .respond( + bpr::ss::member::member_class::ListMemberClassesResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_class(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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| { + 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 parent_id = match req.parent_id { + Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) { + Ok(parent_id) => Some(parent_id), + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid parent_id param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "parent_id".to_string(), + value: parent_id.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )); + } + }, + 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, + message: format!("server {}", e), + data: None, + }) + })?; + + let _affected = self + .member_class_repository + .update( + &conn, + id, + &repositories::member_class::models::ModifyMemberClass { + parent_id, + name: req.name, + }, + ) + .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::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)), + }, + ), + } + .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_class::UpdateMemberClassResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_class(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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 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 _affected = self + .member_class_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::member_class::DeleteMemberClassResponse { + error: None, + result: Some(bpr::ss::member::member_class::delete_member_class_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::member_class::DeleteMemberClassResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/services/member_level/mod.rs b/src/services/member_level/mod.rs new file mode 100644 index 0000000..b9e52db --- /dev/null +++ b/src/services/member_level/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod service; diff --git a/src/services/member_level/models.rs b/src/services/member_level/models.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/services/member_level/models.rs @@ -0,0 +1 @@ + diff --git a/src/services/member_level/service.rs b/src/services/member_level/service.rs new file mode 100644 index 0000000..8f43986 --- /dev/null +++ b/src/services/member_level/service.rs @@ -0,0 +1,549 @@ +//! +//! + +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_composition: compositions::site::composition::Composition, + member_level_repository: repositories::member_level::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_composition: compositions::site::composition::Composition::new(), + member_level_repository: repositories::member_level::repository::Repository::new(), + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.list_member_levels(), + self.create_member_level(), + self.update_member_level(), + self.delete_member_level(), + ) + .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_level(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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, + }, + ) + .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), + }) + })?; + + let client = match req.client { + Some(c) => c, + None => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid client information".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "client".to_string(), + value: "".to_string(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: "".to_string(), + }, + }, + )); + } + }; + + let conn = self.pool.get().map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + let find_all = repositories::member_level::models::FindAll { + name_like: None, + pagination: req + .pagination + .as_ref() + .map(|d| bcr::models::pagination::Pagination::from(d)), + sorts: Some( + req + .sorts + .iter() + .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) + .collect(), + ), + }; + + let count = self + .member_level_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_level_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::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(), + }, + ), + } + .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::ListMemberLevelsResponse { + 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::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::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 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 _affected = self + .member_level_repository + .update( + &conn, + id, + &repositories::member_level::models::ModifyMemberLevel { + name: req.name, + sort_order: req.sort_order as i32, + }, + ) + .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 + .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::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)), + }, + ), + } + .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::UpdateMemberLevelResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_level(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member::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 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 _affected = self + .member_level_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::member_level::DeleteMemberLevelResponse { + error: None, + result: Some(bpr::ss::member::member_level::delete_member_level_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::member_level::DeleteMemberLevelResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/services/mod.rs b/src/services/mod.rs index d1df18e..40bda1d 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,3 +1,5 @@ pub mod identity; pub mod member; +pub mod member_class; +pub mod member_level; pub mod site;