From 7393f743fa782bfd6aa077308c1037dbec396da6 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Wed, 31 Aug 2022 09:01:27 +0000 Subject: [PATCH] betting is added --- Cargo.toml | 4 +- src/api/betting/mod.rs | 1 + src/api/betting/service.rs | 209 +++++++++++++++++++++++++++++++++++++ src/api/mod.rs | 1 + src/main.rs | 3 + 5 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 src/api/betting/mod.rs create mode 100644 src/api/betting/service.rs diff --git a/Cargo.toml b/Cargo.toml index 1523cac..63f741d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ prost = { version = "0" } nats = { version = "0" } base64 = { version = "0" } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.85-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.71-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.86-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.72-snapshot" } [build-dependencies] diff --git a/src/api/betting/mod.rs b/src/api/betting/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/api/betting/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/api/betting/service.rs b/src/api/betting/service.rs new file mode 100644 index 0000000..75d8312 --- /dev/null +++ b/src/api/betting/service.rs @@ -0,0 +1,209 @@ +use beteran_common_rust as bcr; +use beteran_protobuf_rust as bpr; +use prost::Message; + +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("beteran-backend-server-edge::betting::service::Service") + .finish() + } +} + +impl Service { + /// + pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service { + Service { + connection_broker, + queue_broker, + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!(self.list_betting_history(),).map(|_| ()) + } + + fn get_client_in_header( + &self, + message: &nats::asynk::Message, + ) -> Result { + match &message.headers { + Some(headers) => { + let client = match headers.get(bpr::c2se::core::network::HEADER_CLIENT) { + Some(c) => { + let msg = base64::decode(c).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })?; + bpr::models::core::network::Client::decode(msg.as_slice()).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })? + } + None => { + return Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid client information".to_string(), + })); + } + }; + + Ok(client) + } + None => Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid header".to_string(), + })), + } + } + + async fn list_betting_history(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::api::betting::SUBJECT_LIST_BETTING_HISTORY, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::api::betting::ListBettingHistoryRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_list_betting_history_req = bpr::ss::api::betting::ListBettingHistoryRequest { + client: Some(client), + request: Some( + bpr::ss::api::betting::list_betting_history_request::Request { + pagination: req.pagination, + sorts: req.sorts, + search: match req.search { + Some(s) => Some( + bpr::ss::api::betting::list_betting_history_request::request::Search { + member_id: s.member_id, + vendor_id: s.vendor_id, + vendor_name: s.vendor_name, + vendor_name_like: s.vendor_name_like, + game_id: s.game_id, + game_name: s.game_name, + game_name_like: s.game_name_like, + game_category: s.game_category, + game_category_like: s.game_category_like, + game_type: s.game_type, + game_type_like: s.game_type_like, + currency: s.currency, + currency_like: s.currency_like, + key: s.key, + key_like: s.key_like, + ref_id: s.ref_id, + ref_id_like: s.ref_id_like, + o_ref_id: s.o_ref_id, + o_ref_id_like: s.o_ref_id_like, + group_key: s.group_key, + group_key_like: s.group_key_like, + is_bonus: s.is_bonus, + is_promo: s.is_promo, + is_jackpot: s.is_jackpot, + site_username: s.site_username, + site_username_like: s.site_username_like, + betting_type: s.betting_type, + betting_type_like: s.betting_type_like, + category: s.category, + category_like: s.category_like, + }, + ), + None => None, + }, + }, + ), + }; + + let ss_list_betting_history_res_msg = self + .connection_broker + .request( + bpr::ss::api::betting::SUBJECT_LIST_BETTING_HISTORY, + ss_list_betting_history_req.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, + }) + })?; + + let ss_list_betting_history_res = + bpr::ss::api::betting::ListBettingHistoryResponse::decode( + ss_list_betting_history_res_msg.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, + }) + })?; + + if let Some(e) = ss_list_betting_history_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_list_betting_history_res.result { + message + .respond( + bpr::c2se::api::betting::ListBettingHistoryResponse { + error: None, + result: Some( + bpr::c2se::api::betting::list_betting_history_response::Result { + betting_history: r.betting_history, + }, + ), + } + .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::c2se::api::betting::ListBettingHistoryResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index fd2b58f..2965f74 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,2 +1,3 @@ pub mod game; pub mod vendor; +pub mod betting; diff --git a/src/main.rs b/src/main.rs index e90bea4..33fa836 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,6 +58,8 @@ async fn main() -> Result<(), Box> { api::vendor::service::Service::new(connection_broker.clone(), queue_broker.clone()); let api_game_service = api::game::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let api_betting_service = + api::betting::service::Service::new(connection_broker.clone(), queue_broker.clone()); println!("Server edge[beteran-backend-server-edge] is started"); @@ -76,6 +78,7 @@ async fn main() -> Result<(), Box> { site_service.subscribe(), api_vendor_service.subscribe(), api_game_service.subscribe(), + api_betting_service.subscribe(), )?; Ok(())