implemented

This commit is contained in:
병준 박 2022-08-17 17:45:00 +00:00
parent 88fc45b7c8
commit 0db6f75d61
62 changed files with 1757 additions and 412 deletions

6
.vscode/launch.json vendored
View File

@ -23,8 +23,9 @@
"URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran", "URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran",
"URL_BROKER": "nats://192.168.50.200:4222", "URL_BROKER": "nats://192.168.50.200:4222",
"QUEUE_BROKER": "bet.beteran", "QUEUE_BROKER": "bet.beteran",
"K_SECRET": "c23d770b873b2ce95747abc57052beb0", "K_URL": "https://dev-mw.kgonapi.com",
"K_USERNAME": "Turbo77", "K_USERNAME": "Turbo77",
"K_SECRET": "c23d770b873b2ce95747abc57052beb0",
}, },
"args": [], "args": [],
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}"
@ -49,8 +50,9 @@
"URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran", "URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran",
"URL_BROKER": "nats://192.168.50.200:4222", "URL_BROKER": "nats://192.168.50.200:4222",
"QUEUE_BROKER": "bet.beteran", "QUEUE_BROKER": "bet.beteran",
"K_SECRET": "c23d770b873b2ce95747abc57052beb0", "K_URL": "https://dev-mw.kgonapi.com",
"K_USERNAME": "Turbo77", "K_USERNAME": "Turbo77",
"K_SECRET": "c23d770b873b2ce95747abc57052beb0",
}, },
"args": [], "args": [],
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}"

View File

@ -19,6 +19,7 @@ futures = { version = "0", default-features = false, features = [
"async-await", "async-await",
] } ] }
nats = { version = "0" } nats = { version = "0" }
once_cell = { version = "1" }
openssl = { version = "0", features = ["vendored"] } openssl = { version = "0", features = ["vendored"] }
prost = { version = "0" } prost = { version = "0" }
reqwest = { version = "0", features = ["json"] } reqwest = { version = "0", features = ["json"] }

View File

@ -0,0 +1 @@
DROP FUNCTION update_synchronizations;

View File

@ -0,0 +1,16 @@
CREATE OR REPLACE FUNCTION update_synchronizations()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO api_kgon_synchronizations
(item, last_code, synchronized_at)
VALUES
(NEW.item, NEW.code, (extract(epoch from now()) * 1000))
ON CONFLICT (item)
DO UPDATE
SET
last_code = NEW.code,
synchronized_at = (extract(epoch from now()) * 1000);
RETURN NEW;
END;
$$ language 'plpgsql';

View File

@ -0,0 +1,2 @@
CREATE UNIQUE INDEX uidx_api_kgon_synchronizations_item;
DROP TABLE api_kgon_synchronization_members;

View File

@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronizations (
id SERIAL PRIMARY KEY;
item TEXT NOT NULL,
last_code BIGINT NOT NULL,
synchronized_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);
CREATE UNIQUE INDEX uidx_api_kgon_synchronizations_item ON api_kgon_synchronizations (item);

View File

@ -0,0 +1,3 @@
DROP TRIGGER tg_api_kgon_synchronization_history_synchronizations;
DROP INDEX idx_api_kgon_synchronization_history_item;
DROP TABLE api_kgon_synchronization_members;

View File

@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronization_history (
id SERIAL PRIMARY KEY;
item TEXT NOT NULL,
start_at BIGINT NOT NULL,
complete_at BIGINT NOT NULL,
code BIGINT NOT NULL,
message TEXT,
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);
CREATE INDEX idx_api_kgon_synchronization_history_item ON api_kgon_synchronization_history (item);
-- trigger (synchronized_at)
CREATE TRIGGER tg_api_kgon_synchronization_history_synchronizations
BEFORE UPDATE
ON api_kgon_synchronization_history
FOR EACH ROW
EXECUTE PROCEDURE update_synchronizations();

View File

@ -22,6 +22,3 @@ CREATE TRIGGER tg_api_kgon_members_updated_at
ON api_kgon_members ON api_kgon_members
FOR EACH ROW FOR EACH ROW
EXECUTE PROCEDURE update_updated_at_column(); EXECUTE PROCEDURE update_updated_at_column();

View File

@ -1 +0,0 @@
DROP TABLE api_kgon_synchronization_members;

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronization_members (
id BIGSERIAL PRIMARY KEY;
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);

View File

@ -1,2 +0,0 @@
DROP TRIGGER tg_api_kgon_balances_updated_at;
DROP TABLE api_kgon_balances;

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronization_balances (
id BIGSERIAL PRIMARY KEY;
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);

View File

@ -0,0 +1,6 @@
DROP INDEX idx_api_kgon_vendors_company_id;
DROP INDEX idx_api_kgon_vendors_vendor_id;
DROP INDEX idx_api_kgon_vendors_key;
DROP INDEX idx_api_kgon_vendors_category;
DROP TRIGGER tg_api_kgon_vendors_updated_at;
DROP TABLE api_kgon_vendors;

View File

@ -4,19 +4,21 @@ CREATE TABLE IF NOT EXISTS api_kgon_vendors (
vendor_id BIGINT NOT NULL, vendor_id BIGINT NOT NULL,
max_bet_casino BIGINT NOT NULL, max_bet_casino BIGINT NOT NULL,
max_bet_slot BIGINT NOT NULL, max_bet_slot BIGINT NOT NULL,
is_enable BOOLEAN NOT NULL, is_enable CHAR(1) NOT NULL,
bet_count BIGINT NOT NULL, bet_count BIGINT NOT NULL,
key TEXT NOT NULL, key TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
category TEXT NOT NULL, category TEXT NOT NULL,
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
PRIMARY KEY (id), PRIMARY KEY (id)
CONSTRAINT fk_api_kgon_vendors_parent_id
FOREIGN KEY(parent_id)
REFERENCES roles(id)
); );
CREATE INDEX idx_api_kgon_vendors_company_id ON api_kgon_vendors (company_id);
CREATE INDEX idx_api_kgon_vendors_vendor_id ON api_kgon_vendors (vendor_id);
CREATE INDEX idx_api_kgon_vendors_key ON api_kgon_vendors (key);
CREATE INDEX idx_api_kgon_vendors_category ON api_kgon_vendors (category);
-- trigger (updated_at) -- trigger (updated_at)
CREATE TRIGGER tg_api_kgon_vendors_updated_at CREATE TRIGGER tg_api_kgon_vendors_updated_at
BEFORE UPDATE BEFORE UPDATE

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronization_vendors (
id BIGSERIAL PRIMARY KEY;
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);

View File

@ -0,0 +1,6 @@
DROP INDEX idx_api_kgon_games_vendor_id;
DROP INDEX idx_api_kgon_games_key;
DROP INDEX idx_api_kgon_games_category;
DROP INDEX idx_api_kgon_games_game_type;
DROP TRIGGER tg_api_kgon_games_updated_at;
DROP TABLE api_kgon_games;

View File

@ -15,6 +15,11 @@ CREATE TABLE IF NOT EXISTS api_kgon_games (
REFERENCES api_kgon_vendors(id) REFERENCES api_kgon_vendors(id)
); );
CREATE INDEX idx_api_kgon_games_vendor_id ON api_kgon_games (vendor_id);
CREATE INDEX idx_api_kgon_games_key ON api_kgon_games (key);
CREATE INDEX idx_api_kgon_games_category ON api_kgon_games (category);
CREATE INDEX idx_api_kgon_games_game_type ON api_kgon_games (game_type);
-- trigger (updated_at) -- trigger (updated_at)
CREATE TRIGGER tg_api_kgon_games_updated_at CREATE TRIGGER tg_api_kgon_games_updated_at
BEFORE UPDATE BEFORE UPDATE

View File

@ -1,4 +0,0 @@
CREATE TABLE IF NOT EXISTS api_kgon_synchronization_games (
id BIGSERIAL PRIMARY KEY;
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000)
);

View File

