From 6b9b7e6b241281345ca9778dcd4d042bc1bd1225 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Thu, 18 Aug 2022 04:27:11 +0000 Subject: [PATCH] implemented --- Cargo.toml | 4 +- src/main.rs | 7 +- src/services/game/mod.rs | 1 + src/services/game/models.rs | 21 +++++ src/services/game/service.rs | 166 ++++++++++++++++++++++++++++++++++- 5 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 src/services/game/models.rs diff --git a/Cargo.toml b/Cargo.toml index 7556531..4f19555 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,8 +29,8 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] } tokio-cron-scheduler = { version = "*" } uuid = { version = "0", features = ["serde", "v4", "v5"] } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.62-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.46-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.63-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.47-snapshot" } [build-dependencies] diff --git a/src/main.rs b/src/main.rs index 099c0cc..ef5e94f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,7 @@ async fn main() -> Result<(), Box> { let game_service = services::game::service::Service::new( connection_server_broker.clone(), queue_server_broker.clone(), + pool.clone(), ); let betting_service = services::betting::service::Service::new( connection_server_broker.clone(), @@ -116,10 +117,10 @@ async fn main() -> Result<(), Box> { println!("Server service [beteran-api-kgon-server-service] is started"); futures::try_join!( - member_service.subscribe(), - member_account_service.subscribe(), + // member_service.subscribe(), + // member_account_service.subscribe(), game_service.subscribe(), - betting_service.subscribe(), + // betting_service.subscribe(), )?; sched.shutdown().await?; diff --git a/src/services/game/mod.rs b/src/services/game/mod.rs index 1f278a4..b9e52db 100644 --- a/src/services/game/mod.rs +++ b/src/services/game/mod.rs @@ -1 +1,2 @@ +pub mod models; pub mod service; diff --git a/src/services/game/models.rs b/src/services/game/models.rs new file mode 100644 index 0000000..e1413d0 --- /dev/null +++ b/src/services/game/models.rs @@ -0,0 +1,21 @@ +use crate::repositories; +use beteran_protobuf_rust as bpr; + +impl From<&repositories::vendor::models::Vendor> for bpr::models::api::vendor::Vendor { + fn from(d: &repositories::vendor::models::Vendor) -> Self { + bpr::models::api::vendor::Vendor { + id: d.id as u64, + name: d.name.clone(), + company_id: d.company_id as u64, + vendor_id: d.vendor_id as u64, + key: d.key.clone(), + category: d.category.clone(), + max_bet_casino: d.max_bet_casino as u64, + max_bet_slot: d.max_bet_slot as u64, + is_enable: d.is_enable.clone(), + bet_count: d.bet_count as u64, + created_at: d.created_at as u64, + updated_at: d.updated_at as u64, + } + } +} diff --git a/src/services/game/service.rs b/src/services/game/service.rs index aa5dbfe..4816011 100644 --- a/src/services/game/service.rs +++ b/src/services/game/service.rs @@ -1,7 +1,18 @@ +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>, + vendor_repository: repositories::vendor::repository::Repository, } impl std::fmt::Debug for Service { @@ -12,18 +23,169 @@ impl std::fmt::Debug for Service { impl Service { /// - pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service { + pub fn new( + connection_broker: nats::asynk::Connection, + queue_broker: String, + pool: Pool>, + ) -> Service { Service { connection_broker, queue_broker, + pool, + vendor_repository: repositories::vendor::repository::Repository::new(), } } pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { - futures::try_join!(self.list_vendors(), self.list_games(), self.execute_game(),).map(|_| ()) + futures::try_join!(self.list_vendors()).map(|_| ()) } async fn list_vendors(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::ss::api::vendor::SUBJECT_LIST_VENDORS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let req = bpr::ss::api::vendor::ListVendorsRequest::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::vendor::models::FindAll { + search: match request.search { + Some(s) => Some(repositories::vendor::models::FindAllSearch { + name_like: s.name_like, + company_id: s.company_id.map(|d| d as i64), + vendor_id: s.vendor_id.map(|d| d as i64), + key_like: s.key_like, + category_like: s.category_like, + is_enable: s.is_enable, + }), + None => None, + }, + 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 + .vendor_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 + .vendor_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::api::vendor::ListVendorsResponse { + error: None, + result: Some(bpr::ss::api::vendor::list_vendors_response::Result { + vendors: list + .iter() + .map(|d| bpr::models::api::vendor::Vendor::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::api::vendor::ListVendorsResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + Ok(()) } async fn list_games(&self) -> Result<(), Box> {