Skip to content

Commit b70c9b1

Browse files
committed
add user test db
1 parent 0b75b76 commit b70c9b1

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed

dao/src/db.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use sqlx::{mysql::MySqlPool, Pool, MySql};
2+
use once_cell::sync::Lazy;
3+
use std::sync::Mutex;
4+
use utils::config::load_config;
5+
6+
static DB_POOL: Lazy<Mutex<Option<Pool<MySql>>>> = Lazy::new(|| Mutex::new(None));
7+
8+
pub async fn get_db_pool() -> Result<Pool<MySql>, sqlx::Error> {
9+
// load config
10+
let config = load_config();
11+
let mut pool_lock = DB_POOL.lock().unwrap();
12+
13+
// check if the pool has already been initialized
14+
if let Some(pool) = &*pool_lock {
15+
return Ok(pool.clone());
16+
}
17+
18+
// load database url from config file
19+
let database_url = config.database.url.clone();
20+
21+
// create a new pool
22+
let pool = MySqlPool::connect(&database_url).await?;
23+
24+
// set the pool in the static variable
25+
*pool_lock = Some(pool.clone());
26+
27+
Ok(pool)
28+
}

dao/src/user/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod user_repository;

dao/src/user/user_repository.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use sqlx::MySqlPool;
2+
use domain::entity::user::user::SysUser;
3+
4+
pub async fn get_all_users(pool: &MySqlPool) -> Result<Vec<SysUser>, sqlx::Error> {
5+
let users = sqlx::query_as::<_, SysUser>("SELECT * FROM sys_user")
6+
.fetch_all(pool)
7+
.await?;
8+
9+
Ok(users)
10+
}

domain/src/entity/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod user;

domain/src/entity/user/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod user;

domain/src/entity/user/user.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use chrono::{NaiveDateTime};
2+
use serde::{Deserialize, Serialize};
3+
use sqlx::FromRow;
4+
use utils::naive_datetime_format;
5+
6+
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
7+
pub struct SysUser {
8+
pub id: Option<i64>, // 对应 Long 类型
9+
pub name: Option<String>, // 用户姓名
10+
pub user_name: Option<String>, // 登录用户名
11+
pub avatar: Option<String>, // 头像
12+
pub password: Option<String>, // 登录密码
13+
pub leader_flag: Option<bool>, // 是否经理
14+
pub position: Option<String>, // 职位
15+
pub email: Option<String>, // 电子邮件
16+
pub phone_number: Option<String>, // 手机号码
17+
pub system_language: Option<String>, // 系统语言
18+
pub is_manager: Option<i32>, // 是否为管理者 0==管理者 1==员工
19+
pub is_system: Option<i32>, // 是否系统自带数据
20+
pub status: Option<i32>, // 状态 0:正常,1:删除,2:封禁
21+
pub description: Option<String>, // 用户描述
22+
pub remark: Option<String>, // 备注
23+
pub wechat_open_id: Option<String>, // 微信绑定
24+
pub tenant_id: Option<i64>, // 租户id
25+
26+
// NaiveDateTime添加序列化和反序列化处理
27+
#[serde(with = "naive_datetime_format")]
28+
pub create_time: Option<NaiveDateTime>, // 创建时间
29+
30+
#[serde(with = "naive_datetime_format")]
31+
pub update_time: Option<NaiveDateTime>, // 更新时间
32+
33+
pub create_by: Option<i64>, // 创建人
34+
pub update_by: Option<i64>, // 修改人
35+
pub delete_flag: Option<i32>, // 删除标记
36+
}
37+
38+
impl SysUser {
39+
pub fn new(
40+
id: Option<i64>,
41+
name: Option<String>,
42+
user_name: Option<String>,
43+
avatar: Option<String>,
44+
password: Option<String>,
45+
leader_flag: Option<bool>,
46+
position: Option<String>,
47+
email: Option<String>,
48+
phone_number: Option<String>,
49+
system_language: Option<String>,
50+
is_manager: Option<i32>,
51+
is_system: Option<i32>,
52+
status: Option<i32>,
53+
description: Option<String>,
54+
remark: Option<String>,
55+
wechat_open_id: Option<String>,
56+
tenant_id: Option<i64>,
57+
create_time: Option<NaiveDateTime>,
58+
update_time: Option<NaiveDateTime>,
59+
create_by: Option<i64>,
60+
update_by: Option<i64>,
61+
delete_flag: Option<i32>,
62+
) -> Self {
63+
SysUser {
64+
id,
65+
name,
66+
user_name,
67+
avatar,
68+
password,
69+
leader_flag,
70+
position,
71+
email,
72+
phone_number,
73+
system_language,
74+
is_manager,
75+
is_system,
76+
status,
77+
description,
78+
remark,
79+
wechat_open_id,
80+
tenant_id,
81+
create_time,
82+
update_time,
83+
create_by,
84+
update_by,
85+
delete_flag,
86+
}
87+
}
88+
}

utils/src/naive_datetime_format.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use chrono::NaiveDateTime;
2+
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
3+
4+
// custom format for NaiveDateTime to string
5+
const FORMAT: &str = "%Y-%m-%d %H:%M:%S";
6+
7+
pub fn serialize<S>(dt: &Option<NaiveDateTime>, serializer: S) -> Result<S::Ok, S::Error>
8+
where
9+
S: Serializer,
10+
{
11+
match *dt {
12+
Some(ref dt) => dt.format(FORMAT).to_string().serialize(serializer),
13+
None => "".serialize(serializer), // for None, serialize as empty string
14+
}
15+
}
16+
17+
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<NaiveDateTime>, D::Error>
18+
where
19+
D: Deserializer<'de>,
20+
{
21+
let opt_str: Option<String> = Option::deserialize(deserializer)?;
22+
match opt_str {
23+
Some(s) => NaiveDateTime::parse_from_str(&s, FORMAT)
24+
.map(Some)
25+
.map_err(serde::de::Error::custom),
26+
None => Ok(None),
27+
}
28+
}

0 commit comments

Comments
 (0)