api refactoring

This commit is contained in:
병준 박 2022-08-20 06:17:17 +00:00
parent 9d0f9f1f34
commit 51d3e6648f
10 changed files with 381 additions and 165 deletions

View File

@ -1,4 +1,5 @@
use super::models; use super::models;
use crate::api::core::models::Error;
use crate::core; use crate::core;
use std::collections::HashMap; use std::collections::HashMap;
@ -24,10 +25,10 @@ impl Api {
} }
/// ///
pub async fn find_transactions( pub async fn list_bettings(
&self, &self,
data: models::FindTransactionsRequest, data: models::ListBettingsRequest,
) -> Result<models::FindTransactionsResponse, models::Error> { ) -> Result<models::ListBettingsResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
if let Some(vendor_key) = data.vendor_key { if let Some(vendor_key) = data.vendor_key {
@ -62,25 +63,48 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::FindTransactionsResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_ListBettingsResponse>().await {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(models::Error { code: 0, msg: None }), 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, &self,
data: models::FindPragmaticHistoryRequest, data: models::GetPragmaticBettingRequest,
) -> Result<models::FindPragmaticHistoryResponse, models::Error> { ) -> Result<models::GetPragmaticBettingResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("id", data.id); params.insert("id", data.id);
@ -100,25 +124,48 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::FindPragmaticHistoryResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_GetPragmaticBettingResponse>().await {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(models::Error { code: 0, msg: None }), 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, &self,
data: models::FindEvolutionTransactionDetailRequest, data: models::GetEvolutionBettingDetailRequest,
) -> Result<models::FindEvolutionTransactionDetailResponse, models::Error> { ) -> Result<models::GetEvolutionBettingDetailResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("ids", data.ids); params.insert("ids", data.ids);
@ -141,28 +188,51 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res reqwest::StatusCode::OK => match res
.json::<models::FindEvolutionTransactionDetailResponse>() .json::<models::_GetEvolutionBettingDetailResponse>()
.await .await
{ {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(models::Error { code: 0, msg: None }), 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, &self,
data: models::FindStatisticRequest, data: models::GetStatisticRequest,
) -> Result<models::FindStatisticResponse, models::Error> { ) -> Result<models::GetStatisticResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
if let Some(vendor_key) = data.vendor_key { if let Some(vendor_key) = data.vendor_key {
@ -190,17 +260,40 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::FindStatisticResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_GetStatisticResponse>().await {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(models::Error { code: 0, msg: None }), 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,
}),
} }
} }
} }

View File

@ -1,11 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub struct Error { pub struct ListBettingsRequest {
pub code: i64,
pub msg: Option<String>,
}
pub struct FindTransactionsRequest {
pub vendor_key: Option<String>, pub vendor_key: Option<String>,
pub sdate: Option<String>, pub sdate: Option<String>,
pub edate: Option<String>, pub edate: Option<String>,
@ -15,43 +10,63 @@ pub struct FindTransactionsRequest {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindTransactionsResponse { pub struct _ListBettingsResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub transactions: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ListBettingsResponse {
pub transactions: Vec<String>, pub transactions: Vec<String>,
} }
pub struct FindPragmaticHistoryRequest { pub struct GetPragmaticBettingRequest {
pub id: String, pub id: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindPragmaticHistoryResponse { pub struct _GetPragmaticBettingResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub url: Option<String>, pub url: Option<String>,
} }
pub struct FindEvolutionTransactionDetailRequest { #[derive(Serialize, Deserialize, Debug)]
pub struct GetPragmaticBettingResponse {
pub url: String,
}
pub struct GetEvolutionBettingDetailRequest {
pub ids: String, pub ids: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindEvolutionTransactionDetailResponse { pub struct _GetEvolutionBettingDetailResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub transactions: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetEvolutionBettingDetailResponse {
pub transactions: Vec<String>, pub transactions: Vec<String>,
} }
pub struct FindStatisticRequest { pub struct GetStatisticRequest {
pub vendor_key: Option<String>, pub vendor_key: Option<String>,
pub group_key: Option<String>, pub group_key: Option<String>,
pub date: Option<String>, pub date: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindStatisticResponse { pub struct _GetStatisticResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub data: Option<String>, pub data: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct GetStatisticResponse {
pub data: String,
}

View File

@ -66,14 +66,12 @@ impl Api {
}); });
} }
if let Some(games) = r.games { let games = match r.games {
Some(v) => v,
None => vec![],
};
Ok(models::ListGamesResponse { games }) Ok(models::ListGamesResponse { games })
} else {
Err(Error {
code: r.code,
msg: r.msg,
})
}
} }
Err(e) => Err(Error { Err(e) => Err(Error {
code: -1, 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 { Ok(models::PlayResponse {
user_id: r.user_id, user_id,
url: r.url, url,
balance: r.balance, balance,
}) })
} }
Err(e) => Err(Error { Err(e) => Err(Error {

View File

@ -45,9 +45,9 @@ pub struct _PlayResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
#[serde(rename = "userId")] #[serde(rename = "userId")]
pub user_id: String, pub user_id: Option<String>,
pub url: String, pub url: Option<String>,
pub balance: i64, pub balance: Option<i64>,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -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 { Err(e) => Err(Error {
code: -1, code: -1,
@ -130,13 +135,12 @@ impl Api {
}); });
} }
match r.id { let id = match r.id {
Some(id) => Ok(models::CreateMemberResponse { id }), Some(v) => v,
None => Err(Error { None => 0,
code: -1, };
msg: Some("id is not exist in response of api".to_string()),
}), Ok(models::CreateMemberResponse { id })
}
} }
Err(e) => Err(Error { Err(e) => Err(Error {
code: -1, code: -1,

View File

@ -21,7 +21,7 @@ pub struct Member {
pub struct _ListMembersResponse { pub struct _ListMembersResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub users: Vec<Member>, pub users: Option<Vec<Member>>,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -66,21 +66,29 @@ impl Api {
}); });
} }
if let (Some(balance), Some(balance_bota), Some(balance_sum), Some(companies)) = let balance = match r.balance {
(r.balance, r.balance_bota, r.balance_sum, r.companies) 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 { Ok(models::GetBalanceForUserResponse {
balance, balance,
balance_bota, balance_bota,
balance_sum, balance_sum,
companies, companies,
}) })
} else {
Err(Error {
code: r.code,
msg: r.msg,
})
}
} }
Err(e) => Err(Error { Err(e) => Err(Error {
code: -1, code: -1,
@ -131,17 +139,19 @@ impl Api {
}); });
} }
if let (Some(balance), Some(balance_bota)) = (r.balance, r.balance_bota) { 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 { Ok(models::GetBalanceForPartnerResponse {
balance, balance,
balance_bota, balance_bota,
}) })
} else {
Err(Error {
code: r.code,
msg: r.msg,
})
}
} }
Err(e) => Err(Error { Err(e) => Err(Error {
code: -1, code: -1,
@ -156,10 +166,10 @@ impl Api {
} }
/// ///
pub async fn save_deposit( pub async fn create_deposit(
&self, &self,
data: models::SaveDepositRequest, data: models::CreateDepositRequest,
) -> Result<models::SaveDepositResponse, Error> { ) -> Result<models::CreateDepositResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
@ -186,25 +196,48 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::SaveDepositResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_CreateDepositResponse>().await {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(Error { code: 0, msg: None }), 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, &self,
data: models::SaveWithdrawRequest, data: models::CreateWithdrawRequest,
) -> Result<models::SaveWithdrawResponse, Error> { ) -> Result<models::CreateWithdrawResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
@ -227,17 +260,57 @@ impl Api {
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(e) => {
return Err(Error { code: 0, msg: None }); return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::SaveWithdrawResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_CreateWithdrawResponse>().await {
Ok(r) => Ok(r), Ok(r) => {
Err(e) => Err(Error { code: 0, msg: None }), 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,
}),
} }
} }
} }

