修改player 为 account

This commit is contained in:
liyunjia 2024-05-24 21:35:12 +08:00
parent 851700065a
commit 4917b8bcf2
8 changed files with 55 additions and 68 deletions

View File

@ -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<Json<PlayerInfoRegister>>
WithRejection(Json(req), _): IRejection<Json<AccountRegister>>
) -> ResResult<ResOK<()>> {
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))
}

View File

@ -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<String>,
#[validate(required(message = "电子邮箱不能为空"), length(min = 1, message = "电子邮箱不能为空"))]

View File

@ -1,3 +1,3 @@
pub mod player_info;
pub mod account;
pub mod feedback;
pub mod pageable;

View File

@ -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<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();
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
}
}

View File

@ -1,4 +1,3 @@
pub mod account;
pub mod player;
pub mod feedback;

View File

@ -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<Player, sqlx::Error> {
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, sqlx::Error> {
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
}
}

View File

@ -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<ResOK<()>> {
match Player::find_by_platform_id(req.platform_id.clone().unwrap().as_mut_str(), db!())
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) => {
@ -19,8 +19,8 @@ pub async fn register(req: PlayerInfoRegister) -> ResResult<ResOK<()>> {
}
}
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(),

View File

@ -1,2 +1,2 @@
pub mod player;
pub mod account;
pub mod feedback;