@ -1,11 +1,11 @@
use super::models; use super::models;
use std::{collections::HashMap, time::Duration}; use crate::core;
use std::collections::HashMap;
/// ///
pub struct Api { pub struct Api {
client: reqwest::Client, client: reqwest::Client,
k_secret: String, api_config: core::config::ApiConfig,
k_username: String,
} }
impl std::fmt::Debug for Api { impl std::fmt::Debug for Api {
@ -16,11 +16,10 @@ impl std::fmt::Debug for Api {
impl Api { impl Api {
/// ///
pub fn new(k_secret: &str, k_username: &str) -> Api { pub fn new(api_config: core::config::ApiConfig) -> Api {
Api { Api {
client: reqwest::Client::new(), client: reqwest::Client::new(),
k_secret: k_secret.to_string(), api_config,
k_username: k_username.to_string(),
} }
} }
@ -51,14 +50,14 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/transaction") .post(format!("{}/transaction", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.send() .send()
.await .await
{ {
@ -88,14 +87,14 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/pragmatic/history") .post(format!("{}/pragmatic/history", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await
@ -126,14 +125,17 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/evolution/transaction/detail") .post(format!(
"{}/evolution/transaction/detail",
self.api_config.k_url
))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await
@ -175,14 +177,14 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/statistic") .post(format!("{}/statistic", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await

1
src/api/core/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod models;

5
src/api/core/models.rs Normal file
View File

@ -0,0 +1,5 @@
#[derive(Debug)]
pub struct Error {
pub code: i64,
pub msg: Option<String>,
}

View File

@ -1,11 +1,12 @@
use super::models; use super::models;
use crate::api::core::models::Error;
use crate::core;
use std::{collections::HashMap, time::Duration}; use std::{collections::HashMap, time::Duration};
/// ///
pub struct Api { pub struct Api {
client: reqwest::Client, client: reqwest::Client,
k_secret: String, api_config: core::config::ApiConfig,
k_username: String,
} }
impl std::fmt::Debug for Api { impl std::fmt::Debug for Api {
@ -16,90 +17,71 @@ impl std::fmt::Debug for Api {
impl Api { impl Api {
/// ///
pub fn new(k_secret: &str, k_username: &str) -> Api { pub fn new(api_config: core::config::ApiConfig) -> Api {
Api { Api {
client: reqwest::Client::new(), client: reqwest::Client::new(),
k_secret: k_secret.to_string(), api_config,
k_username: k_username.to_string(),
} }
} }
/// ///
pub async fn find_vendors( pub async fn list_games(
&self, &self,
data: models::FindVendorsRequest, data: models::ListGamesRequest,
) -> Result<models::FindVendorsResponse, models::Error> { ) -> Result<models::ListGamesResponse, Error> {
let res = match self
.client
.post("https://dev-mw.kgonapi.com/vendors")
.header(reqwest::header::ACCEPT, "application/json")
.header(
reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header("k-secret", self.k_secret.as_str())
.header("k-username", self.k_username.as_str())
.send()
.await
{
Ok(res) => res,
Err(err) => {
return Err(models::Error { code: 0, msg: None });
}
};
match res.status() {
reqwest::StatusCode::OK => match res.json::<models::FindVendorsResponse>().await {
Ok(r) => Ok(r),
Err(e) => Err(models::Error { code: 0, msg: None }),
},
_ => Err(models::Error { code: 0, msg: None }),
}
}
///
pub async fn find_games(
&self,
data: models::FindGamesRequest,
) -> Result<models::FindGamesResponse, models::Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("vendorKey", data.vendor_key); params.insert("vendorKey", data.vendor_key);
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/games") .post(format!("{}/games", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.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::FindGamesResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_ListGamesResponse>().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,
});
}
Ok(models::ListGamesResponse { games: r.games })
}
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 play( pub async fn play(&self, data: models::PlayRequest) -> Result<models::PlayResponse, Error> {
&self,
data: models::PlayRequest,
) -> Result<models::PlayResponse, models::Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("vendorKey", data.vendor_key); params.insert("vendorKey", data.vendor_key);
@ -120,31 +102,53 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/play") .post(format!("{}/play", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.send() .send()
.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::PlayResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_PlayResponse>().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,
});
}
Ok(models::PlayResponse {
user_id: r.user_id,
url: r.url,
balance: r.balance,
})
}
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,28 +1,32 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub struct Error {
pub code: i64,
pub msg: Option<String>,
}
pub struct FindVendorsRequest {}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindVendorsResponse { pub struct Game {
pub code: i64, pub id: i64,
pub msg: Option<String>, pub key: String,
pub vendors: Vec<String>, pub names: HashMap<String, String>,
pub platform: String,
pub category: String,
#[serde(rename = "type")]
pub game_type: String,
pub image: Option<String>,
} }
pub struct FindGamesRequest { pub struct ListGamesRequest {
pub vendor_key: String, pub vendor_key: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindGamesResponse { pub struct _ListGamesResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub games: Vec<String>, pub games: Vec<Game>,
}
#[derive(Debug)]
pub struct ListGamesResponse {
pub games: Vec<Game>,
} }
pub struct PlayRequest { pub struct PlayRequest {
@ -37,7 +41,7 @@ pub struct PlayRequest {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct PlayResponse { pub struct _PlayResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
#[serde(rename = "userId")] #[serde(rename = "userId")]
@ -45,3 +49,10 @@ pub struct PlayResponse {
pub url: String, pub url: String,
pub balance: i64, pub balance: i64,
} }
#[derive(Debug)]
pub struct PlayResponse {
pub user_id: String,
pub url: String,
pub balance: i64,
}

View File

@ -1,12 +1,12 @@
use super::models; use super::models;
use crate::api::core::models::Error;
use crate::core;
use std::collections::HashMap; use std::collections::HashMap;
/// ///
pub struct Api { pub struct Api {
client: reqwest::Client, client: reqwest::Client,
url: String, api_config: core::config::ApiConfig,
k_secret: String,
k_username: String,
} }
impl std::fmt::Debug for Api { impl std::fmt::Debug for Api {
@ -17,12 +17,10 @@ impl std::fmt::Debug for Api {
impl Api { impl Api {
/// ///
pub fn new(url: &str, k_secret: &str, k_username: &str) -> Api { pub fn new(api_config: core::config::ApiConfig) -> Api {
Api { Api {
client: reqwest::Client::new(), client: reqwest::Client::new(),
url: url.to_string(), api_config,
k_secret: k_secret.to_string(),
k_username: k_username.to_string(),
} }
} }
@ -30,7 +28,7 @@ impl Api {
pub async fn list_members( pub async fn list_members(
&self, &self,
data: models::ListMembersRequest, data: models::ListMembersRequest,
) -> Result<models::ListMembersResponse, models::Error> { ) -> Result<models::ListMembersResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
if let Some(group_key) = data.group_key { if let Some(group_key) = data.group_key {
@ -39,30 +37,48 @@ impl Api {
let res = match self let res = match self
.client .client
.post(format!("{}/users", self.url)) .post(format!("{}/users", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.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::ListMembersResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_ListMembersResponse>().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,
});
}
Ok(models::ListMembersResponse { users: r.users })
}
Err(e) => Err(Error {
code: -1,
msg: Some(e.to_string()),
}),
}, },
_ => Err(models::Error { code: 0, msg: None }), _ => Err(Error {
code: -1,
msg: None,
}),
} }
} }
@ -70,7 +86,7 @@ impl Api {
pub async fn save_member( pub async fn save_member(
&self, &self,
data: models::SaveMemberRequest, data: models::SaveMemberRequest,
) -> Result<models::SaveMemberResponse, models::Error> { ) -> Result<models::SaveMemberResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
@ -83,65 +99,30 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/register") .post(format!("{}/register", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error { code: 0, msg: None });
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::SaveMemberResponse>().await { reqwest::StatusCode::OK => match res.json::<models::SaveMemberResponse>().await {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(e) => Err(models::Error { code: 0, msg: None }), Err(e) => Err(Error { code: 0, msg: None }),
}, },
_ => Err(models::Error { code: 0, msg: None }), _ => Err(Error { code: 0, msg: None }),
} }
} }
} }
#[cfg(test)]
mod tests {
use super::super::models;
use super::*;
#[tokio::test]
async fn find_members() {
let repository = Api::new("c23d770b873b2ce95747abc57052beb0", "Turbo77");
let r = repository
.find_members(models::FindMembersRequest { group_key: None })
.await
.expect("");
println!("members: {:?}", r);
}
#[tokio::test]
async fn save_member() {
let repository = Api::new("c23d770b873b2ce95747abc57052beb0", "Turbo77");
let r = repository
.save_member(models::SaveMemberRequest {
username: "".to_string(),
nickname: "".to_string(),
site_username: "".to_string(),
group_key: None,
})
.await
.expect("");
println!("member: {:?}", r);
}
}

View File

@ -1,18 +1,12 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Error {
pub code: i64,
pub msg: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ListMembersRequest { pub struct ListMembersRequest {
pub group_key: Option<String>, pub group_key: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindMember { pub struct Member {
pub id: i64, pub id: i64,
pub username: String, pub username: String,
pub cash: i64, pub cash: i64,
@ -20,14 +14,19 @@ pub struct FindMember {
pub nickname: String, pub nickname: String,
pub site_username: String, pub site_username: String,
pub group_key: Option<String>, pub group_key: Option<String>,
pub oriental_play: Option<String>, pub oriental_play: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ListMembersResponse { pub struct _ListMembersResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub users: Vec<FindMember>, pub users: Vec<Member>,
}
#[derive(Debug)]
pub struct ListMembersResponse {
pub users: Vec<Member>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View File

@ -1,11 +1,12 @@
use super::models; use super::models;
use crate::api::core::models::Error;
use crate::core;
use std::collections::HashMap; use std::collections::HashMap;
/// ///
pub struct Api { pub struct Api {
client: reqwest::Client, client: reqwest::Client,
k_secret: String, api_config: core::config::ApiConfig,
k_username: String,
} }
impl std::fmt::Debug for Api { impl std::fmt::Debug for Api {
@ -16,82 +17,125 @@ impl std::fmt::Debug for Api {
impl Api { impl Api {
/// ///
pub fn new(k_secret: &str, k_username: &str) -> Api { pub fn new(api_config: core::config::ApiConfig) -> Api {
Api { Api {
client: reqwest::Client::new(), client: reqwest::Client::new(),
k_secret: k_secret.to_string(), api_config,
k_username: k_username.to_string(),
} }
} }
/// ///
pub async fn find_balance( pub async fn get_balance_for_user(
&self, &self,
data: models::FindBalanceRequest, data: models::GetBalanceForUserRequest,
) -> Result<models::FindBalanceResponse, models::Error> { ) -> Result<models::GetBalanceForUserResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/balance") .post(format!("{}/balance", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.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::FindBalanceResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_GetBalanceForUserResponse>().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,
});
}
Ok(models::GetBalanceForUserResponse {
balance: r.balance,
balance_bota: r.balance_bota,
balance_sum: r.balance_sum,
companies: r.companies,
})
}
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_partner_balance( pub async fn get_balance_for_partner(
&self, &self,
data: models::FindPartnerBalanceRequest, data: models::GetBalanceForPartnerRequest,
) -> Result<models::FindPartnerBalanceResponse, models::Error> { ) -> Result<models::GetBalanceForPartnerResponse, Error> {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/partner/balance") .post(format!("{}/partner/balance", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.send() .send()
.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::FindPartnerBalanceResponse>().await { reqwest::StatusCode::OK => match res.json::<models::_GetBalanceForPartnerResponse>().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,
});
}
Ok(models::GetBalanceForPartnerResponse {
balance: r.balance,
balance_bota: r.balance_bota,
})
}
Err(e) => Err(Error {
code: -1,
msg: Some(e.to_string()),
}),
}, },
_ => Err(models::Error { code: 0, msg: None }), _ => Err(Error {
code: -1,
msg: None,
}),
} }
} }
@ -99,7 +143,7 @@ impl Api {
pub async fn save_deposit( pub async fn save_deposit(
&self, &self,
data: models::SaveDepositRequest, data: models::SaveDepositRequest,
) -> Result<models::SaveDepositResponse, models::Error> { ) -> Result<models::SaveDepositResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
@ -113,30 +157,30 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/deposit") .post(format!("{}/deposit", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error { code: 0, msg: None });
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::SaveDepositResponse>().await { reqwest::StatusCode::OK => match res.json::<models::SaveDepositResponse>().await {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(e) => Err(models::Error { code: 0, msg: None }), Err(e) => Err(Error { code: 0, msg: None }),
}, },
_ => Err(models::Error { code: 0, msg: None }), _ => Err(Error { code: 0, msg: None }),
} }
} }
@ -144,7 +188,7 @@ impl Api {
pub async fn save_withdraw( pub async fn save_withdraw(
&self, &self,
data: models::SaveWithdrawRequest, data: models::SaveWithdrawRequest,
) -> Result<models::SaveWithdrawResponse, models::Error> { ) -> Result<models::SaveWithdrawResponse, Error> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("username", data.username); params.insert("username", data.username);
@ -154,30 +198,30 @@ impl Api {
let res = match self let res = match self
.client .client
.post("https://dev-mw.kgonapi.com/withdraw") .post(format!("{}/withdraw", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json") .header(reqwest::header::ACCEPT, "application/json")
.header( .header(
reqwest::header::CONTENT_TYPE, reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )
.header("k-secret", self.k_secret.as_str()) .header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.k_username.as_str()) .header("k-username", self.api_config.k_username.as_str())
.form(&params) .form(&params)
.send() .send()
.await .await
{ {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
return Err(models::Error { code: 0, msg: None }); return Err(Error { code: 0, msg: None });
} }
}; };
match res.status() { match res.status() {
reqwest::StatusCode::OK => match res.json::<models::SaveWithdrawResponse>().await { reqwest::StatusCode::OK => match res.json::<models::SaveWithdrawResponse>().await {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(e) => Err(models::Error { code: 0, msg: None }), Err(e) => Err(Error { code: 0, msg: None }),
}, },
_ => Err(models::Error { code: 0, msg: None }), _ => Err(Error { code: 0, msg: None }),
} }
} }
} }

View File

@ -1,16 +1,11 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub struct Error { pub struct GetBalanceForUserRequest {
pub code: i64,
pub msg: Option<String>,
}
pub struct FindBalanceRequest {
pub username: String, pub username: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindBalanceResponse { pub struct _GetBalanceForUserResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub balance: i64, pub balance: i64,
@ -19,16 +14,30 @@ pub struct FindBalanceResponse {
pub companies: i64, pub companies: i64,
} }
pub struct FindPartnerBalanceRequest {} #[derive(Serialize, Deserialize, Debug)]
pub struct GetBalanceForUserResponse {
pub balance: i64,
pub balance_bota: i64,
pub balance_sum: i64,
pub companies: i64,
}
pub struct GetBalanceForPartnerRequest {}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FindPartnerBalanceResponse { pub struct _GetBalanceForPartnerResponse {
pub code: i64, pub code: i64,
pub msg: Option<String>, pub msg: Option<String>,
pub balance: i64, pub balance: i64,
pub balance_bota: i64, pub balance_bota: i64,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct GetBalanceForPartnerResponse {
pub balance: i64,
pub balance_bota: i64,
}
pub struct SaveDepositRequest { pub struct SaveDepositRequest {
pub username: String, pub username: String,
pub cash_type: Option<String>, pub cash_type: Option<String>,

View File

@ -1,4 +1,6 @@
pub mod betting; pub mod betting;
pub mod core;
pub mod game; pub mod game;
pub mod member; pub mod member;
pub mod member_account; pub mod member_account;
pub mod vendor;

76
src/api/vendor/api.rs vendored Normal file
View File

@ -0,0 +1,76 @@
use super::models;
use crate::api::core::models::Error;
use crate::core;
///
pub struct Api {
client: reqwest::Client,
api_config: core::config::ApiConfig,
}
impl std::fmt::Debug for Api {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Api of api.kgon.identity").finish()
}
}
impl Api {
///
pub fn new(api_config: core::config::ApiConfig) -> Api {
Api {
client: reqwest::Client::new(),
api_config,
}
}
///
pub async fn list_vendors(
&self,
data: models::ListVendorsRequest,
) -> Result<models::ListVendorsResponse, Error> {
let res = match self
.client
.post(format!("{}/vendors", self.api_config.k_url))
.header(reqwest::header::ACCEPT, "application/json")
.header(
reqwest::header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header("k-secret", self.api_config.k_secret.as_str())
.header("k-username", self.api_config.k_username.as_str())
.send()
.await
{
Ok(res) => res,
Err(e) => {
return Err(Error {
code: -1,
msg: Some(e.to_string()),
});
}
};
match res.status() {
reqwest::StatusCode::OK => match res.json::<models::_ListVendorsResponse>().await {
Ok(r) => {
if r.code != 0 {
return Err(Error {
code: r.code,
msg: r.msg,
});
}
Ok(models::ListVendorsResponse { vendors: r.vendors })
}
Err(e) => Err(Error {
code: -1,
msg: Some(e.to_string()),
}),
},
_ => Err(Error {
code: -1,
msg: None,
}),
}
}
}

7
src/api/vendor/mod.rs vendored Normal file
View File

@ -0,0 +1,7 @@
//!
//!
///
pub mod api;
///
pub mod models;

29
src/api/vendor/models.rs vendored Normal file
View File

@ -0,0 +1,29 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Vendor {
pub id: i64,
pub company_id: i64,
pub vendor_id: i64,
pub max_bet_casino: i64,
pub max_bet_slot: i64,
pub is_enable: String,
pub bet_count: i64,
pub key: String,
pub name: String,
pub category: String,
}
pub struct ListVendorsRequest {}
#[derive(Serialize, Deserialize, Debug)]
pub struct _ListVendorsResponse {
pub code: i64,
pub msg: Option<String>,
pub vendors: Vec<Vendor>,
}
#[derive(Debug)]
pub struct ListVendorsResponse {
pub vendors: Vec<Vendor>,
}

6
src/core/config.rs Normal file
View File

@ -0,0 +1,6 @@
#[derive(Debug, Clone)]
pub struct ApiConfig {
pub k_url: String,
pub k_username: String,
pub k_secret: String,
}

1
src/core/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod config;

View File

@ -13,6 +13,7 @@ use std::env;
mod api; mod api;
mod compositions; mod compositions;
mod core;
mod repositories; mod repositories;
mod schedulers; mod schedulers;
mod services; mod services;
@ -35,7 +36,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
None => "".to_string(), None => "".to_string(),
}; };
let k_secret = match env::var_os("K_SECRET") { let k_url = match env::var_os("K_URL") {
Some(v) => v.into_string().unwrap(), Some(v) => v.into_string().unwrap(),
None => "".to_string(), None => "".to_string(),
}; };
@ -43,8 +44,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(v) => v.into_string().unwrap(), Some(v) => v.into_string().unwrap(),
None => "".to_string(), None => "".to_string(),
}; };
let k_secret = match env::var_os("K_SECRET") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let mut sched = tokio_cron_scheduler::JobScheduler::new().await?; let api_config = core::config::ApiConfig {
k_url,
k_username,
k_secret,
};
let manager = ConnectionManager::<PgConnection>::new(url_db); let manager = ConnectionManager::<PgConnection>::new(url_db);
let pool = Pool::builder() let pool = Pool::builder()
@ -61,35 +70,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let member_service = services::member::service::Service::new( let member_service = services::member::service::Service::new(
connection_server_broker.clone(), connection_server_broker.clone(),
queue_server_broker.clone(), queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
); );
let member_account_service = services::member_account::service::Service::new( let member_account_service = services::member_account::service::Service::new(
connection_server_broker.clone(), connection_server_broker.clone(),
queue_server_broker.clone(), queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
); );
let game_service = services::game::service::Service::new( let game_service = services::game::service::Service::new(
connection_server_broker.clone(), connection_server_broker.clone(),
queue_server_broker.clone(), queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
); );
let betting_service = services::betting::service::Service::new( let betting_service = services::betting::service::Service::new(
connection_server_broker.clone(), connection_server_broker.clone(),
queue_server_broker.clone(), queue_server_broker.clone(),
k_secret.clone(),
k_username.clone(),
); );
let vendor_scheduler = schedulers::vendor::scheduler::Scheduler::new(pool.clone(), sched.clone()); let mut sched = tokio_cron_scheduler::JobScheduler::new().await?;
vendor_scheduler.queue().await;
let start = sched.start().await; let member_scheduler = schedulers::member::scheduler::Scheduler::get_instance(
if start.is_err() { pool.clone(),
panic!("Error starting scheduler"); sched.clone(),
} api_config.clone(),
)?;
member_scheduler.queue().await;
let balance_scheduler = schedulers::balance::scheduler::Scheduler::get_instance(
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
balance_scheduler.queue().await;
let vendor_scheduler = schedulers::vendor::scheduler::Scheduler::get_instance(
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
vendor_scheduler.queue().await;
let game_scheduler = schedulers::game::scheduler::Scheduler::get_instance(
pool.clone(),
sched.clone(),
api_config.clone(),
)?;
game_scheduler.queue().await;
let _h_scheduler = sched.start().await?;
println!("Server service [beteran-api-kgon-server-service] is started"); println!("Server service [beteran-api-kgon-server-service] is started");

View File

@ -67,6 +67,28 @@ pub struct ModifyGame {
pub image: Option<String>, pub image: Option<String>,
} }
///
#[derive(Insertable, AsChangeset, Debug, Clone)]
#[table_name = "api_kgon_games"]
pub struct UpsertGame {
///
pub id: i64,
///
pub vendor_id: i64,
///
pub key: String,
///
pub names: String,
///
pub platform: String,
///
pub category: String,
///
pub game_type: String,
///
pub image: Option<String>,
}
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FindAllSearch { pub struct FindAllSearch {

View File

@ -39,6 +39,33 @@ impl Repository {
Ok(inserted) Ok(inserted)
} }
///
pub fn upserts(
&self,
conn: &diesel::PgConnection,
upsert_games: Vec<models::UpsertGame>,
) -> Result<usize, Error> {
use api_kgon_games::dsl;
use diesel::pg::upsert::excluded;
let affected = diesel::insert_into(api_kgon_games::table)
.values(upsert_games)
.on_conflict(dsl::id)
.do_update()
.set((
dsl::vendor_id.eq(excluded(dsl::vendor_id)),
dsl::key.eq(excluded(dsl::key)),
dsl::names.eq(excluded(dsl::names)),
dsl::platform.eq(excluded(dsl::platform)),
dsl::category.eq(excluded(dsl::category)),
dsl::game_type.eq(excluded(dsl::game_type)),
dsl::image.eq(excluded(dsl::image)),
))
.execute(conn)?;
Ok(affected)
}
/// ///
pub fn select( pub fn select(
&self, &self,
@ -66,16 +93,16 @@ impl Repository {
if let Some(sp) = s.vendor_id { if let Some(sp) = s.vendor_id {
q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp)); q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp));
} }
if let Some(sp) = s.key_like { if let Some(sp) = &s.key_like {
q = q.filter(api_kgon_games::dsl::key.like(sp)); q = q.filter(api_kgon_games::dsl::key.like(sp));
} }
if let Some(sp) = s.platform_like { if let Some(sp) = &s.platform_like {
q = q.filter(api_kgon_games::dsl::platform.like(sp)); q = q.filter(api_kgon_games::dsl::platform.like(sp));
} }
if let Some(sp) = s.category_like { if let Some(sp) = &s.category_like {
q = q.filter(api_kgon_games::dsl::category.like(sp)); q = q.filter(api_kgon_games::dsl::category.like(sp));
} }
if let Some(sp) = s.game_type_like { if let Some(sp) = &s.game_type_like {
q = q.filter(api_kgon_games::dsl::game_type.like(sp)); q = q.filter(api_kgon_games::dsl::game_type.like(sp));
} }
} }
@ -95,16 +122,16 @@ impl Repository {
if let Some(sp) = s.vendor_id { if let Some(sp) = s.vendor_id {
q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp)); q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp));
} }
if let Some(sp) = s.key_like { if let Some(sp) = &s.key_like {
q = q.filter(api_kgon_games::dsl::key.like(sp)); q = q.filter(api_kgon_games::dsl::key.like(sp));
} }
if let Some(sp) = s.platform_like { if let Some(sp) = &s.platform_like {
q = q.filter(api_kgon_games::dsl::platform.like(sp)); q = q.filter(api_kgon_games::dsl::platform.like(sp));
} }
if let Some(sp) = s.category_like { if let Some(sp) = &s.category_like {
q = q.filter(api_kgon_games::dsl::category.like(sp)); q = q.filter(api_kgon_games::dsl::category.like(sp));
} }
if let Some(sp) = s.game_type_like { if let Some(sp) = &s.game_type_like {
q = q.filter(api_kgon_games::dsl::game_type.like(sp)); q = q.filter(api_kgon_games::dsl::game_type.like(sp));
} }
} }

View File

@ -16,7 +16,7 @@ pub struct Member {
/// ///
pub companies: i64, pub companies: i64,
/// ///
pub oriental_play: bool, pub oriental_play: String,
/// ///
pub member_id: uuid::Uuid, pub member_id: uuid::Uuid,
/// ///
@ -40,7 +40,7 @@ pub struct NewMember {
/// ///
pub companies: i64, pub companies: i64,
/// ///
pub oriental_play: bool, pub oriental_play: String,
/// ///
pub member_id: uuid::Uuid, pub member_id: uuid::Uuid,
} }
@ -49,6 +49,18 @@ pub struct NewMember {
#[derive(AsChangeset, Debug, Clone)] #[derive(AsChangeset, Debug, Clone)]
#[table_name = "api_kgon_members"] #[table_name = "api_kgon_members"]
pub struct ModifyMember { pub struct ModifyMember {
///
pub balance: i64,
///
pub balance_bota: i64,
///
pub oriental_play: String,
}
///
#[derive(AsChangeset, Debug, Clone)]
#[table_name = "api_kgon_members"]
pub struct ModifyMemberForBalance {
/// ///
pub balance: i64, pub balance: i64,
/// ///
@ -57,15 +69,13 @@ pub struct ModifyMember {
pub balance_sum: i64, pub balance_sum: i64,
/// ///
pub companies: i64, pub companies: i64,
///
pub oriental_play: bool,
} }
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FindAllSearch { pub struct FindAllSearch {
/// ///
pub oriental_play: Option<bool>, pub oriental_play: Option<String>,
} }
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -86,7 +86,7 @@ impl Repository {
let mut q = api_kgon_members::table.into_boxed(); let mut q = api_kgon_members::table.into_boxed();
if let Some(s) = &find_all.search { if let Some(s) = &find_all.search {
if let Some(sp) = s.oriental_play { if let Some(sp) = &s.oriental_play {
q = q.filter(api_kgon_members::dsl::oriental_play.eq(sp)); q = q.filter(api_kgon_members::dsl::oriental_play.eq(sp));
} }
} }
@ -103,7 +103,7 @@ impl Repository {
let mut q = api_kgon_members::table.into_boxed(); let mut q = api_kgon_members::table.into_boxed();
if let Some(s) = &find_all.search { if let Some(s) = &find_all.search {
if let Some(sp) = s.oriental_play { if let Some(sp) = &s.oriental_play {
q = q.filter(api_kgon_members::dsl::oriental_play.eq(sp)); q = q.filter(api_kgon_members::dsl::oriental_play.eq(sp));
} }
} }
@ -189,6 +189,21 @@ impl Repository {
.map(|c| c as u64) .map(|c| c as u64)
} }
///
pub fn update_balance(
&self,
conn: &diesel::PgConnection,
id: i64,
modify: &models::ModifyMemberForBalance,
) -> Result<u64, Error> {
use api_kgon_members::dsl;
diesel::update(dsl::api_kgon_members.filter(dsl::id.eq(id)))
.set(modify)
.execute(conn)
.map(|c| c as u64)
}
/// ///
pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result<u64, Error> { pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result<u64, Error> {
use api_kgon_members::dsl; use api_kgon_members::dsl;

View File

@ -15,7 +15,7 @@ table! {
/// ///
companies -> BigInt, companies -> BigInt,
/// ///
oriental_play -> Bool, oriental_play -> Text,
/// ///
member_id -> Uuid, member_id -> Uuid,
/// ///

View File

@ -1,4 +1,6 @@
pub mod balance; pub mod balance;
pub mod game; pub mod game;
pub mod member; pub mod member;
pub mod synchronization;
pub mod synchronization_history;
pub mod vendor; pub mod vendor;

View File

@ -0,0 +1,9 @@
//!
//!
///
pub mod models;
///
pub mod repository;
///
pub mod schema;

View File

@ -0,0 +1,39 @@
use super::schema::api_kgon_synchronizations;
pub static ITEM_MEMBERS: &str = "members";
pub static ITEM_BALANCE_PARTNER: &str = "balance_partner";
pub static ITEM_BALANCE_USER: &str = "balance_user";
pub static ITEM_VENDORS: &str = "vendors";
pub static ITEM_GAMES: &str = "games";
///
#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)]
#[table_name = "api_kgon_synchronizations"]
pub struct Synchronization {
///
pub id: i64,
///
pub item: String,
///
pub last_code: i64,
///
pub synchronized_at: i64,
}
///
#[derive(Insertable, Debug, Clone)]
#[table_name = "api_kgon_synchronizations"]
pub struct NewSynchronization {
///
pub item: String,
///
pub last_code: i64,
}
///
#[derive(AsChangeset, Debug, Clone)]
#[table_name = "api_kgon_synchronizations"]
pub struct ModifySynchronization {
///
pub last_code: i64,
}

View File

@ -0,0 +1,103 @@
//!
//!
use super::{models, schema::api_kgon_synchronizations};
use diesel::prelude::*;
use diesel::result::Error;
///
pub struct Repository {}
impl std::fmt::Debug for Repository {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Repository of api_kgon_synchronizations")
.finish()
}
}
impl Default for Repository {
fn default() -> Self {
Self::new()
}
}
impl Repository {
///
pub fn new() -> Repository {
Repository {}
}
///
pub fn insert(
&self,
conn: &diesel::PgConnection,
new_member: &models::NewSynchronization,
) -> Result<models::Synchronization, Error> {
let inserted = diesel::insert_into(api_kgon_synchronizations::table)
.values(new_member)
.get_result::<models::Synchronization>(conn)?;
Ok(inserted)
}
///
pub fn select(
&self,
conn: &diesel::PgConnection,
id: i64,
) -> Result<Option<models::Synchronization>, Error> {
match api_kgon_synchronizations::table
.find(id)
.first::<models::Synchronization>(conn)
{
Ok(m) => Ok(Some(m)),
Err(e) => match e {
diesel::result::Error::NotFound => Ok(None),
_ => Err(e),
},
}
}
///
pub fn select_by_item(
&self,
conn: &diesel::PgConnection,
item: String,
) -> Result<Option<models::Synchronization>, Error> {
use api_kgon_synchronizations::dsl;
match api_kgon_synchronizations::table
.filter(dsl::item.eq(item))
.first::<models::Synchronization>(conn)
{
Ok(m) => Ok(Some(m)),
Err(e) => match e {
diesel::result::Error::NotFound => Ok(None),
_ => Err(e),
},
}
}
///
pub fn update(
&self,
conn: &diesel::PgConnection,
id: i64,
modify: &models::ModifySynchronization,
) -> Result<u64, Error> {
use api_kgon_synchronizations::dsl;
diesel::update(dsl::api_kgon_synchronizations.filter(dsl::id.eq(id)))
.set(modify)
.execute(conn)
.map(|c| c as u64)
}
///
pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result<u64, Error> {
use api_kgon_synchronizations::dsl;
diesel::delete(api_kgon_synchronizations::table.filter(dsl::id.eq(id)))
.execute(conn)
.map(|c| c as u64)
}
}

View File

@ -0,0 +1,16 @@
//!
//!
table! {
///
api_kgon_synchronizations(id) {
///
id -> BigInt,
///
item -> Text,
///
last_code -> BigInt,
///
synchronized_at -> BigInt,
}
}

View File

@ -0,0 +1,9 @@
//!
//!
///
pub mod models;
///
pub mod repository;
///
pub mod schema;

View File

@ -0,0 +1,56 @@
use super::schema::api_kgon_synchronization_history;
use beteran_common_rust as bcr;
///
#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)]
#[table_name = "api_kgon_synchronization_history"]
pub struct SynchronizationHistory {
///
pub id: i64,
///
pub item: String,
///
pub start_at: i64,
///
pub complete_at: i64,
///
pub code: i64,
///
pub message: Option<String>,
///
pub created_at: i64,
}
///
#[derive(Insertable, Debug, Clone)]
#[table_name = "api_kgon_synchronization_history"]
pub struct NewSynchronizationHistory {
///
pub item: String,
///
pub start_at: i64,
///
pub complete_at: i64,
///
pub code: i64,
///
pub message: Option<String>,
}
///
#[derive(Debug, Clone)]
pub struct FindAllSearch {
///
pub item: Option<String>,
///
pub code: Option<i64>,
}
///
#[derive(Debug, Clone)]
pub struct FindAll {
pub search: Option<FindAllSearch>,
///
pub pagination: Option<bcr::models::pagination::Pagination>,
///
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
}

View File

@ -0,0 +1,160 @@
//!
//!
use super::{models, schema::api_kgon_synchronization_history};
use beteran_common_rust as bcr;
use diesel::prelude::*;
use diesel::result::Error;
///
pub struct Repository {}
impl std::fmt::Debug for Repository {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Repository of api_kgon_synchronization_history")
.finish()
}
}
impl Default for Repository {
fn default() -> Self {
Self::new()
}
}
impl Repository {
///
pub fn new() -> Repository {
Repository {}
}
///
pub fn insert(
&self,
conn: &diesel::PgConnection,
new_member: &models::NewSynchronizationHistory,
) -> Result<models::SynchronizationHistory, Error> {
let inserted = diesel::insert_into(api_kgon_synchronization_history::table)
.values(new_member)
.get_result::<models::SynchronizationHistory>(conn)?;
Ok(inserted)
}
///
pub fn select(
&self,
conn: &diesel::PgConnection,
id: i64,
) -> Result<Option<models::SynchronizationHistory>, Error> {
match api_kgon_synchronization_history::table
.find(id)
.first::<models::SynchronizationHistory>(conn)
{
Ok(m) => Ok(Some(m)),
Err(e) => match e {
diesel::result::Error::NotFound => Ok(None),
_ => Err(e),
},
}
}
///
pub fn select_all_count(
&self,
conn: &diesel::PgConnection,
find_all: &models::FindAll,
) -> Result<i64, Error> {
let mut q = api_kgon_synchronization_history::table.into_boxed();
if let Some(s) = &find_all.search {
if let Some(sp) = &s.item {
q = q.filter(api_kgon_synchronization_history::dsl::item.eq(sp));
}
if let Some(sp) = s.code {
q = q.filter(api_kgon_synchronization_history::dsl::code.eq(sp));
}
}
q.count().get_result(conn)
}
///
pub fn select_all(
&self,
conn: &diesel::PgConnection,
find_all: &models::FindAll,
) -> Result<Vec<models::SynchronizationHistory>, Error> {
let mut q = api_kgon_synchronization_history::table.into_boxed();
if let Some(s) = &find_all.search {
if let Some(sp) = &s.item {
q = q.filter(api_kgon_synchronization_history::dsl::item.eq(sp));
}
if let Some(sp) = s.code {
q = q.filter(api_kgon_synchronization_history::dsl::code.eq(sp));
}
}
if let Some(p) = &find_all.pagination {
let page = p.page.unwrap_or(1);
if let Some(page_size) = p.page_size {
q = q.offset(((page - 1) * page_size) as i64);
q = q.limit(page_size as i64);
}
}
if let Some(orderbys) = &find_all.sorts {
for s in orderbys {
match s {
bcr::models::pagination::Sort::ASC(property) => match property.as_str() {
"item" => {
q = q.order_by(api_kgon_synchronization_history::item.asc());
}
"start_at" => {
q = q.order_by(api_kgon_synchronization_history::start_at.asc());
}
"complete_at" => {
q = q.order_by(api_kgon_synchronization_history::complete_at.asc());
}
"code" => {
q = q.order_by(api_kgon_synchronization_history::code.asc());
}
"created_at" => {
q = q.order_by(api_kgon_synchronization_history::created_at.asc());
}
_ => {}
},
bcr::models::pagination::Sort::DESC(property) => match property.as_str() {
"item" => {
q = q.order_by(api_kgon_synchronization_history::item.desc());
}
"start_at" => {
q = q.order_by(api_kgon_synchronization_history::start_at.desc());
}
"complete_at" => {
q = q.order_by(api_kgon_synchronization_history::complete_at.desc());
}
"code" => {
q = q.order_by(api_kgon_synchronization_history::code.desc());
}
"created_at" => {
q = q.order_by(api_kgon_synchronization_history::created_at.desc());
}
_ => {}
},
};
}
}
q.load::<models::SynchronizationHistory>(conn)
}
///
pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result<u64, Error> {
use api_kgon_synchronization_history::dsl;
diesel::delete(api_kgon_synchronization_history::table.filter(dsl::id.eq(id)))
.execute(conn)
.map(|c| c as u64)
}
}

View File

@ -0,0 +1,22 @@
//!
//!
table! {
///
api_kgon_synchronization_history(id) {
///
id -> BigInt,
///
item -> Text,
///
start_at -> BigInt,
///
complete_at -> BigInt,
///
code -> BigInt,
///
message -> Nullable<Text>,
///
created_at -> BigInt,
}
}

View File

@ -22,7 +22,7 @@ pub struct Vendor {
/// ///
pub max_bet_slot: i64, pub max_bet_slot: i64,
/// ///
pub is_enable: bool, pub is_enable: String,
/// ///
pub bet_count: i64, pub bet_count: i64,
/// ///
@ -52,7 +52,7 @@ pub struct NewVendor {
/// ///
pub max_bet_slot: i64, pub max_bet_slot: i64,
/// ///
pub is_enable: bool, pub is_enable: String,
/// ///
pub bet_count: i64, pub bet_count: i64,
} }
@ -76,7 +76,33 @@ pub struct ModifyVendor {
/// ///
pub max_bet_slot: i64, pub max_bet_slot: i64,
/// ///
pub is_enable: bool, pub is_enable: String,
///
pub bet_count: i64,
}
///
#[derive(Insertable, AsChangeset, Debug, Clone)]
#[table_name = "api_kgon_vendors"]
pub struct UpsertVendor {
///
pub id: i64,
///
pub company_id: i64,
///
pub vendor_id: i64,
///
pub key: String,
///
pub name: String,
///
pub category: String,
///
pub max_bet_casino: i64,
///
pub max_bet_slot: i64,
///
pub is_enable: String,
/// ///
pub bet_count: i64, pub bet_count: i64,
} }
@ -95,7 +121,7 @@ pub struct FindAllSearch {
/// ///
pub category_like: Option<String>, pub category_like: Option<String>,
/// ///
pub is_enable: Option<bool>, pub is_enable: Option<String>,
} }
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -39,6 +39,35 @@ impl Repository {
Ok(inserted) Ok(inserted)
} }
///
pub fn upserts(
&self,
conn: &diesel::PgConnection,
upsert_vendors: Vec<models::UpsertVendor>,
) -> Result<usize, Error> {
use api_kgon_vendors::dsl;
use diesel::pg::upsert::excluded;
let affected = diesel::insert_into(api_kgon_vendors::table)
.values(upsert_vendors)
.on_conflict(dsl::id)
.do_update()
.set((
dsl::company_id.eq(excluded(dsl::company_id)),
dsl::vendor_id.eq(excluded(dsl::vendor_id)),
dsl::key.eq(excluded(dsl::key)),
dsl::name.eq(excluded(dsl::name)),
dsl::category.eq(excluded(dsl::category)),
dsl::max_bet_casino.eq(excluded(dsl::max_bet_casino)),
dsl::max_bet_slot.eq(excluded(dsl::max_bet_slot)),
dsl::is_enable.eq(excluded(dsl::is_enable)),
dsl::bet_count.eq(excluded(dsl::bet_count)),
))
.execute(conn)?;
Ok(affected)
}
/// ///
pub fn select( pub fn select(
&self, &self,
@ -72,16 +101,16 @@ impl Repository {
if let Some(sp) = s.vendor_id { if let Some(sp) = s.vendor_id {
q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp)); q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp));
} }
if let Some(sp) = s.key_like { if let Some(sp) = &s.key_like {
q = q.filter(api_kgon_vendors::dsl::key.like(sp)); q = q.filter(api_kgon_vendors::dsl::key.like(sp));
} }
if let Some(sp) = s.name_like { if let Some(sp) = &s.name_like {
q = q.filter(api_kgon_vendors::dsl::name.like(sp)); q = q.filter(api_kgon_vendors::dsl::name.like(sp));
} }
if let Some(sp) = s.category_like { if let Some(sp) = &s.category_like {
q = q.filter(api_kgon_vendors::dsl::category.like(sp)); q = q.filter(api_kgon_vendors::dsl::category.like(sp));
} }
if let Some(sp) = s.is_enable { if let Some(sp) = &s.is_enable {
q = q.filter(api_kgon_vendors::dsl::is_enable.eq(sp)); q = q.filter(api_kgon_vendors::dsl::is_enable.eq(sp));
} }
} }
@ -104,16 +133,16 @@ impl Repository {
if let Some(sp) = s.vendor_id { if let Some(sp) = s.vendor_id {
q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp)); q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp));
} }
if let Some(sp) = s.key_like { if let Some(sp) = &s.key_like {
q = q.filter(api_kgon_vendors::dsl::key.like(sp)); q = q.filter(api_kgon_vendors::dsl::key.like(sp));
} }
if let Some(sp) = s.name_like { if let Some(sp) = &s.name_like {
q = q.filter(api_kgon_vendors::dsl::name.like(sp)); q = q.filter(api_kgon_vendors::dsl::name.like(sp));
} }
if let Some(sp) = s.category_like { if let Some(sp) = &s.category_like {
q = q.filter(api_kgon_vendors::dsl::category.like(sp)); q = q.filter(api_kgon_vendors::dsl::category.like(sp));
} }
if let Some(sp) = s.is_enable { if let Some(sp) = &s.is_enable {
q = q.filter(api_kgon_vendors::dsl::is_enable.eq(sp)); q = q.filter(api_kgon_vendors::dsl::is_enable.eq(sp));
} }
} }

