From 51d3e6648f56a92be5e29a282d58b8600e98a12c Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Sat, 20 Aug 2022 06:17:17 +0000 Subject: [PATCH] api refactoring --- src/api/betting/api.rs | 165 ++++++++++++++++++++++++------- src/api/betting/models.rs | 41 +++++--- src/api/game/api.rs | 33 ++++--- src/api/game/models.rs | 6 +- src/api/member/api.rs | 20 ++-- src/api/member/models.rs | 2 +- src/api/member_account/api.rs | 161 +++++++++++++++++++++--------- src/api/member_account/models.rs | 21 +++- src/api/vendor/api.rs | 14 ++- src/events/member/event.rs | 83 +++++++++------- 10 files changed, 381 insertions(+), 165 deletions(-) diff --git a/src/api/betting/api.rs b/src/api/betting/api.rs index 1d820b0..2ec0145 100644 --- a/src/api/betting/api.rs +++ b/src/api/betting/api.rs @@ -1,4 +1,5 @@ use super::models; +use crate::api::core::models::Error; use crate::core; use std::collections::HashMap; @@ -24,10 +25,10 @@ impl Api { } /// - pub async fn find_transactions( + pub async fn list_bettings( &self, - data: models::FindTransactionsRequest, - ) -> Result { + data: models::ListBettingsRequest, + ) -> Result { let mut params = HashMap::new(); if let Some(vendor_key) = data.vendor_key { @@ -62,25 +63,48 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let transactions = match r.transactions { + Some(v) => v, + None => vec![], + }; + + Ok(models::ListBettingsResponse { transactions }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(models::Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } /// - pub async fn find_gragmatic_history( + pub async fn get_gragmatic_betting( &self, - data: models::FindPragmaticHistoryRequest, - ) -> Result { + data: models::GetPragmaticBettingRequest, + ) -> Result { let mut params = HashMap::new(); params.insert("id", data.id); @@ -100,25 +124,48 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let url = match r.url { + Some(v) => v, + None => "".to_string(), + }; + + Ok(models::GetPragmaticBettingResponse { url }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(models::Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } /// - pub async fn find_evolution_transaction_detail( + pub async fn get_evolution_betting_detail( &self, - data: models::FindEvolutionTransactionDetailRequest, - ) -> Result { + data: models::GetEvolutionBettingDetailRequest, + ) -> Result { let mut params = HashMap::new(); params.insert("ids", data.ids); @@ -141,28 +188,51 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { reqwest::StatusCode::OK => match res - .json::() + .json::() .await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let transactions = match r.transactions { + Some(v) => v, + None => vec![], + }; + + Ok(models::GetEvolutionBettingDetailResponse { transactions }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(models::Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } /// - pub async fn find_statistic( + pub async fn get_statistic( &self, - data: models::FindStatisticRequest, - ) -> Result { + data: models::GetStatisticRequest, + ) -> Result { let mut params = HashMap::new(); if let Some(vendor_key) = data.vendor_key { @@ -190,17 +260,40 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let data = match r.data { + Some(v) => v, + None => "".to_string(), + }; + + Ok(models::GetStatisticResponse { data }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(models::Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } } diff --git a/src/api/betting/models.rs b/src/api/betting/models.rs index 0320047..8f0f3c9 100644 --- a/src/api/betting/models.rs +++ b/src/api/betting/models.rs @@ -1,11 +1,6 @@ use serde::{Deserialize, Serialize}; -pub struct Error { - pub code: i64, - pub msg: Option, -} - -pub struct FindTransactionsRequest { +pub struct ListBettingsRequest { pub vendor_key: Option, pub sdate: Option, pub edate: Option, @@ -15,43 +10,63 @@ pub struct FindTransactionsRequest { } #[derive(Serialize, Deserialize, Debug)] -pub struct FindTransactionsResponse { +pub struct _ListBettingsResponse { pub code: i64, pub msg: Option, + pub transactions: Option>, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct ListBettingsResponse { pub transactions: Vec, } -pub struct FindPragmaticHistoryRequest { +pub struct GetPragmaticBettingRequest { pub id: String, } #[derive(Serialize, Deserialize, Debug)] -pub struct FindPragmaticHistoryResponse { +pub struct _GetPragmaticBettingResponse { pub code: i64, pub msg: Option, pub url: Option, } -pub struct FindEvolutionTransactionDetailRequest { +#[derive(Serialize, Deserialize, Debug)] +pub struct GetPragmaticBettingResponse { + pub url: String, +} + +pub struct GetEvolutionBettingDetailRequest { pub ids: String, } #[derive(Serialize, Deserialize, Debug)] -pub struct FindEvolutionTransactionDetailResponse { +pub struct _GetEvolutionBettingDetailResponse { pub code: i64, pub msg: Option, + pub transactions: Option>, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct GetEvolutionBettingDetailResponse { pub transactions: Vec, } -pub struct FindStatisticRequest { +pub struct GetStatisticRequest { pub vendor_key: Option, pub group_key: Option, pub date: Option, } #[derive(Serialize, Deserialize, Debug)] -pub struct FindStatisticResponse { +pub struct _GetStatisticResponse { pub code: i64, pub msg: Option, pub data: Option, } + +#[derive(Serialize, Deserialize, Debug)] +pub struct GetStatisticResponse { + pub data: String, +} diff --git a/src/api/game/api.rs b/src/api/game/api.rs index 5a869c0..6490e1f 100644 --- a/src/api/game/api.rs +++ b/src/api/game/api.rs @@ -66,14 +66,12 @@ impl Api { }); } - if let Some(games) = r.games { - Ok(models::ListGamesResponse { games }) - } else { - Err(Error { - code: r.code, - msg: r.msg, - }) - } + let games = match r.games { + Some(v) => v, + None => vec![], + }; + + Ok(models::ListGamesResponse { games }) } Err(e) => Err(Error { code: -1, @@ -141,10 +139,23 @@ impl Api { }); } + let user_id = match r.user_id { + Some(v) => v, + None => "".to_string(), + }; + let url = match r.url { + Some(v) => v, + None => "".to_string(), + }; + let balance = match r.balance { + Some(v) => v, + None => 0, + }; + Ok(models::PlayResponse { - user_id: r.user_id, - url: r.url, - balance: r.balance, + user_id, + url, + balance, }) } Err(e) => Err(Error { diff --git a/src/api/game/models.rs b/src/api/game/models.rs index 37383b8..4f9e03c 100644 --- a/src/api/game/models.rs +++ b/src/api/game/models.rs @@ -45,9 +45,9 @@ pub struct _PlayResponse { pub code: i64, pub msg: Option, #[serde(rename = "userId")] - pub user_id: String, - pub url: String, - pub balance: i64, + pub user_id: Option, + pub url: Option, + pub balance: Option, } #[derive(Debug)] diff --git a/src/api/member/api.rs b/src/api/member/api.rs index 1d9634c..ff49b8f 100644 --- a/src/api/member/api.rs +++ b/src/api/member/api.rs @@ -68,7 +68,12 @@ impl Api { }); } - Ok(models::ListMembersResponse { users: r.users }) + let users = match r.users { + Some(v) => v, + None => vec![], + }; + + Ok(models::ListMembersResponse { users }) } Err(e) => Err(Error { code: -1, @@ -130,13 +135,12 @@ impl Api { }); } - match r.id { - Some(id) => Ok(models::CreateMemberResponse { id }), - None => Err(Error { - code: -1, - msg: Some("id is not exist in response of api".to_string()), - }), - } + let id = match r.id { + Some(v) => v, + None => 0, + }; + + Ok(models::CreateMemberResponse { id }) } Err(e) => Err(Error { code: -1, diff --git a/src/api/member/models.rs b/src/api/member/models.rs index 908fb76..5dad897 100644 --- a/src/api/member/models.rs +++ b/src/api/member/models.rs @@ -21,7 +21,7 @@ pub struct Member { pub struct _ListMembersResponse { pub code: i64, pub msg: Option, - pub users: Vec, + pub users: Option>, } #[derive(Debug)] diff --git a/src/api/member_account/api.rs b/src/api/member_account/api.rs index e4ad946..a41ca1e 100644 --- a/src/api/member_account/api.rs +++ b/src/api/member_account/api.rs @@ -66,21 +66,29 @@ impl Api { }); } - if let (Some(balance), Some(balance_bota), Some(balance_sum), Some(companies)) = - (r.balance, r.balance_bota, r.balance_sum, r.companies) - { - Ok(models::GetBalanceForUserResponse { - balance, - balance_bota, - balance_sum, - companies, - }) - } else { - Err(Error { - code: r.code, - msg: r.msg, - }) - } + let balance = match r.balance { + Some(v) => v, + None => 0, + }; + let balance_bota = match r.balance_bota { + Some(v) => v, + None => 0, + }; + let balance_sum = match r.balance_sum { + Some(v) => v, + None => 0, + }; + let companies = match r.companies { + Some(v) => v, + None => 0, + }; + + Ok(models::GetBalanceForUserResponse { + balance, + balance_bota, + balance_sum, + companies, + }) } Err(e) => Err(Error { code: -1, @@ -131,17 +139,19 @@ impl Api { }); } - if let (Some(balance), Some(balance_bota)) = (r.balance, r.balance_bota) { - Ok(models::GetBalanceForPartnerResponse { - balance, - balance_bota, - }) - } else { - Err(Error { - code: r.code, - msg: r.msg, - }) - } + let balance = match r.balance { + Some(v) => v, + None => 0, + }; + let balance_bota = match r.balance_bota { + Some(v) => v, + None => 0, + }; + + Ok(models::GetBalanceForPartnerResponse { + balance, + balance_bota, + }) } Err(e) => Err(Error { code: -1, @@ -156,10 +166,10 @@ impl Api { } /// - pub async fn save_deposit( + pub async fn create_deposit( &self, - data: models::SaveDepositRequest, - ) -> Result { + data: models::CreateDepositRequest, + ) -> Result { let mut params = HashMap::new(); params.insert("username", data.username); @@ -186,25 +196,48 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(Error { code: 0, msg: None }), + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let balance = match r.balance { + Some(v) => v, + None => 0, + }; + + Ok(models::CreateDepositResponse { balance }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } /// - pub async fn save_withdraw( + pub async fn create_withdraw( &self, - data: models::SaveWithdrawRequest, - ) -> Result { + data: models::CreateWithdrawRequest, + ) -> Result { let mut params = HashMap::new(); params.insert("username", data.username); @@ -227,17 +260,57 @@ impl Api { .await { Ok(res) => res, - Err(err) => { - return Err(Error { code: 0, msg: None }); + Err(e) => { + return Err(Error { + code: -1, + msg: Some(e.to_string()), + }); } }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(Error { code: 0, msg: None }), + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => { + if r.code != 0 { + return Err(Error { + code: r.code, + msg: r.msg, + }); + } + + let balance = match r.balance { + Some(v) => v, + None => 0, + }; + let balance_cash = match r.balance_cash { + Some(v) => v, + None => 0, + }; + let balance_cash_bota = match r.balance_cash_bota { + Some(v) => v, + None => 0, + }; + let amount = match r.amount { + Some(v) => v, + None => 0, + }; + + Ok(models::CreateWithdrawResponse { + balance, + balance_cash, + balance_cash_bota, + amount, + }) + } + Err(e) => Err(Error { + code: -1, + msg: Some(e.to_string()), + }), }, - _ => Err(Error { code: 0, msg: None }), + _ => Err(Error { + code: -1, + msg: None, + }), } } } diff --git a/src/api/member_account/models.rs b/src/api/member_account/models.rs index 8d61513..1ef99e5 100644 --- a/src/api/member_account/models.rs +++ b/src/api/member_account/models.rs @@ -44,7 +44,7 @@ pub struct GetBalanceForPartnerResponse { pub balance_bota: i64, } -pub struct SaveDepositRequest { +pub struct CreateDepositRequest { pub username: String, pub cash_type: Option, pub amount: i64, @@ -52,21 +52,34 @@ pub struct SaveDepositRequest { } #[derive(Serialize, Deserialize, Debug)] -pub struct SaveDepositResponse { +pub struct _CreateDepositResponse { pub code: i64, pub msg: Option, + pub balance: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct CreateDepositResponse { pub balance: i64, } -pub struct SaveWithdrawRequest { +pub struct CreateWithdrawRequest { pub username: String, pub request_key: Option, } #[derive(Serialize, Deserialize, Debug)] -pub struct SaveWithdrawResponse { +pub struct _CreateWithdrawResponse { pub code: i64, pub msg: Option, + pub balance: Option, + pub balance_cash: Option, + pub balance_cash_bota: Option, + pub amount: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct CreateWithdrawResponse { pub balance: i64, pub balance_cash: i64, pub balance_cash_bota: i64, diff --git a/src/api/vendor/api.rs b/src/api/vendor/api.rs index ec740e2..58fa78b 100644 --- a/src/api/vendor/api.rs +++ b/src/api/vendor/api.rs @@ -60,14 +60,12 @@ impl Api { }); } - if let Some(vendors) = r.vendors { - Ok(models::ListVendorsResponse { vendors }) - } else { - Err(Error { - code: r.code, - msg: r.msg, - }) - } + let vendors = match r.vendors { + Some(v) => v, + None => vec![], + }; + + Ok(models::ListVendorsResponse { vendors }) } Err(e) => Err(Error { code: -1, diff --git a/src/events/member/event.rs b/src/events/member/event.rs index 4088684..011a317 100644 --- a/src/events/member/event.rs +++ b/src/events/member/event.rs @@ -10,6 +10,13 @@ use diesel::{ use prost::Message; use std::str::FromStr; +static MEMBER_CLASS_ID_MAIN_OFFICE: &str = "4b014ef5-3bab-4413-aaf9-b0040a70ec77"; +static MEMBER_CLASS_ID_BRANCH: &str = "ae9b874e-5d0e-4c4d-8432-f45f02691ceb"; +static MEMBER_CLASS_ID_DIVISION: &str = "f25a17e9-5c5f-4e9c-bf80-92a9cedf829c"; +static MEMBER_CLASS_ID_OFFICE: &str = "cac7b897-2549-4f04-8415-8868f1dcb1da"; +static MEMBER_CLASS_ID_STORE: &str = "e11cac11-3825-4f4e-9cd5-39367f23f973"; +static MEMBER_CLASS_ID_USER: &str = "4598f07a-86d1-42a4-b038-25706683a7cd"; + pub struct EventHandler { connection_broker: nats::asynk::Connection, queue_broker: String, @@ -116,7 +123,6 @@ impl EventHandler { )); } }; - let member_id = uuid::Uuid::from_str(member.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid member.id param".to_string(), @@ -129,7 +135,6 @@ impl EventHandler { }, }) })?; - let conn = self.pool.get().map_err(|e| { bcr::error::rpc::Error::Server(bcr::error::rpc::Server { code: bpr::protobuf::rpc::Error::SERVER_00, @@ -138,42 +143,46 @@ impl EventHandler { }) })?; - let api_create_res = self - .member_api - .create_member(api::member::models::CreateMemberRequest { - username: member.username.clone(), - nickname: member.nickname.clone(), - site_username: member.username.clone(), - group_key: match member.parent_member { - Some(m) => Some(m.username), - None => None, - }, - }) - .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, - }) - })?; + if let Some(mc) = member.member_class { + if mc.id.eq(MEMBER_CLASS_ID_USER) { + let api_create_res = self + .member_api + .create_member(api::member::models::CreateMemberRequest { + username: member.username.clone(), + nickname: member.nickname.clone(), + site_username: member.username.clone(), + group_key: match member.parent_member { + Some(m) => Some(m.username), + None => None, + }, + }) + .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, + }) + })?; - self - .member_repository - .insert( - &conn, - &repositories::member::models::NewMember { - id: api_create_res.id, - member_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, - }) - })?; + self + .member_repository + .insert( + &conn, + &repositories::member::models::NewMember { + id: api_create_res.id, + member_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, + }) + })?; + } + } Ok::<(), bcr::error::rpc::Error>(()) }