From 53695b468ea7a9071f039c930e086a1dc8770efc Mon Sep 17 00:00:00 2001 From: liyunjia Date: Sat, 25 May 2024 22:14:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 4 ++ domain/Cargo.toml | 2 +- domain/src/entities/account.rs | 96 ++++++++++++++++++++++------------ service/src/account.rs | 20 +------ 4 files changed, 71 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a142ee5..eded4c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2097,6 +2097,7 @@ dependencies = [ "smallvec", "sqlformat", "thiserror", + "time", "tokio", "tokio-stream", "tracing", @@ -2183,6 +2184,7 @@ dependencies = [ "sqlx-core", "stringprep", "thiserror", + "time", "tracing", "uuid", "whoami", @@ -2223,6 +2225,7 @@ dependencies = [ "sqlx-core", "stringprep", "thiserror", + "time", "tracing", "uuid", "whoami", @@ -2247,6 +2250,7 @@ dependencies = [ "percent-encoding", "serde", "sqlx-core", + "time", "tracing", "url", "urlencoding", diff --git a/domain/Cargo.toml b/domain/Cargo.toml index c241f98..0133359 100644 --- a/domain/Cargo.toml +++ b/domain/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] serde = { workspace = true, features = ["derive"] } -sqlx = { workspace = true, features = ["postgres", "uuid", "macros", "sqlx-macros", "chrono"] } +sqlx = { workspace = true, features = ["postgres", "uuid", "macros", "sqlx-macros", "chrono", "time", "sqlx-postgres"] } validator = { workspace = true, features = ["derive"] } chrono = { workspace = true, features = ["serde"]} diff --git a/domain/src/entities/account.rs b/domain/src/entities/account.rs index 7e9d7e9..4175635 100644 --- a/domain/src/entities/account.rs +++ b/domain/src/entities/account.rs @@ -1,47 +1,79 @@ -use chrono::NaiveDateTime; +use chrono::{DateTime, Utc}; use sqlx::PgPool; -use sqlx::types::chrono; +use sqlx::types::{chrono, JsonValue}; +use sqlx::types::uuid::Bytes; #[derive(Debug, Clone, Default)] pub struct Account { - pub id: i64, + pub id: String, pub username: String, - pub email: String, - pub platform_id: String, - pub user_type: String, - pub country_code: String, - pub created_at: NaiveDateTime, - pub updated_at: NaiveDateTime, + pub display_name: Option, + pub avatar_url: Option, + pub lang_tag: String, + pub location: Option, + pub timezone: Option, + pub metadata: JsonValue, + pub wallet: JsonValue, + pub email: Option, + pub password: Option>, + pub facebook_id: Option, + pub google_id: Option, + pub gamecenter_id: Option, + pub steam_id: Option, + pub custom_id: Option, + pub apple_id: Option, + pub facebook_instant_game_id: Option, + pub weixin_id: Option, + pub douyin_id: Option, + pub create_time: DateTime, + pub update_time: DateTime, + pub verify_time: DateTime, + pub disable_time: DateTime, } impl Account { - pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result { - sqlx::query_as!( - Account, - r#"select * from account where platform_id = $1"#, - platform_id - ) - .fetch_one(db_pool) - .await - } + // pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result { + // sqlx::query_as!( + // Account, + // r#"select * from account where platform_id = $1"#, + // platform_id + // ) + // .fetch_one(db_pool) + // .await + // } - pub async fn add_account_info(player_info: &mut Account, db_pool: &PgPool) -> Result { - player_info.created_at = chrono::Local::now().naive_local(); - player_info.updated_at = chrono::Local::now().naive_local(); + // pub async fn add_account_info(player_info: &mut Account, db_pool: &PgPool) -> Result { + // sqlx::query_as!( + // Account, + // r#" + // insert into account + // (username, email, platform_id, user_type, country_code, created_at, updated_at) + // values + // ($1, $2, $3, $4, $5, $6, $7) returning *"#, + // player_info.username, + // player_info.email, + // player_info.platform_id, + // player_info.user_type, + // player_info.country_code, + // player_info.created_at, + // player_info.updated_at + // ).fetch_one(db_pool).await + // } + + pub async fn add_google_account(&self, db_pool: &PgPool) -> Result { sqlx::query_as!( Account, r#" - insert into account - (username, email, platform_id, user_type, country_code, created_at, updated_at) - values - ($1, $2, $3, $4, $5, $6, $7) returning *"#, - player_info.username, - player_info.email, - player_info.platform_id, - player_info.user_type, - player_info.country_code, - player_info.created_at, - player_info.updated_at + insert into account + (username, google_id, email, display_name, avatar_url) + values + ($1, $2, $3, $4, $5) returning * + "#, + self.username, + self.google_id.clone().unwrap(), + self.email.clone().unwrap(), + self.display_name.clone().unwrap(), + self.avatar_url.clone().unwrap() ).fetch_one(db_pool).await } } \ No newline at end of file diff --git a/service/src/account.rs b/service/src/account.rs index 015bf08..a75cea2 100644 --- a/service/src/account.rs +++ b/service/src/account.rs @@ -5,27 +5,11 @@ use library::resp::response::ResErr::{ErrPerm, ErrSystem}; use library::resp::response::{ResOK, ResResult}; pub async fn register(req: AccountRegister) -> ResResult> { - match Account::find_by_platform_id(req.platform_id.clone().unwrap().as_mut_str(), db!()) - .await - { - Err(err) => { - tracing::error!(error = ?err, "查询账号失败"); - return Err(ErrSystem(None)); - } - Ok(v) => { - if v.id > 0 { - return Err(ErrPerm(Some("用户已存在".to_string()))); - } - } - } - match Account::add_account_info( + match Account::add_google_account( &mut Account { username: req.username.unwrap(), - email: req.email.unwrap(), - platform_id: req.platform_id.unwrap(), - user_type: req.user_type.unwrap(), - country_code: req.country_code.unwrap(), + email: req.email, ..Default::default() }, db!() ).await {