diff --git a/api/src/controller/player_info.rs b/api/src/controller/player_info.rs index 7194aab..02fc848 100644 --- a/api/src/controller/player_info.rs +++ b/api/src/controller/player_info.rs @@ -1,17 +1,17 @@ use axum::Json; use axum_extra::extract::WithRejection; -use domain::dto::player_info::PlayerInfoRegister; +use domain::dto::account::AccountRegister; use library::resp::{rejection::IRejection, response::{ResErr, ResOK, ResResult}}; use validator::Validate; pub async fn create( - WithRejection(Json(req), _): IRejection> + WithRejection(Json(req), _): IRejection> ) -> ResResult> { if let Err(err) = req.validate() { return Err(ResErr::ErrParams(Some(err.to_string()))); } - service::player::register(req).await?; + service::account::register(req).await?; Ok(ResOK(None)) } diff --git a/domain/src/dto/player_info.rs b/domain/src/dto/account.rs similarity index 97% rename from domain/src/dto/player_info.rs rename to domain/src/dto/account.rs index 2d3d981..7c38e94 100644 --- a/domain/src/dto/player_info.rs +++ b/domain/src/dto/account.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use validator::Validate; #[derive(Debug, Validate, Deserialize, Serialize)] -pub struct PlayerInfoRegister { +pub struct AccountRegister { #[validate(required(message = "用户名称不能为空"), length(min = 1, message = "用户名称不能为空"))] pub username: Option, #[validate(required(message = "电子邮箱不能为空"), length(min = 1, message = "电子邮箱不能为空"))] diff --git a/domain/src/dto/mod.rs b/domain/src/dto/mod.rs index 484ac63..e9ddf8c 100644 --- a/domain/src/dto/mod.rs +++ b/domain/src/dto/mod.rs @@ -1,3 +1,3 @@ -pub mod player_info; +pub mod account; pub mod feedback; pub mod pageable; \ No newline at end of file diff --git a/domain/src/entities/account.rs b/domain/src/entities/account.rs index 972230f..7e9d7e9 100644 --- a/domain/src/entities/account.rs +++ b/domain/src/entities/account.rs @@ -1,14 +1,47 @@ +use chrono::NaiveDateTime; +use sqlx::PgPool; +use sqlx::types::chrono; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct Account { - pub id: u64, + pub id: i64, pub username: String, - pub password: String, - pub salt: String, - pub role: i8, - pub realname: String, - pub login_at: i64, - pub login_token: String, - pub created_at: i64, - pub updated_at: i64, + pub email: String, + pub platform_id: String, + pub user_type: String, + pub country_code: String, + pub created_at: NaiveDateTime, + pub updated_at: NaiveDateTime, } + +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 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(); + 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 + } +} \ No newline at end of file diff --git a/domain/src/entities/mod.rs b/domain/src/entities/mod.rs index 1aaf1c2..93d4fd5 100644 --- a/domain/src/entities/mod.rs +++ b/domain/src/entities/mod.rs @@ -1,4 +1,3 @@ pub mod account; -pub mod player; pub mod feedback; diff --git a/domain/src/entities/player.rs b/domain/src/entities/player.rs deleted file mode 100644 index 3d2c802..0000000 --- a/domain/src/entities/player.rs +++ /dev/null @@ -1,45 +0,0 @@ -use chrono::NaiveDateTime; -use sqlx::PgPool; -use sqlx::types::chrono; - -#[derive(Debug, Clone, Default)] -pub struct Player { - pub id: i64, - 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, -} - -impl Player { - pub async fn find_by_platform_id(platform_id: &str, db_pool: &PgPool) -> Result { - sqlx::query_as!( - Player, - r#"select * from player where platform_id = $1"#, - platform_id - ) - .fetch_one(db_pool) - .await - } - - pub async fn add_player_info(player_info: &mut Player, db_pool: &PgPool) -> Result { - player_info.created_at = chrono::Local::now().naive_local(); - sqlx::query_as!( - Player, - r#" - insert into player - (username, email, platform_id, user_type, country_code, created_at) - values - ($1, $2, $3, $4, $5, $6) returning *"#, - player_info.username, - player_info.email, - player_info.platform_id, - player_info.user_type, - player_info.country_code, - player_info.created_at, - ).fetch_one(db_pool).await - } -} \ No newline at end of file diff --git a/service/src/player.rs b/service/src/account.rs similarity index 76% rename from service/src/player.rs rename to service/src/account.rs index 94e3c27..015bf08 100644 --- a/service/src/player.rs +++ b/service/src/account.rs @@ -1,11 +1,11 @@ -use domain::dto::player_info::PlayerInfoRegister; -use domain::entities::player::Player; +use domain::dto::account::AccountRegister; +use domain::entities::account::Account; use library::db; use library::resp::response::ResErr::{ErrPerm, ErrSystem}; use library::resp::response::{ResOK, ResResult}; -pub async fn register(req: PlayerInfoRegister) -> ResResult> { - match Player::find_by_platform_id(req.platform_id.clone().unwrap().as_mut_str(), db!()) +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) => { @@ -19,8 +19,8 @@ pub async fn register(req: PlayerInfoRegister) -> ResResult> { } } - match Player::add_player_info( - &mut Player { + match Account::add_account_info( + &mut Account { username: req.username.unwrap(), email: req.email.unwrap(), platform_id: req.platform_id.unwrap(), diff --git a/service/src/lib.rs b/service/src/lib.rs index e159cbd..ed16296 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -1,2 +1,2 @@ -pub mod player; +pub mod account; pub mod feedback;