View File

@ -44,7 +44,7 @@ pub struct GetBalanceForPartnerResponse {
pub balance_bota: i64, pub balance_bota: i64,
} }
pub struct SaveDepositRequest { pub struct CreateDepositRequest {
pub username: String, pub username: String,
pub cash_type: Option<String>, pub cash_type: Option<String>,
pub amount: i64, pub amount: i64,
@ -52,21 +52,34 @@ pub struct SaveDepositRequest {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct SaveDepositResponse { pub struct _CreateDepositResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub balance: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateDepositResponse {
pub balance: i64, pub balance: i64,
} }
pub struct SaveWithdrawRequest { pub struct CreateWithdrawRequest {
pub username: String, pub username: String,
pub request_key: Option<String>, pub request_key: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct SaveWithdrawResponse { pub struct _CreateWithdrawResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub balance: Option<i64>,
pub balance_cash: Option<i64>,
pub balance_cash_bota: Option<i64>,
pub amount: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateWithdrawResponse {
pub balance: i64, pub balance: i64,
pub balance_cash: i64, pub balance_cash: i64,
pub balance_cash_bota: i64, pub balance_cash_bota: i64,

12
src/api/vendor/api.rs vendored
View File

@ -60,14 +60,12 @@ impl Api {
}); });
} }
if let Some(vendors) = r.vendors { let vendors = match r.vendors {
Some(v) => v,
None => vec![],
};
Ok(models::ListVendorsResponse { vendors }) Ok(models::ListVendorsResponse { vendors })
} else {
Err(Error {
code: r.code,
msg: r.msg,
})
}
} }
Err(e) => Err(Error { Err(e) => Err(Error {
code: -1, code: -1,

View File

@ -10,6 +10,13 @@ use diesel::{
use prost::Message; use prost::Message;
use std::str::FromStr; 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 { pub struct EventHandler {
connection_broker: nats::asynk::Connection, connection_broker: nats::asynk::Connection,
queue_broker: String, queue_broker: String,
@ -116,7 +123,6 @@ impl EventHandler {
)); ));
} }
}; };
let member_id = uuid::Uuid::from_str(member.id.as_str()).map_err(|e| { let member_id = uuid::Uuid::from_str(member.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid member.id param".to_string(), message: "invalid member.id param".to_string(),
@ -129,7 +135,6 @@ impl EventHandler {
}, },
}) })
})?; })?;
let conn = self.pool.get().map_err(|e| { let conn = self.pool.get().map_err(|e| {
bcr::error::rpc::Error::Server(bcr::error::rpc::Server { bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
code: bpr::protobuf::rpc::Error::SERVER_00, code: bpr::protobuf::rpc::Error::SERVER_00,
@ -138,6 +143,8 @@ impl EventHandler {
}) })
})?; })?;
if let Some(mc) = member.member_class {
if mc.id.eq(MEMBER_CLASS_ID_USER) {
let api_create_res = self let api_create_res = self
.member_api .member_api
.create_member(api::member::models::CreateMemberRequest { .create_member(api::member::models::CreateMemberRequest {
@ -174,6 +181,8 @@ impl EventHandler {
data: None, data: None,
}) })
})?; })?;
}
}
Ok::<(), bcr::error::rpc::Error>(()) Ok::<(), bcr::error::rpc::Error>(())
} }