修改用户实体类

This commit is contained in:
liyunjia 2024-05-25 22:14:19 +08:00
parent 486e488fd0
commit 53695b468e
4 changed files with 71 additions and 51 deletions

4
Cargo.lock generated
View File

@ -2097,6 +2097,7 @@ dependencies = [
"smallvec", "smallvec",
"sqlformat", "sqlformat",
"thiserror", "thiserror",
"time",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tracing", "tracing",
@ -2183,6 +2184,7 @@ dependencies = [
"sqlx-core", "sqlx-core",
"stringprep", "stringprep",
"thiserror", "thiserror",
"time",
"tracing", "tracing",
"uuid", "uuid",
"whoami", "whoami",
@ -2223,6 +2225,7 @@ dependencies = [
"sqlx-core", "sqlx-core",
"stringprep", "stringprep",
"thiserror", "thiserror",
"time",
"tracing", "tracing",
"uuid", "uuid",
"whoami", "whoami",
@ -2247,6 +2250,7 @@ dependencies = [
"percent-encoding", "percent-encoding",
"serde", "serde",
"sqlx-core", "sqlx-core",
"time",
"tracing", "tracing",
"url", "url",
"urlencoding", "urlencoding",

View File

@ -7,7 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
serde = { workspace = true, features = ["derive"] } 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"] } validator = { workspace = true, features = ["derive"] }
chrono = { workspace = true, features = ["serde"]} chrono = { workspace = true, features = ["serde"]}

View File

@ -1,47 +1,79 @@
use chrono::NaiveDateTime; use chrono::{DateTime, Utc};
use sqlx::PgPool; use sqlx::PgPool;
use sqlx::types::chrono; use sqlx::types::{chrono, JsonValue};
use sqlx::types::uuid::Bytes;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Account { pub struct Account {
pub id: i64, pub id: String,
pub username: String, pub username: String,
pub email: String, pub display_name: Option<String>,
pub platform_id: String, pub avatar_url: Option<String>,
pub user_type: String, pub lang_tag: String,
pub country_code: String, pub location: Option<String>,
pub created_at: NaiveDateTime, pub timezone: Option<String>,
pub updated_at: NaiveDateTime, pub metadata: JsonValue,
pub wallet: JsonValue,
pub email: Option<String>,
pub password: Option<Vec<u8>>,
pub facebook_id: Option<String>,
pub google_id: Option<String>,
pub gamecenter_id: Option<String>,
pub steam_id: Option<String>,
pub custom_id: Option<String>,
pub apple_id: Option<String>,
pub facebook_instant_game_id: Option<String>,
pub weixin_id: Option<String>,
pub douyin_id: Option<String>,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub verify_time: DateTime<Utc>,
pub disable_time: DateTime<Utc>,
} }
impl Account { impl Account {
pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result<Account, sqlx::Error> { // pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result<Account, sqlx::Error> {
sqlx::query_as!( // sqlx::query_as!(
Account, // Account,
r#"select * from account where platform_id = $1"#, // r#"select * from account where platform_id = $1"#,
platform_id // platform_id
) // )
.fetch_one(db_pool) // .fetch_one(db_pool)
.await // .await
} // }
pub async fn add_account_info(player_info: &mut Account, db_pool: &PgPool) -> Result<Account, sqlx::Error> { // pub async fn add_account_info(player_info: &mut Account, db_pool: &PgPool) -> Result<Account, sqlx::Error> {
player_info.created_at = chrono::Local::now().naive_local(); // sqlx::query_as!(
player_info.updated_at = chrono::Local::now().naive_local(); // 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<Account, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Account, Account,
r#" r#"
insert into account insert into account
(username, email, platform_id, user_type, country_code, created_at, updated_at) (username, google_id, email, display_name, avatar_url)
values values
($1, $2, $3, $4, $5, $6, $7) returning *"#, ($1, $2, $3, $4, $5) returning *
player_info.username, "#,
player_info.email, self.username,
player_info.platform_id, self.google_id.clone().unwrap(),
player_info.user_type, self.email.clone().unwrap(),
player_info.country_code, self.display_name.clone().unwrap(),
player_info.created_at, self.avatar_url.clone().unwrap()
player_info.updated_at
).fetch_one(db_pool).await ).fetch_one(db_pool).await
} }
} }

View File

@ -5,27 +5,11 @@ use library::resp::response::ResErr::{ErrPerm, ErrSystem};
use library::resp::response::{ResOK, ResResult}; use library::resp::response::{ResOK, ResResult};
pub async fn register(req: AccountRegister) -> ResResult<ResOK<()>> { pub async fn register(req: AccountRegister) -> ResResult<ResOK<()>> {
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 { &mut Account {
username: req.username.unwrap(), username: req.username.unwrap(),
email: req.email.unwrap(), email: req.email,
platform_id: req.platform_id.unwrap(),
user_type: req.user_type.unwrap(),
country_code: req.country_code.unwrap(),
..Default::default() ..Default::default()
}, db!() }, db!()
).await { ).await {