From 08041ce356380bcfaab212ee42db7a9a818c2814 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Sat, 20 Aug 2022 10:55:25 +0000 Subject: [PATCH] member_referrer is modified --- Cargo.toml | 4 +- .../member_referrer/composition.rs | 74 +++- .../member_referrer/repository.rs | 32 +- src/services/member_referrer/service.rs | 407 +++++++++++++++++- 4 files changed, 509 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d872c92..ffc7931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,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.67-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.51-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.68-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.52-snapshot" } [build-dependencies] diff --git a/src/compositions/member_referrer/composition.rs b/src/compositions/member_referrer/composition.rs index 95e6160..15aafb4 100644 --- a/src/compositions/member_referrer/composition.rs +++ b/src/compositions/member_referrer/composition.rs @@ -4,6 +4,7 @@ use super::models; use crate::repositories; use diesel::{result::Error, sql_query, RunQueryDsl}; +use std::fmt::Write; static MEMBER_QUERY: &str = " SELECT @@ -67,7 +68,7 @@ impl Composition { " {} WHERE - m.id = $1 + mr.id = $1 ", MEMBER_QUERY ); @@ -84,14 +85,81 @@ impl Composition { } } + /// + pub fn selects( + &self, + conn: &diesel::PgConnection, + ids: Vec, + ) -> Result, Error> { + if ids.is_empty() { + return Ok(vec![]); + } + + let mut query_where_id_in = String::new(); + + for id in ids { + if !query_where_id_in.is_empty() { + write!(&mut query_where_id_in, " , ") + .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; + } + + write!(&mut query_where_id_in, " '{}' ", id) + .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; + } + + let query = format!( + " + {} + WHERE + mr.id IN ({}) + ", + MEMBER_QUERY, query_where_id_in + ); + + println!("query: {}", query); + + match sql_query(query).get_results::(conn) { + Ok(m) => Ok(m), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(vec![]), + _ => Err(e), + }, + } + } + + /// + pub fn select_by_username( + &self, + conn: &diesel::PgConnection, + username: &str, + ) -> Result, Error> { + let query = format!( + " + {} + WHERE + _m.username = $1 + ", + MEMBER_QUERY + ); + + match sql_query(query) + .bind::(username) + .get_result::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } + } + /// pub fn select_all( &self, conn: &diesel::PgConnection, find_all: &repositories::member_referrer::models::FindAll, ) -> Result, Error> { - use std::fmt::Write; - let mut query = String::new(); write!(&mut query, "{}", MEMBER_QUERY) .map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?; diff --git a/src/repositories/member_referrer/repository.rs b/src/repositories/member_referrer/repository.rs index 39d3231..bf411f5 100644 --- a/src/repositories/member_referrer/repository.rs +++ b/src/repositories/member_referrer/repository.rs @@ -31,10 +31,29 @@ impl Repository { &self, conn: &diesel::PgConnection, new_resource: &models::NewMemberReferrer, - ) -> Result { + ) -> Result { + use member_referrers::dsl; + let inserted = diesel::insert_into(member_referrers::table) .values(new_resource) - .get_result::(conn)?; + .returning(dsl::id) + .get_result(conn)?; + + Ok(inserted) + } + + /// + pub fn inserts( + &self, + conn: &diesel::PgConnection, + new_referrers: &Vec, + ) -> Result, Error> { + use member_referrers::dsl; + + let inserted = diesel::insert_into(member_referrers::table) + .values(new_referrers) + .returning(dsl::id) + .get_results(conn)?; Ok(inserted) } @@ -192,4 +211,13 @@ impl Repository { .execute(conn) .map(|c| c as u64) } + + /// + pub fn deletes(&self, conn: &diesel::PgConnection, ids: Vec) -> Result { + use member_referrers::dsl; + + diesel::delete(member_referrers::table.filter(dsl::id.eq_any(ids))) + .execute(conn) + .map(|c| c as u64) + } } diff --git a/src/services/member_referrer/service.rs b/src/services/member_referrer/service.rs index 7fdbe6d..5fdebbf 100644 --- a/src/services/member_referrer/service.rs +++ b/src/services/member_referrer/service.rs @@ -50,10 +50,13 @@ impl Service { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( self.create_member_referrer(), + self.create_member_referrers(), self.list_member_referrers(), self.get_member_referrer(), + self.get_member_referrer_by_code(), self.update_member_referrer(), self.delete_member_referrer(), + self.delete_member_referrers(), ) .map(|_| ()) } @@ -179,7 +182,7 @@ impl Service { let mr = self .member_referrer_composition - .select(&conn, s.id) + .select(&conn, s) .map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -230,6 +233,165 @@ impl Service { Ok(()) } + async fn create_member_referrers(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_referrer::SUBJECT_CREATE_MEMBER_REFERRERS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::member_referrer::CreateMemberReferrersRequest::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 mut member_ids: Vec = vec![]; + for i in request.member_ids { + let u = match uuid::Uuid::from_str(i.as_str()) { + Ok(u) => u, + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid member_ids param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "member_ids".to_string(), + value: i.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )) + } + }; + member_ids.push(u); + } + + 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_referrers: Vec = + member_ids + .iter() + .map( + |member_id| repositories::member_referrer::models::NewMemberReferrer { + member_id: member_id.clone(), + }, + ) + .collect(); + + let s = self + .member_referrer_repository + .inserts(&conn, &new_member_referrers) + .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 mr = self + .member_referrer_composition + .selects(&conn, s) + .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_referrer::CreateMemberReferrersResponse { + error: None, + result: Some( + bpr::ss::member_referrer::create_member_referrers_response::Result { + member_referrers: mr + .iter() + .map(bpr::models::member_referrer::MemberReferrerModel::from) + .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_referrer::CreateMemberReferrersResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + async fn list_member_referrers(&self) -> Result<(), Box> { let s = self .connection_broker @@ -528,6 +690,119 @@ impl Service { Ok(()) } + async fn get_member_referrer_by_code(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::member_referrer::GetMemberReferrerByCodeRequest::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 s = self + .member_referrer_composition + .select_by_username(&conn, &request.code) + .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_referrer::GetMemberReferrerByCodeResponse { + error: None, + result: Some( + bpr::ss::member_referrer::get_member_referrer_by_code_response::Result { + member_referrer: s + .map(|d| bpr::models::member_referrer::MemberReferrerModel::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_referrer::GetMemberReferrerByCodeResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + async fn update_member_referrer(&self) -> Result<(), Box> { let s = self .connection_broker @@ -788,4 +1063,134 @@ impl Service { Ok(()) } + + async fn delete_member_referrers(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::member_referrer::SUBJECT_DELETE_MEMBER_REFERRERS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = + bpr::ss::member_referrer::DeleteMemberReferrersRequest::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 mut ids: Vec = vec![]; + for i in request.ids { + let u = match uuid::Uuid::from_str(i.as_str()) { + Ok(u) => u, + Err(e) => { + return Err(bcr::error::rpc::Error::InvalidParams( + bcr::error::rpc::InvalidParams { + message: "invalid ids param".to_string(), + detail: bcr::error::rpc::InvalidParamsDetail { + location: "request".to_string(), + param: "ids".to_string(), + value: i.clone(), + error_type: bcr::error::rpc::InvalidParamsType::Required, + message: e.to_string(), + }, + }, + )) + } + }; + ids.push(u); + } + + 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_referrer_repository + .deletes(&conn, ids) + .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_referrer::DeleteMemberReferrersResponse { + error: None, + result: Some(bpr::ss::member_referrer::delete_member_referrers_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_referrer::DeleteMemberReferrersResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } }