修改用户实体类

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",
"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",

View File

@ -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"]}

View File

@ -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<String>,
pub avatar_url: Option<String>,
pub lang_tag: String,
pub location: Option<String>,
pub timezone: Option<String>,
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 {
pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result<Account, sqlx::Error> {
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<Account, sqlx::Error> {
// 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<Account, sqlx::Error> {
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<Account, sqlx::Error> {
// 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<Account, sqlx::Error> {
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
}
}

View File

@ -5,27 +5,11 @@ use library::resp::response::ResErr::{ErrPerm, ErrSystem};
use library::resp::response::{ResOK, ResResult};
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 {
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 {