From 88fc45b7c8162f3312a10ad28caef2a7b74a367f Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Tue, 16 Aug 2022 17:00:26 +0000 Subject: [PATCH] implemented --- .vscode/launch.json | 2 + Cargo.toml | 10 +- .../202208051200_api_kgon_member/down.sql | 3 + .../202208051200_api_kgon_member/up.sql | 27 ++ .../down.sql | 1 + .../up.sql | 4 + .../202208051400_api_kgon_balance/down.sql | 2 + .../202208051400_api_kgon_balance/up.sql | 14 + .../down.sql | 2 + .../up.sql | 4 + .../202208061200_api_kgon_vendor/down.sql | 0 .../202208061200_api_kgon_vendor/up.sql | 25 ++ .../down.sql | 0 .../up.sql | 4 + .../202208061300_api_kgon_game/down.sql | 0 migrations/202208061300_api_kgon_game/up.sql | 23 ++ .../down.sql | 0 .../up.sql | 4 + .../202208071200_api_kgon_betting/down.sql | 0 .../202208071200_api_kgon_betting/up.sql | 0 .../repository.rs => api/betting/api.rs} | 12 +- src/api/betting/mod.rs | 7 + src/{repositories => api}/betting/models.rs | 0 src/api/game/api.rs | 150 +++++++++ src/api/game/mod.rs | 7 + src/api/game/models.rs | 47 +++ .../repository.rs => api/member/api.rs} | 28 +- src/api/member/mod.rs | 7 + .../identity => api/member}/models.rs | 4 +- .../member_account/api.rs} | 12 +- src/api/member_account/mod.rs | 7 + .../account => api/member_account}/models.rs | 0 src/api/mod.rs | 4 + src/compositions/betting/mod.rs | 0 src/compositions/game/mod.rs | 0 src/compositions/member/mod.rs | 0 src/compositions/member_account/mod.rs | 0 src/compositions/mod.rs | 4 + src/main.rs | 65 +++- src/repositories/{account => balance}/mod.rs | 2 + src/repositories/balance/models.rs | 37 +++ src/repositories/balance/repository.rs | 75 +++++ src/repositories/balance/schema.rs | 18 ++ src/repositories/game/mod.rs | 2 + src/repositories/game/models.rs | 121 +++++--- src/repositories/game/repository.rs | 285 +++++++++++------- src/repositories/game/schema.rs | 28 ++ src/repositories/{identity => member}/mod.rs | 2 + src/repositories/member/models.rs | 78 +++++ src/repositories/member/repository.rs | 200 ++++++++++++ src/repositories/member/schema.rs | 26 ++ src/repositories/mod.rs | 6 +- src/repositories/{betting => vendor}/mod.rs | 2 + src/repositories/vendor/models.rs | 108 +++++++ src/repositories/vendor/repository.rs | 234 ++++++++++++++ src/repositories/vendor/schema.rs | 32 ++ src/schedulers/balance/mod.rs | 1 + src/schedulers/balance/scheduler.rs | 15 + src/schedulers/game/mod.rs | 1 + src/schedulers/game/scheduler.rs | 15 + src/schedulers/member/mod.rs | 1 + src/schedulers/member/scheduler.rs | 19 ++ src/schedulers/mod.rs | 4 + src/schedulers/vendor/mod.rs | 1 + src/schedulers/vendor/scheduler.rs | 87 ++++++ src/services/betting/mod.rs | 1 + src/services/betting/service.rs | 40 +++ src/services/game/mod.rs | 1 + src/services/{identity => game}/service.rs | 7 +- src/services/{identity => member}/mod.rs | 0 src/services/member/service.rs | 40 +++ src/services/member_account/mod.rs | 1 + src/services/member_account/service.rs | 41 +++ src/services/mod.rs | 4 +- 74 files changed, 1822 insertions(+), 192 deletions(-) create mode 100644 migrations/202208051200_api_kgon_member/down.sql create mode 100644 migrations/202208051200_api_kgon_member/up.sql create mode 100644 migrations/202208051210_api_kgon_synchronization_member/down.sql create mode 100644 migrations/202208051210_api_kgon_synchronization_member/up.sql create mode 100644 migrations/202208051400_api_kgon_balance/down.sql create mode 100644 migrations/202208051400_api_kgon_balance/up.sql create mode 100644 migrations/202208051410_api_kgon_synchronization_balance/down.sql create mode 100644 migrations/202208051410_api_kgon_synchronization_balance/up.sql rename src/services/account/mod.rs => migrations/202208061200_api_kgon_vendor/down.sql (100%) create mode 100644 migrations/202208061200_api_kgon_vendor/up.sql create mode 100644 migrations/202208061210_api_kgon_synchronization_vendor/down.sql create mode 100644 migrations/202208061210_api_kgon_synchronization_vendor/up.sql create mode 100644 migrations/202208061300_api_kgon_game/down.sql create mode 100644 migrations/202208061300_api_kgon_game/up.sql create mode 100644 migrations/202208061310_api_kgon_synchronization_game/down.sql create mode 100644 migrations/202208061310_api_kgon_synchronization_game/up.sql create mode 100644 migrations/202208071200_api_kgon_betting/down.sql create mode 100644 migrations/202208071200_api_kgon_betting/up.sql rename src/{repositories/betting/repository.rs => api/betting/api.rs} (95%) create mode 100644 src/api/betting/mod.rs rename src/{repositories => api}/betting/models.rs (100%) create mode 100644 src/api/game/api.rs create mode 100644 src/api/game/mod.rs create mode 100644 src/api/game/models.rs rename src/{repositories/identity/repository.rs => api/member/api.rs} (82%) create mode 100644 src/api/member/mod.rs rename src/{repositories/identity => api/member}/models.rs (93%) rename src/{repositories/account/repository.rs => api/member_account/api.rs} (95%) create mode 100644 src/api/member_account/mod.rs rename src/{repositories/account => api/member_account}/models.rs (100%) create mode 100644 src/api/mod.rs create mode 100644 src/compositions/betting/mod.rs create mode 100644 src/compositions/game/mod.rs create mode 100644 src/compositions/member/mod.rs create mode 100644 src/compositions/member_account/mod.rs rename src/repositories/{account => balance}/mod.rs (72%) create mode 100644 src/repositories/balance/models.rs create mode 100644 src/repositories/balance/repository.rs create mode 100644 src/repositories/balance/schema.rs create mode 100644 src/repositories/game/schema.rs rename src/repositories/{identity => member}/mod.rs (72%) create mode 100644 src/repositories/member/models.rs create mode 100644 src/repositories/member/repository.rs create mode 100644 src/repositories/member/schema.rs rename src/repositories/{betting => vendor}/mod.rs (72%) create mode 100644 src/repositories/vendor/models.rs create mode 100644 src/repositories/vendor/repository.rs create mode 100644 src/repositories/vendor/schema.rs create mode 100644 src/schedulers/balance/mod.rs create mode 100644 src/schedulers/balance/scheduler.rs create mode 100644 src/schedulers/game/mod.rs create mode 100644 src/schedulers/game/scheduler.rs create mode 100644 src/schedulers/member/mod.rs create mode 100644 src/schedulers/member/scheduler.rs create mode 100644 src/schedulers/mod.rs create mode 100644 src/schedulers/vendor/mod.rs create mode 100644 src/schedulers/vendor/scheduler.rs create mode 100644 src/services/betting/service.rs rename src/services/{identity => game}/service.rs (67%) rename src/services/{identity => member}/mod.rs (100%) create mode 100644 src/services/member/service.rs create mode 100644 src/services/member_account/mod.rs create mode 100644 src/services/member_account/service.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 68409c6..0af3c8f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,6 +20,7 @@ } }, "env": { + "URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran", "URL_BROKER": "nats://192.168.50.200:4222", "QUEUE_BROKER": "bet.beteran", "K_SECRET": "c23d770b873b2ce95747abc57052beb0", @@ -45,6 +46,7 @@ } }, "env": { + "URL_DATABASE": "postgresql://beteran:qwer5795QWER@192.168.50.200:25432/beteran", "URL_BROKER": "nats://192.168.50.200:4222", "QUEUE_BROKER": "bet.beteran", "K_SECRET": "c23d770b873b2ce95747abc57052beb0", diff --git a/Cargo.toml b/Cargo.toml index a6644b5..25d1e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,10 @@ path = "./src/main.rs" [dependencies] +chrono = { version = "0" } +diesel = { version = "1", features = ["chrono", "r2d2", "uuidv07", "postgres"] } +diesel_migrations = { version = "1" } +diesel-derive-enum = { version = "1", features = ["postgres"] } futures = { version = "0", default-features = false, features = [ "async-await", ] } @@ -21,9 +25,11 @@ reqwest = { version = "0", features = ["json"] } serde = { version = "1", features = ["derive"] } serde_json = { version = "1" } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } +tokio-cron-scheduler = { version = "*" } +uuid = { version = "0", features = ["serde", "v4", "v5"] } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.22-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.2-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.61-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.45-snapshot" } [build-dependencies] diff --git a/migrations/202208051200_api_kgon_member/down.sql b/migrations/202208051200_api_kgon_member/down.sql new file mode 100644 index 0000000..c17b198 --- /dev/null +++ b/migrations/202208051200_api_kgon_member/down.sql @@ -0,0 +1,3 @@ +DROP INDEX idx_api_kgon_members_member_id; +DROP TRIGGER tg_api_kgon_members_updated_at; +DROP TABLE api_kgon_members; diff --git a/migrations/202208051200_api_kgon_member/up.sql b/migrations/202208051200_api_kgon_member/up.sql new file mode 100644 index 0000000..266f785 --- /dev/null +++ b/migrations/202208051200_api_kgon_member/up.sql @@ -0,0 +1,27 @@ +CREATE TABLE IF NOT EXISTS api_kgon_members ( + id BIGINT NOT NULL, + balance BIGINT NOT NULL DEFAULT 0, + balance_bota BIGINT NOT NULL DEFAULT 0, + balance_sum BIGINT NOT NULL DEFAULT 0, + companies BIGINT NOT NULL DEFAULT 0, + oriental_play BOOLEAN NOT NULL DEFAULT FALSE, + member_id UUID NOT NULL, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + CONSTRAINT fk_api_kgon_members_member_id + FOREIGN KEY(member_id) + REFERENCES members(id) +); + +CREATE INDEX idx_api_kgon_members_member_id ON api_kgon_members (member_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_api_kgon_members_updated_at + BEFORE UPDATE + ON api_kgon_members + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); + + + diff --git a/migrations/202208051210_api_kgon_synchronization_member/down.sql b/migrations/202208051210_api_kgon_synchronization_member/down.sql new file mode 100644 index 0000000..714f512 --- /dev/null +++ b/migrations/202208051210_api_kgon_synchronization_member/down.sql @@ -0,0 +1 @@ +DROP TABLE api_kgon_synchronization_members; diff --git a/migrations/202208051210_api_kgon_synchronization_member/up.sql b/migrations/202208051210_api_kgon_synchronization_member/up.sql new file mode 100644 index 0000000..dd2414f --- /dev/null +++ b/migrations/202208051210_api_kgon_synchronization_member/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS api_kgon_synchronization_members ( + id BIGSERIAL PRIMARY KEY; + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000) +); diff --git a/migrations/202208051400_api_kgon_balance/down.sql b/migrations/202208051400_api_kgon_balance/down.sql new file mode 100644 index 0000000..58771eb --- /dev/null +++ b/migrations/202208051400_api_kgon_balance/down.sql @@ -0,0 +1,2 @@ +DROP TRIGGER tg_api_kgon_balances_updated_at; +DROP TABLE api_kgon_balances; diff --git a/migrations/202208051400_api_kgon_balance/up.sql b/migrations/202208051400_api_kgon_balance/up.sql new file mode 100644 index 0000000..a605cae --- /dev/null +++ b/migrations/202208051400_api_kgon_balance/up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS api_kgon_balances ( + id SERIAL PRIMARY KEY; + balance BIGINT NOT NULL DEFAULT 0, + balance_bota BIGINT NOT NULL DEFAULT 0, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000) +); + +-- trigger (updated_at) +CREATE TRIGGER tg_api_kgon_balances_updated_at + BEFORE UPDATE + ON api_kgon_balances + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202208051410_api_kgon_synchronization_balance/down.sql b/migrations/202208051410_api_kgon_synchronization_balance/down.sql new file mode 100644 index 0000000..58771eb --- /dev/null +++ b/migrations/202208051410_api_kgon_synchronization_balance/down.sql @@ -0,0 +1,2 @@ +DROP TRIGGER tg_api_kgon_balances_updated_at; +DROP TABLE api_kgon_balances; diff --git a/migrations/202208051410_api_kgon_synchronization_balance/up.sql b/migrations/202208051410_api_kgon_synchronization_balance/up.sql new file mode 100644 index 0000000..d5274eb --- /dev/null +++ b/migrations/202208051410_api_kgon_synchronization_balance/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS api_kgon_synchronization_balances ( + id BIGSERIAL PRIMARY KEY; + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000) +); diff --git a/src/services/account/mod.rs b/migrations/202208061200_api_kgon_vendor/down.sql similarity index 100% rename from src/services/account/mod.rs rename to migrations/202208061200_api_kgon_vendor/down.sql diff --git a/migrations/202208061200_api_kgon_vendor/up.sql b/migrations/202208061200_api_kgon_vendor/up.sql new file mode 100644 index 0000000..6ae325e --- /dev/null +++ b/migrations/202208061200_api_kgon_vendor/up.sql @@ -0,0 +1,25 @@ +CREATE TABLE IF NOT EXISTS api_kgon_vendors ( + id BIGINT NOT NULL, + company_id BIGINT NOT NULL, + vendor_id BIGINT NOT NULL, + max_bet_casino BIGINT NOT NULL, + max_bet_slot BIGINT NOT NULL, + is_enable BOOLEAN NOT NULL, + bet_count BIGINT NOT NULL, + key TEXT NOT NULL, + name TEXT NOT NULL, + category TEXT NOT NULL, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + CONSTRAINT fk_api_kgon_vendors_parent_id + FOREIGN KEY(parent_id) + REFERENCES roles(id) +); + +-- trigger (updated_at) +CREATE TRIGGER tg_api_kgon_vendors_updated_at + BEFORE UPDATE + ON api_kgon_vendors + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202208061210_api_kgon_synchronization_vendor/down.sql b/migrations/202208061210_api_kgon_synchronization_vendor/down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/202208061210_api_kgon_synchronization_vendor/up.sql b/migrations/202208061210_api_kgon_synchronization_vendor/up.sql new file mode 100644 index 0000000..53f8fd4 --- /dev/null +++ b/migrations/202208061210_api_kgon_synchronization_vendor/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS api_kgon_synchronization_vendors ( + id BIGSERIAL PRIMARY KEY; + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000) +); diff --git a/migrations/202208061300_api_kgon_game/down.sql b/migrations/202208061300_api_kgon_game/down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/202208061300_api_kgon_game/up.sql b/migrations/202208061300_api_kgon_game/up.sql new file mode 100644 index 0000000..5a4eae5 --- /dev/null +++ b/migrations/202208061300_api_kgon_game/up.sql @@ -0,0 +1,23 @@ +CREATE TABLE IF NOT EXISTS api_kgon_games ( + id BIGINT NOT NULL, + vendor_id BIGINT NOT NULL, + key TEXT NOT NULL, + names TEXT NOT NULL, + platform TEXT NOT NULL, + category TEXT NOT NULL, + game_type TEXT NOT NULL, + image TEXT, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + CONSTRAINT fk_api_kgon_games_vendor_id + FOREIGN KEY(vendor_id) + REFERENCES api_kgon_vendors(id) +); + +-- trigger (updated_at) +CREATE TRIGGER tg_api_kgon_games_updated_at + BEFORE UPDATE + ON api_kgon_games + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202208061310_api_kgon_synchronization_game/down.sql b/migrations/202208061310_api_kgon_synchronization_game/down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/202208061310_api_kgon_synchronization_game/up.sql b/migrations/202208061310_api_kgon_synchronization_game/up.sql new file mode 100644 index 0000000..32acd5c --- /dev/null +++ b/migrations/202208061310_api_kgon_synchronization_game/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS api_kgon_synchronization_games ( + id BIGSERIAL PRIMARY KEY; + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000) +); diff --git a/migrations/202208071200_api_kgon_betting/down.sql b/migrations/202208071200_api_kgon_betting/down.sql new file mode 100644 index 0000000..e69de29 diff --git a/migrations/202208071200_api_kgon_betting/up.sql b/migrations/202208071200_api_kgon_betting/up.sql new file mode 100644 index 0000000..e69de29 diff --git a/src/repositories/betting/repository.rs b/src/api/betting/api.rs similarity index 95% rename from src/repositories/betting/repository.rs rename to src/api/betting/api.rs index da1ea01..eab45ed 100644 --- a/src/repositories/betting/repository.rs +++ b/src/api/betting/api.rs @@ -2,22 +2,22 @@ use super::models; use std::{collections::HashMap, time::Duration}; /// -pub struct Repository { +pub struct Api { client: reqwest::Client, k_secret: String, k_username: String, } -impl std::fmt::Debug for Repository { +impl std::fmt::Debug for Api { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Repository of api.kgon.identity").finish() + f.debug_struct("Api of api.kgon.identity").finish() } } -impl Repository { +impl Api { /// - pub fn new(k_secret: &str, k_username: &str) -> Repository { - Repository { + pub fn new(k_secret: &str, k_username: &str) -> Api { + Api { client: reqwest::Client::new(), k_secret: k_secret.to_string(), k_username: k_username.to_string(), diff --git a/src/api/betting/mod.rs b/src/api/betting/mod.rs new file mode 100644 index 0000000..b6d267a --- /dev/null +++ b/src/api/betting/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod api; +/// +pub mod models; diff --git a/src/repositories/betting/models.rs b/src/api/betting/models.rs similarity index 100% rename from src/repositories/betting/models.rs rename to src/api/betting/models.rs diff --git a/src/api/game/api.rs b/src/api/game/api.rs new file mode 100644 index 0000000..5dbfa46 --- /dev/null +++ b/src/api/game/api.rs @@ -0,0 +1,150 @@ +use super::models; +use std::{collections::HashMap, time::Duration}; + +/// +pub struct Api { + client: reqwest::Client, + k_secret: String, + k_username: String, +} + +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(k_secret: &str, k_username: &str) -> Api { + Api { + client: reqwest::Client::new(), + k_secret: k_secret.to_string(), + k_username: k_username.to_string(), + } + } + + /// + pub async fn find_vendors( + &self, + data: models::FindVendorsRequest, + ) -> Result { + 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::().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 { + let mut params = HashMap::new(); + + params.insert("vendorKey", data.vendor_key); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/games") + .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()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn play( + &self, + data: models::PlayRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("vendorKey", data.vendor_key); + params.insert("gameKey", data.game_key); + params.insert("username", data.username); + params.insert("nickname", data.nickname); + params.insert("siteUsername", data.site_username); + + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + + params.insert("amount", data.amount.to_string()); + + if let Some(request_key) = data.request_key { + params.insert("requestKey", request_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/play") + .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()) + .form(¶ms) + .timeout(Duration::from_secs(10)) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } +} diff --git a/src/api/game/mod.rs b/src/api/game/mod.rs new file mode 100644 index 0000000..b6d267a --- /dev/null +++ b/src/api/game/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod api; +/// +pub mod models; diff --git a/src/api/game/models.rs b/src/api/game/models.rs new file mode 100644 index 0000000..5df3e3d --- /dev/null +++ b/src/api/game/models.rs @@ -0,0 +1,47 @@ +use serde::{Deserialize, Serialize}; + +pub struct Error { + pub code: i64, + pub msg: Option, +} + +pub struct FindVendorsRequest {} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindVendorsResponse { + pub code: i64, + pub msg: Option, + pub vendors: Vec, +} + +pub struct FindGamesRequest { + pub vendor_key: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindGamesResponse { + pub code: i64, + pub msg: Option, + pub games: Vec, +} + +pub struct PlayRequest { + pub vendor_key: String, + pub game_key: String, + pub username: String, + pub nickname: String, + pub site_username: String, + pub group_key: Option, + pub amount: i64, + pub request_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct PlayResponse { + pub code: i64, + pub msg: Option, + #[serde(rename = "userId")] + pub user_id: String, + pub url: String, + pub balance: i64, +} diff --git a/src/repositories/identity/repository.rs b/src/api/member/api.rs similarity index 82% rename from src/repositories/identity/repository.rs rename to src/api/member/api.rs index 644aaf6..8e62486 100644 --- a/src/repositories/identity/repository.rs +++ b/src/api/member/api.rs @@ -2,33 +2,35 @@ use super::models; use std::collections::HashMap; /// -pub struct Repository { +pub struct Api { client: reqwest::Client, + url: String, k_secret: String, k_username: String, } -impl std::fmt::Debug for Repository { +impl std::fmt::Debug for Api { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Repository of api.kgon.identity").finish() + f.debug_struct("Api of api.kgon.identity").finish() } } -impl Repository { +impl Api { /// - pub fn new(k_secret: &str, k_username: &str) -> Repository { - Repository { + pub fn new(url: &str, k_secret: &str, k_username: &str) -> Api { + Api { client: reqwest::Client::new(), + url: url.to_string(), k_secret: k_secret.to_string(), k_username: k_username.to_string(), } } /// - pub async fn find_members( + pub async fn list_members( &self, - data: models::FindMembersRequest, - ) -> Result { + data: models::ListMembersRequest, + ) -> Result { let mut params = HashMap::new(); if let Some(group_key) = data.group_key { @@ -37,7 +39,7 @@ impl Repository { let res = match self .client - .post("https://dev-mw.kgonapi.com/users") + .post(format!("{}/users", self.url)) .header(reqwest::header::ACCEPT, "application/json") .header( reqwest::header::CONTENT_TYPE, @@ -56,7 +58,7 @@ impl Repository { }; match res.status() { - reqwest::StatusCode::OK => match res.json::().await { + reqwest::StatusCode::OK => match res.json::().await { Ok(r) => Ok(r), Err(e) => Err(models::Error { code: 0, msg: None }), }, @@ -116,7 +118,7 @@ mod tests { #[tokio::test] async fn find_members() { - let repository = Repository::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); + let repository = Api::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); let r = repository .find_members(models::FindMembersRequest { group_key: None }) @@ -128,7 +130,7 @@ mod tests { #[tokio::test] async fn save_member() { - let repository = Repository::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); + let repository = Api::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); let r = repository .save_member(models::SaveMemberRequest { diff --git a/src/api/member/mod.rs b/src/api/member/mod.rs new file mode 100644 index 0000000..b6d267a --- /dev/null +++ b/src/api/member/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod api; +/// +pub mod models; diff --git a/src/repositories/identity/models.rs b/src/api/member/models.rs similarity index 93% rename from src/repositories/identity/models.rs rename to src/api/member/models.rs index fc0efe1..4ae48b7 100644 --- a/src/repositories/identity/models.rs +++ b/src/api/member/models.rs @@ -7,7 +7,7 @@ pub struct Error { } #[derive(Serialize, Deserialize, Debug)] -pub struct FindMembersRequest { +pub struct ListMembersRequest { pub group_key: Option, } @@ -24,7 +24,7 @@ pub struct FindMember { } #[derive(Serialize, Deserialize, Debug)] -pub struct FindMembersResponse { +pub struct ListMembersResponse { pub code: i64, pub msg: Option, pub users: Vec, diff --git a/src/repositories/account/repository.rs b/src/api/member_account/api.rs similarity index 95% rename from src/repositories/account/repository.rs rename to src/api/member_account/api.rs index e95c6f0..836936b 100644 --- a/src/repositories/account/repository.rs +++ b/src/api/member_account/api.rs @@ -2,22 +2,22 @@ use super::models; use std::collections::HashMap; /// -pub struct Repository { +pub struct Api { client: reqwest::Client, k_secret: String, k_username: String, } -impl std::fmt::Debug for Repository { +impl std::fmt::Debug for Api { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Repository of api.kgon.identity").finish() + f.debug_struct("Api of api.kgon.member_account").finish() } } -impl Repository { +impl Api { /// - pub fn new(k_secret: &str, k_username: &str) -> Repository { - Repository { + pub fn new(k_secret: &str, k_username: &str) -> Api { + Api { client: reqwest::Client::new(), k_secret: k_secret.to_string(), k_username: k_username.to_string(), diff --git a/src/api/member_account/mod.rs b/src/api/member_account/mod.rs new file mode 100644 index 0000000..b6d267a --- /dev/null +++ b/src/api/member_account/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod api; +/// +pub mod models; diff --git a/src/repositories/account/models.rs b/src/api/member_account/models.rs similarity index 100% rename from src/repositories/account/models.rs rename to src/api/member_account/models.rs diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..0e5fc3e --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1,4 @@ +pub mod betting; +pub mod game; +pub mod member; +pub mod member_account; diff --git a/src/compositions/betting/mod.rs b/src/compositions/betting/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/compositions/game/mod.rs b/src/compositions/game/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/compositions/member/mod.rs b/src/compositions/member/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/compositions/member_account/mod.rs b/src/compositions/member_account/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/compositions/mod.rs b/src/compositions/mod.rs index e69de29..0e5fc3e 100644 --- a/src/compositions/mod.rs +++ b/src/compositions/mod.rs @@ -0,0 +1,4 @@ +pub mod betting; +pub mod game; +pub mod member; +pub mod member_account; diff --git a/src/main.rs b/src/main.rs index 6197b36..74371a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,31 @@ //! //! +#[macro_use] +extern crate diesel; +#[macro_use] +extern crate diesel_migrations; +use diesel::{ + r2d2::{ConnectionManager, Pool}, + PgConnection, +}; use std::env; +mod api; mod compositions; mod repositories; +mod schedulers; mod services; +diesel_migrations::embed_migrations!(); + #[tokio::main] async fn main() -> Result<(), Box> { + let url_db = match env::var_os("URL_DATABASE") { + Some(v) => v.into_string().unwrap(), + None => "".to_string(), + }; + let url_server_broker = match env::var_os("URL_BROKER") { Some(v) => v.into_string().unwrap(), None => "".to_string(), @@ -27,19 +44,63 @@ async fn main() -> Result<(), Box> { None => "".to_string(), }; + let mut sched = tokio_cron_scheduler::JobScheduler::new().await?; + + let manager = ConnectionManager::::new(url_db); + let pool = Pool::builder() + .max_size(4) + .test_on_check_out(true) + .build(manager)?; + let conn = pool.get()?; + + embedded_migrations::run(&conn)?; + let server_broker_opts = nats::asynk::Options::new(); let connection_server_broker = server_broker_opts.connect(url_server_broker).await?; - let identity_service = services::identity::service::Service::new( + let member_service = services::member::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + k_secret.clone(), + k_username.clone(), + ); + let member_account_service = services::member_account::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + k_secret.clone(), + k_username.clone(), + ); + let game_service = services::game::service::Service::new( + connection_server_broker.clone(), + queue_server_broker.clone(), + k_secret.clone(), + k_username.clone(), + ); + let betting_service = services::betting::service::Service::new( connection_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()); + vendor_scheduler.queue().await; + + let start = sched.start().await; + if start.is_err() { + panic!("Error starting scheduler"); + } + println!("Server service [beteran-api-kgon-server-service] is started"); - futures::try_join!(identity_service.subscribe(),)?; + futures::try_join!( + member_service.subscribe(), + member_account_service.subscribe(), + game_service.subscribe(), + betting_service.subscribe(), + )?; + + sched.shutdown().await?; Ok(()) } diff --git a/src/repositories/account/mod.rs b/src/repositories/balance/mod.rs similarity index 72% rename from src/repositories/account/mod.rs rename to src/repositories/balance/mod.rs index 2e71a26..bef7c9e 100644 --- a/src/repositories/account/mod.rs +++ b/src/repositories/balance/mod.rs @@ -5,3 +5,5 @@ pub mod models; /// pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/balance/models.rs b/src/repositories/balance/models.rs new file mode 100644 index 0000000..312095f --- /dev/null +++ b/src/repositories/balance/models.rs @@ -0,0 +1,37 @@ +use super::schema::api_kgon_balances; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "api_kgon_balances"] +pub struct Balance { + /// + pub id: i32, + /// + pub balance: i64, + /// + pub balance_bota: i64, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "api_kgon_balances"] +pub struct NewBalance { + /// + pub balance: i64, + /// + pub balance_bota: i64, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "api_kgon_balances"] +pub struct ModifyBalance { + /// + pub balance: i64, + /// + pub balance_bota: i64, +} diff --git a/src/repositories/balance/repository.rs b/src/repositories/balance/repository.rs new file mode 100644 index 0000000..c047836 --- /dev/null +++ b/src/repositories/balance/repository.rs @@ -0,0 +1,75 @@ +//! +//! +use super::{models, schema::api_kgon_balances}; +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_balances").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::NewBalance, + ) -> Result { + let inserted = diesel::insert_into(api_kgon_balances::table) + .values(new_member) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select(&self, conn: &diesel::PgConnection) -> Result, Error> { + match api_kgon_balances::table.first::(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: i32, + modify: &models::ModifyBalance, + ) -> Result { + use api_kgon_balances::dsl; + + diesel::update(dsl::api_kgon_balances.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: i32) -> Result { + use api_kgon_balances::dsl; + + diesel::delete(api_kgon_balances::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/balance/schema.rs b/src/repositories/balance/schema.rs new file mode 100644 index 0000000..7c3226a --- /dev/null +++ b/src/repositories/balance/schema.rs @@ -0,0 +1,18 @@ +//! +//! + +table! { + /// + api_kgon_balances(id) { + /// + id -> Integer, + /// + balance -> BigInt, + /// + balance_bota -> BigInt, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/game/mod.rs b/src/repositories/game/mod.rs index 2e71a26..bef7c9e 100644 --- a/src/repositories/game/mod.rs +++ b/src/repositories/game/mod.rs @@ -5,3 +5,5 @@ pub mod models; /// pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/game/models.rs b/src/repositories/game/models.rs index 5df3e3d..5d461a0 100644 --- a/src/repositories/game/models.rs +++ b/src/repositories/game/models.rs @@ -1,47 +1,92 @@ -use serde::{Deserialize, Serialize}; +use super::schema::api_kgon_games; +use beteran_common_rust as bcr; -pub struct Error { - pub code: i64, - pub msg: Option, +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "api_kgon_games"] +pub struct Game { + /// + 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, + /// + pub created_at: i64, + /// + pub updated_at: i64, } -pub struct FindVendorsRequest {} - -#[derive(Serialize, Deserialize, Debug)] -pub struct FindVendorsResponse { - pub code: i64, - pub msg: Option, - pub vendors: Vec, +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "api_kgon_games"] +pub struct NewGame { + /// + 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, } -pub struct FindGamesRequest { - pub vendor_key: String, +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "api_kgon_games"] +pub struct ModifyGame { + /// + pub key: String, + /// + pub names: String, + /// + pub platform: String, + /// + pub category: String, + /// + pub game_type: String, + /// + pub image: Option, } -#[derive(Serialize, Deserialize, Debug)] -pub struct FindGamesResponse { - pub code: i64, - pub msg: Option, - pub games: Vec, +/// +#[derive(Debug, Clone)] +pub struct FindAllSearch { + /// + pub vendor_id: Option, + /// + pub key_like: Option, + /// + pub platform_like: Option, + /// + pub category_like: Option, + /// + pub game_type_like: Option, } - -pub struct PlayRequest { - pub vendor_key: String, - pub game_key: String, - pub username: String, - pub nickname: String, - pub site_username: String, - pub group_key: Option, - pub amount: i64, - pub request_key: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PlayResponse { - pub code: i64, - pub msg: Option, - #[serde(rename = "userId")] - pub user_id: String, - pub url: String, - pub balance: i64, +/// +#[derive(Debug, Clone)] +pub struct FindAll { + pub search: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, } diff --git a/src/repositories/game/repository.rs b/src/repositories/game/repository.rs index dd4912a..7438a0a 100644 --- a/src/repositories/game/repository.rs +++ b/src/repositories/game/repository.rs @@ -1,150 +1,201 @@ -use super::models; -use std::{collections::HashMap, time::Duration}; +//! +//! +use super::{models, schema::api_kgon_games}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; /// -pub struct Repository { - client: reqwest::Client, - k_secret: String, - k_username: String, -} +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.identity").finish() + f.debug_struct("Repository of api_kgon_games").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() } } impl Repository { /// - pub fn new(k_secret: &str, k_username: &str) -> Repository { - Repository { - client: reqwest::Client::new(), - k_secret: k_secret.to_string(), - k_username: k_username.to_string(), + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_member: &models::NewGame, + ) -> Result { + let inserted = diesel::insert_into(api_kgon_games::table) + .values(new_member) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: i64, + ) -> Result, Error> { + match api_kgon_games::table.find(id).first::(conn) { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, } } /// - pub async fn find_vendors( + pub fn select_all_count( &self, - data: models::FindVendorsRequest, - ) -> Result { - 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 }); - } - }; + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result { + let mut q = api_kgon_games::table.into_boxed(); - match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), - }, - _ => Err(models::Error { code: 0, msg: None }), + if let Some(s) = &find_all.search { + if let Some(sp) = s.vendor_id { + q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp)); + } + if let Some(sp) = s.key_like { + q = q.filter(api_kgon_games::dsl::key.like(sp)); + } + if let Some(sp) = s.platform_like { + q = q.filter(api_kgon_games::dsl::platform.like(sp)); + } + if let Some(sp) = s.category_like { + q = q.filter(api_kgon_games::dsl::category.like(sp)); + } + if let Some(sp) = s.game_type_like { + q = q.filter(api_kgon_games::dsl::game_type.like(sp)); + } } + + q.count().get_result(conn) } /// - pub async fn find_games( + pub fn select_all( &self, - data: models::FindGamesRequest, - ) -> Result { - let mut params = HashMap::new(); + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result, Error> { + let mut q = api_kgon_games::table.into_boxed(); - params.insert("vendorKey", data.vendor_key); - - let res = match self - .client - .post("https://dev-mw.kgonapi.com/games") - .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()) - .form(¶ms) - .send() - .await - { - Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); + if let Some(s) = &find_all.search { + if let Some(sp) = s.vendor_id { + q = q.filter(api_kgon_games::dsl::vendor_id.eq(sp)); + } + if let Some(sp) = s.key_like { + q = q.filter(api_kgon_games::dsl::key.like(sp)); + } + if let Some(sp) = s.platform_like { + q = q.filter(api_kgon_games::dsl::platform.like(sp)); + } + if let Some(sp) = s.category_like { + q = q.filter(api_kgon_games::dsl::category.like(sp)); + } + if let Some(sp) = s.game_type_like { + q = q.filter(api_kgon_games::dsl::game_type.like(sp)); } - }; - - match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), - }, - _ => Err(models::Error { code: 0, msg: None }), } + + 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() { + "vendor_id" => { + q = q.order_by(api_kgon_games::vendor_id.asc()); + } + "key" => { + q = q.order_by(api_kgon_games::key.asc()); + } + "platform" => { + q = q.order_by(api_kgon_games::platform.asc()); + } + "category" => { + q = q.order_by(api_kgon_games::category.asc()); + } + "game_type" => { + q = q.order_by(api_kgon_games::game_type.asc()); + } + "created_at" => { + q = q.order_by(api_kgon_games::created_at.asc()); + } + "updated_at" => { + q = q.order_by(api_kgon_games::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "vendor_id" => { + q = q.order_by(api_kgon_games::vendor_id.desc()); + } + "key" => { + q = q.order_by(api_kgon_games::key.desc()); + } + "platform" => { + q = q.order_by(api_kgon_games::platform.desc()); + } + "category" => { + q = q.order_by(api_kgon_games::category.desc()); + } + "game_type" => { + q = q.order_by(api_kgon_games::game_type.desc()); + } + "created_at" => { + q = q.order_by(api_kgon_games::created_at.desc()); + } + "updated_at" => { + q = q.order_by(api_kgon_games::updated_at.desc()); + } + _ => {} + }, + }; + } + } + + q.load::(conn) } /// - pub async fn play( + pub fn update( &self, - data: models::PlayRequest, - ) -> Result { - let mut params = HashMap::new(); + conn: &diesel::PgConnection, + id: i64, + modify: &models::ModifyGame, + ) -> Result { + use api_kgon_games::dsl; - params.insert("vendorKey", data.vendor_key); - params.insert("gameKey", data.game_key); - params.insert("username", data.username); - params.insert("nickname", data.nickname); - params.insert("siteUsername", data.site_username); + diesel::update(dsl::api_kgon_games.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } - if let Some(group_key) = data.group_key { - params.insert("groupKey", group_key); - } + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result { + use api_kgon_games::dsl; - params.insert("amount", data.amount.to_string()); - - if let Some(request_key) = data.request_key { - params.insert("requestKey", request_key); - } - - let res = match self - .client - .post("https://dev-mw.kgonapi.com/play") - .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()) - .form(¶ms) - .timeout(Duration::from_secs(10)) - .send() - .await - { - Ok(res) => res, - Err(err) => { - return Err(models::Error { code: 0, msg: None }); - } - }; - - match res.status() { - reqwest::StatusCode::OK => match res.json::().await { - Ok(r) => Ok(r), - Err(e) => Err(models::Error { code: 0, msg: None }), - }, - _ => Err(models::Error { code: 0, msg: None }), - } + diesel::delete(api_kgon_games::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) } } diff --git a/src/repositories/game/schema.rs b/src/repositories/game/schema.rs new file mode 100644 index 0000000..4d5a2e0 --- /dev/null +++ b/src/repositories/game/schema.rs @@ -0,0 +1,28 @@ +//! +//! + +table! { + /// + api_kgon_games(id) { + /// + id -> BigInt, + /// + vendor_id -> BigInt, + /// + key -> Text, + /// + names -> Text, + /// + platform -> Text, + /// + category -> Text, + /// + game_type -> Text, + /// + image -> Nullable, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/identity/mod.rs b/src/repositories/member/mod.rs similarity index 72% rename from src/repositories/identity/mod.rs rename to src/repositories/member/mod.rs index 2e71a26..bef7c9e 100644 --- a/src/repositories/identity/mod.rs +++ b/src/repositories/member/mod.rs @@ -5,3 +5,5 @@ pub mod models; /// pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member/models.rs b/src/repositories/member/models.rs new file mode 100644 index 0000000..0b8d7e5 --- /dev/null +++ b/src/repositories/member/models.rs @@ -0,0 +1,78 @@ +use super::schema::api_kgon_members; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "api_kgon_members"] +pub struct Member { + /// + pub id: i64, + /// + pub balance: i64, + /// + pub balance_bota: i64, + /// + pub balance_sum: i64, + /// + pub companies: i64, + /// + pub oriental_play: bool, + /// + pub member_id: uuid::Uuid, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "api_kgon_members"] +pub struct NewMember { + /// + pub id: i64, + /// + pub balance: i64, + /// + pub balance_bota: i64, + /// + pub balance_sum: i64, + /// + pub companies: i64, + /// + pub oriental_play: bool, + /// + pub member_id: uuid::Uuid, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "api_kgon_members"] +pub struct ModifyMember { + /// + pub balance: i64, + /// + pub balance_bota: i64, + /// + pub balance_sum: i64, + /// + pub companies: i64, + /// + pub oriental_play: bool, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAllSearch { + /// + pub oriental_play: Option, +} +/// +#[derive(Debug, Clone)] +pub struct FindAll { + pub search: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member/repository.rs b/src/repositories/member/repository.rs new file mode 100644 index 0000000..2f959cf --- /dev/null +++ b/src/repositories/member/repository.rs @@ -0,0 +1,200 @@ +//! +//! +use super::{models, schema::api_kgon_members}; +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_members").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::NewMember, + ) -> Result { + let inserted = diesel::insert_into(api_kgon_members::table) + .values(new_member) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: i64, + ) -> Result, Error> { + match api_kgon_members::table + .find(id) + .first::(conn) + { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } + } + + /// + pub fn select_by_member_id( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + ) -> Result, Error> { + use api_kgon_members::dsl; + + match api_kgon_members::table + .filter(dsl::member_id.eq(member_id)) + .first::(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 { + let mut q = api_kgon_members::table.into_boxed(); + + if let Some(s) = &find_all.search { + if let Some(sp) = s.oriental_play { + q = q.filter(api_kgon_members::dsl::oriental_play.eq(sp)); + } + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result, Error> { + let mut q = api_kgon_members::table.into_boxed(); + + if let Some(s) = &find_all.search { + if let Some(sp) = s.oriental_play { + q = q.filter(api_kgon_members::dsl::oriental_play.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() { + "balance" => { + q = q.order_by(api_kgon_members::balance.asc()); + } + "balance_bota" => { + q = q.order_by(api_kgon_members::balance_bota.asc()); + } + "balance_sum" => { + q = q.order_by(api_kgon_members::balance_sum.asc()); + } + "companies" => { + q = q.order_by(api_kgon_members::companies.asc()); + } + "oriental_play" => { + q = q.order_by(api_kgon_members::oriental_play.asc()); + } + "created_at" => { + q = q.order_by(api_kgon_members::created_at.asc()); + } + "updated_at" => { + q = q.order_by(api_kgon_members::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "balance" => { + q = q.order_by(api_kgon_members::balance.desc()); + } + "balance_bota" => { + q = q.order_by(api_kgon_members::balance_bota.desc()); + } + "balance_sum" => { + q = q.order_by(api_kgon_members::balance_sum.desc()); + } + "companies" => { + q = q.order_by(api_kgon_members::companies.desc()); + } + "oriental_play" => { + q = q.order_by(api_kgon_members::oriental_play.desc()); + } + "created_at" => { + q = q.order_by(api_kgon_members::created_at.desc()); + } + "updated_at" => { + q = q.order_by(api_kgon_members::updated_at.desc()); + } + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: i64, + modify: &models::ModifyMember, + ) -> Result { + 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 { + use api_kgon_members::dsl; + + diesel::delete(api_kgon_members::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member/schema.rs b/src/repositories/member/schema.rs new file mode 100644 index 0000000..d9c6ec7 --- /dev/null +++ b/src/repositories/member/schema.rs @@ -0,0 +1,26 @@ +//! +//! + +table! { + /// + api_kgon_members(id) { + /// + id -> BigInt, + /// + balance -> BigInt, + /// + balance_bota -> BigInt, + /// + balance_sum -> BigInt, + /// + companies -> BigInt, + /// + oriental_play -> Bool, + /// + member_id -> Uuid, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs index 6c0af02..d2a9a94 100644 --- a/src/repositories/mod.rs +++ b/src/repositories/mod.rs @@ -1,4 +1,4 @@ -pub mod account; -pub mod betting; +pub mod balance; pub mod game; -pub mod identity; +pub mod member; +pub mod vendor; diff --git a/src/repositories/betting/mod.rs b/src/repositories/vendor/mod.rs similarity index 72% rename from src/repositories/betting/mod.rs rename to src/repositories/vendor/mod.rs index 2e71a26..bef7c9e 100644 --- a/src/repositories/betting/mod.rs +++ b/src/repositories/vendor/mod.rs @@ -5,3 +5,5 @@ pub mod models; /// pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/vendor/models.rs b/src/repositories/vendor/models.rs new file mode 100644 index 0000000..d26031d --- /dev/null +++ b/src/repositories/vendor/models.rs @@ -0,0 +1,108 @@ +use super::schema::api_kgon_vendors; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "api_kgon_vendors"] +pub struct Vendor { + /// + 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: bool, + /// + pub bet_count: i64, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "api_kgon_vendors"] +pub struct NewVendor { + /// + 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: bool, + /// + pub bet_count: i64, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "api_kgon_vendors"] +pub struct ModifyVendor { + /// + 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: bool, + /// + pub bet_count: i64, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAllSearch { + /// + pub company_id: Option, + /// + pub vendor_id: Option, + /// + pub key_like: Option, + /// + pub name_like: Option, + /// + pub category_like: Option, + /// + pub is_enable: Option, +} +/// +#[derive(Debug, Clone)] +pub struct FindAll { + pub search: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/vendor/repository.rs b/src/repositories/vendor/repository.rs new file mode 100644 index 0000000..db69782 --- /dev/null +++ b/src/repositories/vendor/repository.rs @@ -0,0 +1,234 @@ +//! +//! +use super::{models, schema::api_kgon_vendors}; +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_vendors").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::NewVendor, + ) -> Result { + let inserted = diesel::insert_into(api_kgon_vendors::table) + .values(new_member) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: i64, + ) -> Result, Error> { + match api_kgon_vendors::table + .find(id) + .first::(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 { + let mut q = api_kgon_vendors::table.into_boxed(); + + if let Some(s) = &find_all.search { + if let Some(sp) = s.company_id { + q = q.filter(api_kgon_vendors::dsl::company_id.eq(sp)); + } + if let Some(sp) = s.vendor_id { + q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp)); + } + if let Some(sp) = s.key_like { + q = q.filter(api_kgon_vendors::dsl::key.like(sp)); + } + if let Some(sp) = s.name_like { + q = q.filter(api_kgon_vendors::dsl::name.like(sp)); + } + if let Some(sp) = s.category_like { + q = q.filter(api_kgon_vendors::dsl::category.like(sp)); + } + if let Some(sp) = s.is_enable { + q = q.filter(api_kgon_vendors::dsl::is_enable.eq(sp)); + } + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result, Error> { + let mut q = api_kgon_vendors::table.into_boxed(); + + if let Some(s) = &find_all.search { + if let Some(sp) = s.company_id { + q = q.filter(api_kgon_vendors::dsl::company_id.eq(sp)); + } + if let Some(sp) = s.vendor_id { + q = q.filter(api_kgon_vendors::dsl::vendor_id.eq(sp)); + } + if let Some(sp) = s.key_like { + q = q.filter(api_kgon_vendors::dsl::key.like(sp)); + } + if let Some(sp) = s.name_like { + q = q.filter(api_kgon_vendors::dsl::name.like(sp)); + } + if let Some(sp) = s.category_like { + q = q.filter(api_kgon_vendors::dsl::category.like(sp)); + } + if let Some(sp) = s.is_enable { + q = q.filter(api_kgon_vendors::dsl::is_enable.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() { + "company_id" => { + q = q.order_by(api_kgon_vendors::company_id.asc()); + } + "vendor_id" => { + q = q.order_by(api_kgon_vendors::vendor_id.asc()); + } + "key" => { + q = q.order_by(api_kgon_vendors::key.asc()); + } + "name" => { + q = q.order_by(api_kgon_vendors::name.asc()); + } + "category" => { + q = q.order_by(api_kgon_vendors::category.asc()); + } + "max_bet_casino" => { + q = q.order_by(api_kgon_vendors::max_bet_casino.asc()); + } + "max_bet_slot" => { + q = q.order_by(api_kgon_vendors::max_bet_slot.asc()); + } + "is_enable" => { + q = q.order_by(api_kgon_vendors::is_enable.asc()); + } + "bet_count" => { + q = q.order_by(api_kgon_vendors::bet_count.asc()); + } + "created_at" => { + q = q.order_by(api_kgon_vendors::created_at.asc()); + } + "updated_at" => { + q = q.order_by(api_kgon_vendors::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "company_id" => { + q = q.order_by(api_kgon_vendors::company_id.desc()); + } + "vendor_id" => { + q = q.order_by(api_kgon_vendors::vendor_id.desc()); + } + "key" => { + q = q.order_by(api_kgon_vendors::key.desc()); + } + "name" => { + q = q.order_by(api_kgon_vendors::name.desc()); + } + "category" => { + q = q.order_by(api_kgon_vendors::category.desc()); + } + "max_bet_casino" => { + q = q.order_by(api_kgon_vendors::max_bet_casino.desc()); + } + "max_bet_slot" => { + q = q.order_by(api_kgon_vendors::max_bet_slot.desc()); + } + "is_enable" => { + q = q.order_by(api_kgon_vendors::is_enable.desc()); + } + "bet_count" => { + q = q.order_by(api_kgon_vendors::bet_count.desc()); + } + "created_at" => { + q = q.order_by(api_kgon_vendors::created_at.desc()); + } + "updated_at" => { + q = q.order_by(api_kgon_vendors::updated_at.desc()); + } + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: i64, + modify: &models::ModifyVendor, + ) -> Result { + use api_kgon_vendors::dsl; + + diesel::update(dsl::api_kgon_vendors.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: i64) -> Result { + use api_kgon_vendors::dsl; + + diesel::delete(api_kgon_vendors::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/vendor/schema.rs b/src/repositories/vendor/schema.rs new file mode 100644 index 0000000..4d3729f --- /dev/null +++ b/src/repositories/vendor/schema.rs @@ -0,0 +1,32 @@ +//! +//! + +table! { + /// + api_kgon_vendors(id) { + /// + id -> BigInt, + /// + company_id -> BigInt, + /// + vendor_id -> BigInt, + /// + key -> Text, + /// + name -> Text, + /// + category -> Text, + /// + max_bet_casino -> BigInt, + /// + max_bet_slot -> BigInt, + /// + is_enable -> Bool, + /// + bet_count -> BigInt, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/schedulers/balance/mod.rs b/src/schedulers/balance/mod.rs new file mode 100644 index 0000000..81b3546 --- /dev/null +++ b/src/schedulers/balance/mod.rs @@ -0,0 +1 @@ +pub mod scheduler; diff --git a/src/schedulers/balance/scheduler.rs b/src/schedulers/balance/scheduler.rs new file mode 100644 index 0000000..90f4a48 --- /dev/null +++ b/src/schedulers/balance/scheduler.rs @@ -0,0 +1,15 @@ +/// +pub struct Scheduler {} + +impl std::fmt::Debug for Scheduler { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Scheduler of api.kgon.identity").finish() + } +} + +impl Scheduler { + /// + pub fn new() -> Scheduler { + Scheduler {} + } +} diff --git a/src/schedulers/game/mod.rs b/src/schedulers/game/mod.rs new file mode 100644 index 0000000..81b3546 --- /dev/null +++ b/src/schedulers/game/mod.rs @@ -0,0 +1 @@ +pub mod scheduler; diff --git a/src/schedulers/game/scheduler.rs b/src/schedulers/game/scheduler.rs new file mode 100644 index 0000000..90f4a48 --- /dev/null +++ b/src/schedulers/game/scheduler.rs @@ -0,0 +1,15 @@ +/// +pub struct Scheduler {} + +impl std::fmt::Debug for Scheduler { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Scheduler of api.kgon.identity").finish() + } +} + +impl Scheduler { + /// + pub fn new() -> Scheduler { + Scheduler {} + } +} diff --git a/src/schedulers/member/mod.rs b/src/schedulers/member/mod.rs new file mode 100644 index 0000000..81b3546 --- /dev/null +++ b/src/schedulers/member/mod.rs @@ -0,0 +1 @@ +pub mod scheduler; diff --git a/src/schedulers/member/scheduler.rs b/src/schedulers/member/scheduler.rs new file mode 100644 index 0000000..e483b19 --- /dev/null +++ b/src/schedulers/member/scheduler.rs @@ -0,0 +1,19 @@ +use tokio_cron_scheduler::{Job, JobScheduler, JobToRun}; + +/// +pub struct Scheduler {} + +impl std::fmt::Debug for Scheduler { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Scheduler of api.kgon.identity").finish() + } +} + +impl Scheduler { + /// + pub fn new() -> Scheduler { + let mut sched = JobScheduler::new(); + + Scheduler {} + } +} diff --git a/src/schedulers/mod.rs b/src/schedulers/mod.rs new file mode 100644 index 0000000..d2a9a94 --- /dev/null +++ b/src/schedulers/mod.rs @@ -0,0 +1,4 @@ +pub mod balance; +pub mod game; +pub mod member; +pub mod vendor; diff --git a/src/schedulers/vendor/mod.rs b/src/schedulers/vendor/mod.rs new file mode 100644 index 0000000..81b3546 --- /dev/null +++ b/src/schedulers/vendor/mod.rs @@ -0,0 +1 @@ +pub mod scheduler; diff --git a/src/schedulers/vendor/scheduler.rs b/src/schedulers/vendor/scheduler.rs new file mode 100644 index 0000000..929ae0f --- /dev/null +++ b/src/schedulers/vendor/scheduler.rs @@ -0,0 +1,87 @@ +use std::time::Duration; + +use crate::repositories; +use diesel::{ + r2d2::{ConnectionManager, Pool}, + PgConnection, +}; +use tokio_cron_scheduler::{Job, JobScheduler}; + +/// +pub struct Scheduler { + pool: Pool>, + sched: JobScheduler, + vendor_repository: repositories::vendor::repository::Repository, +} + +impl std::fmt::Debug for Scheduler { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Scheduler of api.kgon.identity").finish() + } +} + +impl Scheduler { + /// + pub fn new(pool: Pool>, sched: JobScheduler) -> Scheduler { + Scheduler { + pool, + sched, + vendor_repository: repositories::vendor::repository::Repository::new(), + } + } + + pub async fn queue(&self) -> Result<(), std::boxed::Box> { + self.list_members().await?; + + Ok(()) + } + + async fn list_members(&self) -> Result<(), Box> { + let mut jj = Job::new_repeated(Duration::from_secs(60), |_uuid, _l| { + println!("{:?} I'm repeated every 60 seconds", chrono::Utc::now()); + }) + .unwrap(); + + jj.on_start_notification_add( + &self.sched, + Box::new(|job_id, notification_id, type_of_notification| { + Box::pin(async move { + println!( + "Job {:?} was started, notification {:?} ran ({:?})", + job_id, notification_id, type_of_notification + ); + }) + }), + ) + .await; + + jj.on_stop_notification_add( + &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(()) + } +} diff --git a/src/services/betting/mod.rs b/src/services/betting/mod.rs index e69de29..1f278a4 100644 --- a/src/services/betting/mod.rs +++ b/src/services/betting/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/services/betting/service.rs b/src/services/betting/service.rs new file mode 100644 index 0000000..ba4fce6 --- /dev/null +++ b/src/services/betting/service.rs @@ -0,0 +1,40 @@ +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Service of api.kgon.identity").finish() + } +} + +impl Service { + /// + pub fn new( + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, + ) -> Service { + Service { + connection_broker, + queue_broker, + k_secret, + k_username, + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) + } + + async fn list_history(&self) -> Result<(), Box> {} + async fn list_history_for_pragmatic(&self) -> Result<(), Box> {} + async fn list_history_for_evolution(&self) -> Result<(), Box> {} + + async fn statistics(&self) -> Result<(), Box> {} +} diff --git a/src/services/game/mod.rs b/src/services/game/mod.rs index e69de29..1f278a4 100644 --- a/src/services/game/mod.rs +++ b/src/services/game/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/services/identity/service.rs b/src/services/game/service.rs similarity index 67% rename from src/services/identity/service.rs rename to src/services/game/service.rs index 220852f..3178e3c 100644 --- a/src/services/identity/service.rs +++ b/src/services/game/service.rs @@ -29,6 +29,11 @@ impl Service { } pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { - futures::try_join!().map(|_| ()) + futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) } + + async fn list_vendors(&self) -> Result<(), Box> {} + async fn list_games(&self) -> Result<(), Box> {} + + async fn execute_game(&self) -> Result<(), Box> {} } diff --git a/src/services/identity/mod.rs b/src/services/member/mod.rs similarity index 100% rename from src/services/identity/mod.rs rename to src/services/member/mod.rs diff --git a/src/services/member/service.rs b/src/services/member/service.rs new file mode 100644 index 0000000..ff9f7c4 --- /dev/null +++ b/src/services/member/service.rs @@ -0,0 +1,40 @@ +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Service of api.kgon.identity").finish() + } +} + +impl Service { + /// + pub fn new( + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, + ) -> Service { + Service { + connection_broker, + queue_broker, + k_secret, + k_username, + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) + } + + async fn create_member(&self) -> Result<(), Box> {} + + async fn list_members(&self) -> Result<(), Box> {} + + async fn get_member(&self) -> Result<(), Box> {} +} diff --git a/src/services/member_account/mod.rs b/src/services/member_account/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/services/member_account/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/services/member_account/service.rs b/src/services/member_account/service.rs new file mode 100644 index 0000000..3ab0f0c --- /dev/null +++ b/src/services/member_account/service.rs @@ -0,0 +1,41 @@ +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Service of api.kgon.identity").finish() + } +} + +impl Service { + /// + pub fn new( + connection_broker: nats::asynk::Connection, + queue_broker: String, + k_secret: String, + k_username: String, + ) -> Service { + Service { + connection_broker, + queue_broker, + k_secret, + k_username, + } + } + + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!(self.create_member(), self.list_members(), self.get_member(),).map(|_| ()) + } + + async fn create_deposit(&self) -> Result<(), Box> {} + async fn create_withdraw(&self) -> Result<(), Box> {} + + async fn list_partner_balnace(&self) -> Result<(), Box> {} + async fn list_user_balnace(&self) -> Result<(), Box> {} +} diff --git a/src/services/mod.rs b/src/services/mod.rs index 6c0af02..0e5fc3e 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,4 +1,4 @@ -pub mod account; pub mod betting; pub mod game; -pub mod identity; +pub mod member; +pub mod member_account;