View File

@ -21,7 +21,7 @@ table! {
/// ///
max_bet_slot -> BigInt, max_bet_slot -> BigInt,
/// ///
is_enable -> Bool, is_enable -> Text,
/// ///
bet_count -> BigInt, bet_count -> BigInt,
/// ///

View File

@ -1,5 +1,27 @@
use crate::api;
use crate::core;
use crate::repositories;
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tokio_cron_scheduler::{Job, JobScheduler};
static G_INSTANCE: OnceCell<Arc<Scheduler>> = OnceCell::new();
/// ///
pub struct Scheduler {} pub struct Scheduler {
pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
synchronization_history_repository: repositories::synchronization_history::repository::Repository,
balance_repository: repositories::balance::repository::Repository,
member_repository: repositories::member::repository::Repository,
member_api: api::member::api::Api,
member_account_api: api::member_account::api::Api,
}
impl std::fmt::Debug for Scheduler { impl std::fmt::Debug for Scheduler {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -9,7 +31,204 @@ impl std::fmt::Debug for Scheduler {
impl Scheduler { impl Scheduler {
/// ///
pub fn new() -> Scheduler { pub fn get_instance(
Scheduler {} pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
) -> Result<&'static Arc<Scheduler>, Box<dyn std::error::Error>> {
let instance = G_INSTANCE
.get_or_try_init(|| -> Result<Arc<Scheduler>, Box<dyn std::error::Error>> {
let s = Scheduler {
pool,
sched,
api_config: api_config.clone(),
synchronization_history_repository:
repositories::synchronization_history::repository::Repository::new(),
balance_repository: repositories::balance::repository::Repository::new(),
member_repository: repositories::member::repository::Repository::new(),
member_api: api::member::api::Api::new(api_config.clone()),
member_account_api: api::member_account::api::Api::new(api_config.clone()),
};
Ok(Arc::new(s))
})
.expect("");
Ok(instance)
}
pub async fn queue(&'static self) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
self.balance_for_user().await?;
self.balance_for_partner().await?;
Ok(())
}
async fn add_history(
&'static self,
conn: &diesel::PgConnection,
item: String,
start_at: i64,
code: i64,
message: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let complete_at = (chrono::Utc::now()).timestamp();
self
.synchronization_history_repository
.insert(
conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item,
start_at,
complete_at,
code,
message,
},
)
.expect("synchronization_history insert");
Ok(())
}
async fn balance_for_user(&'static self) -> Result<(), Box<dyn std::error::Error>> {
let j_synchronization = Job::new_async("0 0 0/1 * * *", move |_uuid, _l| {
Box::pin(async move {
let start_at = (chrono::Utc::now()).timestamp();
let conn = self.pool.get().expect("conn");
let req = api::member::models::ListMembersRequest { group_key: None };
let res = match self.member_api.list_members(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("list_members");
for u in res.users {
let req = api::member_account::models::GetBalanceForUserRequest {
username: u.site_username,
};
let res = match self.member_account_api.get_balance_for_user(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("get_balance_for_user");
let modify_member = repositories::member::models::ModifyMemberForBalance {
balance: res.balance,
balance_bota: res.balance_bota,
balance_sum: res.balance_sum,
companies: res.companies,
};
self
.member_repository
.update_balance(&conn, u.id, &modify_member)
.expect("member update_balance");
}
self.add_history(
&conn,
repositories::synchronization::models::ITEM_BALANCE_USER.to_string(),
start_at,
0,
None,
);
})
})?;
self.sched.add(j_synchronization).await;
Ok(())
}
async fn balance_for_partner(&'static self) -> Result<(), Box<dyn std::error::Error>> {
let j_synchronization = Job::new_async("0 0 0/1 * * *", move |_uuid, _l| {
Box::pin(async move {
let start_at = (chrono::Utc::now()).timestamp();
let conn = self.pool.get().expect("conn");
let req = api::member_account::models::GetBalanceForPartnerRequest {};
let res = match self.member_account_api.get_balance_for_partner(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_BALANCE_PARTNER.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("list_members");
match self
.balance_repository
.select(&conn)
.expect("balance select")
{
Some(b) => {
self
.balance_repository
.update(
&conn,
b.id,
&repositories::balance::models::ModifyBalance {
balance: res.balance,
balance_bota: res.balance_bota,
},
)
.expect("balance update");
}
None => {
self
.balance_repository
.insert(
&conn,
&repositories::balance::models::NewBalance {
balance: res.balance,
balance_bota: res.balance_bota,
},
)
.expect("balance insert");
}
}
self.add_history(
&conn,
repositories::synchronization::models::ITEM_BALANCE_PARTNER.to_string(),
start_at,
0,
None,
);
})
})?;
self.sched.add(j_synchronization).await;
Ok(())
} }
} }

View File

@ -1,5 +1,26 @@
use crate::api;
use crate::core;
use crate::repositories;
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tokio_cron_scheduler::{Job, JobScheduler};
static G_INSTANCE: OnceCell<Arc<Scheduler>> = OnceCell::new();
/// ///
pub struct Scheduler {} pub struct Scheduler {
pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
synchronization_history_repository: repositories::synchronization_history::repository::Repository,
vendor_repository: repositories::vendor::repository::Repository,
game_repository: repositories::game::repository::Repository,
game_api: api::game::api::Api,
}
impl std::fmt::Debug for Scheduler { impl std::fmt::Debug for Scheduler {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -9,7 +30,135 @@ impl std::fmt::Debug for Scheduler {
impl Scheduler { impl Scheduler {
/// ///
pub fn new() -> Scheduler { pub fn get_instance(
Scheduler {} pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
) -> Result<&'static Arc<Scheduler>, Box<dyn std::error::Error>> {
let instance = G_INSTANCE
.get_or_try_init(|| -> Result<Arc<Scheduler>, Box<dyn std::error::Error>> {
let s = Scheduler {
pool,
sched,
api_config: api_config.clone(),
synchronization_history_repository:
repositories::synchronization_history::repository::Repository::new(),
vendor_repository: repositories::vendor::repository::Repository::new(),
game_repository: repositories::game::repository::Repository::new(),
game_api: api::game::api::Api::new(api_config.clone()),
};
Ok(Arc::new(s))
})
.expect("");
Ok(instance)
}
pub async fn queue(&'static self) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
self.list_vendors().await?;
Ok(())
}
async fn add_history(
&'static self,
conn: &diesel::PgConnection,
item: String,
start_at: i64,
code: i64,
message: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let complete_at = (chrono::Utc::now()).timestamp();
self
.synchronization_history_repository
.insert(
conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item,
start_at,
complete_at,
code,
message,
},
)
.expect("synchronization_history insert");
Ok(())
}
async fn list_vendors(&'static self) -> Result<(), Box<dyn std::error::Error>> {
let j_synchronization = Job::new_async("0 0 0/1 * * *", move |_uuid, _l| {
Box::pin(async move {
let start_at = (chrono::Utc::now()).timestamp();
let conn = self.pool.get().expect("conn");
let vendors = self
.vendor_repository
.select_all(
&conn,
&repositories::vendor::models::FindAll {
pagination: None,
sorts: None,
search: None,
},
)
.expect("vendor select_all");
let mut upsert_games: Vec<repositories::game::models::UpsertGame> = vec![];
for v in vendors {
let req = api::game::models::ListGamesRequest {
vendor_key: v.key.clone(),
};
let res = match self.game_api.list_games(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_VENDORS.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("list_games");
for g in res.games {
upsert_games.push(repositories::game::models::UpsertGame {
id: g.id,
vendor_id: v.id,
key: g.key.clone(),
names: serde_json::to_string(&g.names).expect("names"),
platform: g.platform.clone(),
category: g.category.clone(),
game_type: g.game_type.clone(),
image: g.image,
});
}
}
let affected = self
.game_repository
.upserts(&conn, upsert_games)
.expect("game upsert");
self.add_history(
&conn,
repositories::synchronization::models::ITEM_GAMES.to_string(),
start_at,
0,
None,
);
})
})?;
self.sched.add(j_synchronization).await;
Ok(())
} }
} }

View File

@ -1,7 +1,25 @@
use tokio_cron_scheduler::{Job, JobScheduler, JobToRun}; use crate::api;
use crate::core;
use crate::repositories;
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tokio_cron_scheduler::{Job, JobScheduler};
static G_INSTANCE: OnceCell<Arc<Scheduler>> = OnceCell::new();
/// ///
pub struct Scheduler {} pub struct Scheduler {
pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
synchronization_history_repository: repositories::synchronization_history::repository::Repository,
member_repository: repositories::member::repository::Repository,
member_api: api::member::api::Api,
}
impl std::fmt::Debug for Scheduler { impl std::fmt::Debug for Scheduler {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -11,9 +29,111 @@ impl std::fmt::Debug for Scheduler {
impl Scheduler { impl Scheduler {
/// ///
pub fn new() -> Scheduler { pub fn get_instance(
let mut sched = JobScheduler::new(); pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
) -> Result<&'static Arc<Scheduler>, Box<dyn std::error::Error>> {
let instance = G_INSTANCE
.get_or_try_init(|| -> Result<Arc<Scheduler>, Box<dyn std::error::Error>> {
let s = Scheduler {
pool,
sched,
api_config: api_config.clone(),
synchronization_history_repository:
repositories::synchronization_history::repository::Repository::new(),
member_repository: repositories::member::repository::Repository::new(),
member_api: api::member::api::Api::new(api_config.clone()),
};
Scheduler {} Ok(Arc::new(s))
})
.expect("");
Ok(instance)
}
pub async fn queue(&'static self) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
self.list_members().await?;
Ok(())
}
async fn add_history(
&'static self,
conn: &diesel::PgConnection,
item: String,
start_at: i64,
code: i64,
message: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let complete_at = (chrono::Utc::now()).timestamp();
self
.synchronization_history_repository
.insert(
conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item,
start_at,
complete_at,
code,
message,
},
)
.expect("synchronization_history insert");
Ok(())
}
async fn list_members(&'static self) -> Result<(), Box<dyn std::error::Error>> {
let j_synchronization = Job::new_async("0 0 0/1 * * *", move |_uuid, _l| {
Box::pin(async move {
let start_at = (chrono::Utc::now()).timestamp();
let conn = self.pool.get().expect("conn");
let req = api::member::models::ListMembersRequest { group_key: None };
let res = match self.member_api.list_members(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_MEMBERS.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("list_members");
for u in res.users {
let modify_member = repositories::member::models::ModifyMember {
balance: u.cash,
balance_bota: u.cash_bota,
oriental_play: u.oriental_play,
};
self
.member_repository
.update(&conn, u.id, &modify_member)
.expect("member update");
}
self.add_history(
&conn,
repositories::synchronization::models::ITEM_MEMBERS.to_string(),
start_at,
0,
None,
);
})
})?;
self.sched.add(j_synchronization).await;
Ok(())
} }
} }

View File

@ -1,17 +1,24 @@
use std::time::Duration; use crate::api;
use crate::core;
use crate::repositories; use crate::repositories;
use diesel::{ use diesel::{
r2d2::{ConnectionManager, Pool}, r2d2::{ConnectionManager, Pool},
PgConnection, PgConnection,
}; };
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tokio_cron_scheduler::{Job, JobScheduler}; use tokio_cron_scheduler::{Job, JobScheduler};
static G_INSTANCE: OnceCell<Arc<Scheduler>> = OnceCell::new();
/// ///
pub struct Scheduler { pub struct Scheduler {
pool: Pool<ConnectionManager<PgConnection>>, pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler, sched: JobScheduler,
api_config: core::config::ApiConfig,
synchronization_history_repository: repositories::synchronization_history::repository::Repository,
vendor_repository: repositories::vendor::repository::Repository, vendor_repository: repositories::vendor::repository::Repository,
vendor_api: api::vendor::api::Api,
} }
impl std::fmt::Debug for Scheduler { impl std::fmt::Debug for Scheduler {
@ -22,65 +29,119 @@ impl std::fmt::Debug for Scheduler {
impl Scheduler { impl Scheduler {
/// ///
pub fn new(pool: Pool<ConnectionManager<PgConnection>>, sched: JobScheduler) -> Scheduler { pub fn get_instance(
Scheduler { pool: Pool<ConnectionManager<PgConnection>>,
sched: JobScheduler,
api_config: core::config::ApiConfig,
) -> Result<&'static Arc<Scheduler>, Box<dyn std::error::Error>> {
let instance = G_INSTANCE
.get_or_try_init(|| -> Result<Arc<Scheduler>, Box<dyn std::error::Error>> {
let s = Scheduler {
pool, pool,
sched, sched,
api_config: api_config.clone(),
synchronization_history_repository:
repositories::synchronization_history::repository::Repository::new(),
vendor_repository: repositories::vendor::repository::Repository::new(), vendor_repository: repositories::vendor::repository::Repository::new(),
} vendor_api: api::vendor::api::Api::new(api_config.clone()),
};
Ok(Arc::new(s))
})
.expect("");
Ok(instance)
} }
pub async fn queue(&self) -> Result<(), std::boxed::Box<dyn std::error::Error>> { pub async fn queue(&'static self) -> Result<(), std::boxed::Box<dyn std::error::Error>> {
self.list_members().await?; self.list_vendors().await?;
Ok(()) Ok(())
} }
async fn list_members(&self) -> Result<(), Box<dyn std::error::Error>> { async fn add_history(
let mut jj = Job::new_repeated(Duration::from_secs(60), |_uuid, _l| { &'static self,
println!("{:?} I'm repeated every 60 seconds", chrono::Utc::now()); conn: &diesel::PgConnection,
}) item: String,
.unwrap(); start_at: i64,
code: i64,
message: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let complete_at = (chrono::Utc::now()).timestamp();
jj.on_start_notification_add( self
&self.sched, .synchronization_history_repository
Box::new(|job_id, notification_id, type_of_notification| { .insert(
conn,
&repositories::synchronization_history::models::NewSynchronizationHistory {
item,
start_at,
complete_at,
code,
message,
},
)
.expect("synchronization_history insert");
Ok(())
}
async fn list_vendors(&'static self) -> Result<(), Box<dyn std::error::Error>> {
let j_synchronization = Job::new_async("0 0 0/1 * * *", move |_uuid, _l| {
Box::pin(async move { Box::pin(async move {
println!( let start_at = (chrono::Utc::now()).timestamp();
"Job {:?} was started, notification {:?} ran ({:?})", let conn = self.pool.get().expect("conn");
job_id, notification_id, type_of_notification
let req = api::vendor::models::ListVendorsRequest {};
let res = match self.vendor_api.list_vendors(req).await {
Ok(r) => Ok(r),
Err(e) => {
self.add_history(
&conn,
repositories::synchronization::models::ITEM_VENDORS.to_string(),
start_at,
e.code,
e.msg.clone(),
);
Err(e)
}
}
.expect("list_vendors");
let upsert_vendors: Vec<repositories::vendor::models::UpsertVendor> = res
.vendors
.iter()
.map(|d| repositories::vendor::models::UpsertVendor {
id: d.id,
company_id: d.company_id,
vendor_id: d.vendor_id,
key: d.key.clone(),
name: d.name.clone(),
category: d.category.clone(),
max_bet_casino: d.max_bet_casino,
max_bet_slot: d.max_bet_slot,
is_enable: d.is_enable.clone(),
bet_count: d.bet_count,
})
.collect();
let affected = self
.vendor_repository
.upserts(&conn, upsert_vendors)
.expect("vendor upsert");
self.add_history(
&conn,
repositories::synchronization::models::ITEM_VENDORS.to_string(),
start_at,
0,
None,
); );
}) })
}), })?;
)
.await;
jj.on_stop_notification_add( self.sched.add(j_synchronization).await;
&self.sched,
Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
println!(
"Job {:?} was completed, notification {:?} ran ({:?})",
job_id, notification_id, type_of_notification
);
})
}),
)
.await;
jj.on_removed_notification_add(
&self.sched,
Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
println!(
"Job {:?} was removed, notification {:?} ran ({:?})",
job_id, notification_id, type_of_notification
);
})
}),
)
.await;
self.sched.add(jj).await;
Ok(()) Ok(())
} }

View File

@ -2,8 +2,6 @@
pub struct Service { pub struct Service {
connection_broker: nats::asynk::Connection, connection_broker: nats::asynk::Connection,
queue_broker: String, queue_broker: String,
k_secret: String,
k_username: String,
} }
impl std::fmt::Debug for Service { impl std::fmt::Debug for Service {
@ -14,27 +12,34 @@ impl std::fmt::Debug for Service {
impl Service { impl Service {
/// ///
pub fn new( pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
k_secret: String,
k_username: String,
) -> Service {
Service { Service {
connection_broker, connection_broker,
queue_broker, queue_broker,
k_secret,
k_username,
} }
} }
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) futures::try_join!(
self.list_history(),
self.list_history_for_pragmatic(),
self.list_history_for_evolution(),
self.statistics(),
)
.map(|_| ())
} }
async fn list_history(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn list_history(&self) -> Result<(), Box<dyn std::error::Error>> {
async fn list_history_for_pragmatic(&self) -> Result<(), Box<dyn std::error::Error>> {} Ok(())
async fn list_history_for_evolution(&self) -> Result<(), Box<dyn std::error::Error>> {} }
async fn list_history_for_pragmatic(&self) -> Result<(), Box<dyn std::error::Error>> {
async fn statistics(&self) -> Result<(), Box<dyn std::error::Error>> {} Ok(())
}
async fn list_history_for_evolution(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn statistics(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
} }

View File

@ -2,8 +2,6 @@
pub struct Service { pub struct Service {
connection_broker: nats::asynk::Connection, connection_broker: nats::asynk::Connection,
queue_broker: String, queue_broker: String,
k_secret: String,
k_username: String,
} }
impl std::fmt::Debug for Service { impl std::fmt::Debug for Service {
@ -14,26 +12,25 @@ impl std::fmt::Debug for Service {
impl Service { impl Service {
/// ///
pub fn new( pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
k_secret: String,
k_username: String,
) -> Service {
Service { Service {
connection_broker, connection_broker,
queue_broker, queue_broker,
k_secret,
k_username,
} }
} }
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) futures::try_join!(self.list_vendors(), self.list_games(), self.execute_game(),).map(|_| ())
} }
async fn list_vendors(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn list_vendors(&self) -> Result<(), Box<dyn std::error::Error>> {
async fn list_games(&self) -> Result<(), Box<dyn std::error::Error>> {} Ok(())
}
async fn execute_game(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn list_games(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn execute_game(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
} }

View File

@ -2,8 +2,6 @@
pub struct Service { pub struct Service {
connection_broker: nats::asynk::Connection, connection_broker: nats::asynk::Connection,
queue_broker: String, queue_broker: String,
k_secret: String,
k_username: String,
} }
impl std::fmt::Debug for Service { impl std::fmt::Debug for Service {
@ -14,17 +12,10 @@ impl std::fmt::Debug for Service {
impl Service { impl Service {
/// ///
pub fn new( pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
k_secret: String,
k_username: String,
) -> Service {
Service { Service {
connection_broker, connection_broker,
queue_broker, queue_broker,
k_secret,
k_username,
} }
} }
@ -32,9 +23,15 @@ impl Service {
futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ())
} }
async fn create_member(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn create_member(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
async fn list_members(&self) -> Result<(), Box<dyn std::error::Error>> {} }
async fn get_member(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn list_members(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn get_member(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
} }

View File

@ -2,8 +2,6 @@
pub struct Service { pub struct Service {
connection_broker: nats::asynk::Connection, connection_broker: nats::asynk::Connection,
queue_broker: String, queue_broker: String,
k_secret: String,
k_username: String,
} }
impl std::fmt::Debug for Service { impl std::fmt::Debug for Service {
@ -14,28 +12,34 @@ impl std::fmt::Debug for Service {
impl Service { impl Service {
/// ///
pub fn new( pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
k_secret: String,
k_username: String,
) -> Service {
Service { Service {
connection_broker, connection_broker,
queue_broker, queue_broker,
k_secret,
k_username,
} }
} }
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) futures::try_join!(
self.create_deposit(),
self.create_withdraw(),
self.list_partner_balnace(),
self.list_user_balnace(),
)
.map(|_| ())
} }
async fn create_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn create_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {
async fn create_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {} Ok(())
}
async fn list_partner_balnace(&self) -> Result<(), Box<dyn std::error::Error>> {} async fn create_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {
async fn list_user_balnace(&self) -> Result<(), Box<dyn std::error::Error>> {} Ok(())
}
async fn list_partner_balnace(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn list_user_balnace(&self) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
} }