Skip to content

Commit d7ebab6

Browse files
committed
fetch: Add user domain
1 parent d9c4554 commit d7ebab6

File tree

10 files changed

+213
-0
lines changed

10 files changed

+213
-0
lines changed

domain/src/dto/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod user;
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use serde::{Deserialize, Serialize};
2+
use sqlx::FromRow;
3+
4+
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
5+
pub struct AccountLoginDTO {
6+
pub username: Option<String>, // 用户昵称(别名 姓名)
7+
8+
pub password: Option<String>, // 职位
9+
}

domain/src/dto/user/mod.rs

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

domain/src/vo/mod.rs

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

domain/src/vo/user/mod.rs

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

domain/src/vo/user/user_info_vo.rs

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use serde::{Serialize, Deserialize};
2+
use serde::ser::Serializer;
3+
use serde::de::{Deserializer, Error};
4+
5+
#[derive(Debug, Serialize, Deserialize)]
6+
pub struct UserInfoVO {
7+
#[serde(serialize_with = "serialize_long")]
8+
pub id: Option<i64>, // 以字符串形式序列化的 Long 类型
9+
10+
pub name: Option<String>, // 用户昵称(别名 姓名)
11+
12+
pub position: Option<String>, // 职位
13+
14+
pub email: Option<String>, // 邮箱
15+
16+
pub phone_number: Option<String>, // 电话号
17+
18+
pub description: Option<String>, // 用户个人简介
19+
20+
pub user_name: Option<String>, // 用户名(登陆的账户)
21+
22+
pub avatar: Option<String>, // 用户头像地址
23+
24+
pub system_language: Option<String>, // 用户语言
25+
26+
pub token: Option<String>, // 用户token
27+
28+
pub expire: Option<i64>, // 过期时间(Unix 时间戳)
29+
}
30+
31+
fn serialize_long<S>(x: &Option<i64>, serializer: S) -> Result<S::Ok, S::Error>
32+
where
33+
S: Serializer,
34+
{
35+
// 以字符串形式序列化 Long 类型
36+
match *x {
37+
Some(value) => serializer.serialize_str(&value.to_string()),
38+
None => serializer.serialize_none(),
39+
}
40+
}
41+
42+
impl UserInfoVO {
43+
// 你可以手动实现一个 builder 模式,或者使用一些第三方库来简化这个过程(比如 `derive_builder`)。
44+
pub fn builder() -> UserInfoVOBuilder {
45+
UserInfoVOBuilder::default()
46+
}
47+
}
48+
49+
#[derive(Default)]
50+
pub struct UserInfoVOBuilder {
51+
id: Option<i64>,
52+
name: Option<String>,
53+
position: Option<String>,
54+
email: Option<String>,
55+
phone_number: Option<String>,
56+
description: Option<String>,
57+
user_name: Option<String>,
58+
avatar: Option<String>,
59+
system_language: Option<String>,
60+
token: Option<String>,
61+
expire: Option<i64>,
62+
}
63+
64+
impl UserInfoVOBuilder {
65+
pub fn id(mut self, id: i64) -> Self {
66+
self.id = Some(id);
67+
self
68+
}
69+
70+
pub fn name(mut self, name: String) -> Self {
71+
self.name = Some(name);
72+
self
73+
}
74+
75+
pub fn position(mut self, position: String) -> Self {
76+
self.position = Some(position);
77+
self
78+
}
79+
80+
pub fn email(mut self, email: String) -> Self {
81+
self.email = Some(email);
82+
self
83+
}
84+
85+
pub fn phone_number(mut self, phone_number: String) -> Self {
86+
self.phone_number = Some(phone_number);
87+
self
88+
}
89+
90+
pub fn description(mut self, description: String) -> Self {
91+
self.description = Some(description);
92+
self
93+
}
94+
95+
pub fn user_name(mut self, user_name: String) -> Self {
96+
self.user_name = Some(user_name);
97+
self
98+
}
99+
100+
pub fn avatar(mut self, avatar: String) -> Self {
101+
self.avatar = Some(avatar);
102+
self
103+
}
104+
105+
pub fn system_language(mut self, system_language: String) -> Self {
106+
self.system_language = Some(system_language);
107+
self
108+
}
109+
110+
pub fn token(mut self, token: String) -> Self {
111+
self.token = Some(token);
112+
self
113+
}
114+
115+
pub fn expire(mut self, expire: i64) -> Self {
116+
self.expire = Some(expire);
117+
self
118+
}
119+
120+
pub fn build(self) -> UserInfoVO {
121+
UserInfoVO {
122+
id: self.id,
123+
name: self.name,
124+
position: self.position,
125+
email: self.email,
126+
phone_number: self.phone_number,
127+
description: self.description,
128+
user_name: self.user_name,
129+
avatar: self.avatar,
130+
system_language: self.system_language,
131+
token: self.token,
132+
expire: self.expire,
133+
}
134+
}
135+
}

service/src/user/mod.rs

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

service/src/user/user_service.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use domain::entity::user;
2+
use domain::vo::user::user_info_vo::UserInfoVO;
3+
use domain::dto::user::account_login_dto::AccountLoginDTO;
4+
use utils::response::Response;
5+
6+
pub struct UserService;
7+
8+
impl UserService {
9+
pub fn new() -> Self {
10+
UserService
11+
}
12+
13+
// pub fn user_login(&self, accountLoginDto: AccountLoginDTO) -> Result<Response<UserInfoVO>, Box<dyn std::error::Error>> {
14+
//
15+
// }
16+
}

utils/src/crypto.rs

Whitespace-only changes.

utils/src/response.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use serde::{Serialize, Deserialize};
2+
3+
#[derive(Serialize, Deserialize, Debug)]
4+
pub struct Response<T> {
5+
pub msg: String,
6+
pub code: String,
7+
pub data: Option<T>,
8+
}
9+
10+
// 枚举类来定义不同的代码和消息
11+
pub enum BaseCodeEnum {
12+
SUCCESS,
13+
ERROR,
14+
}
15+
16+
impl BaseCodeEnum {
17+
pub fn get_code(&self) -> &str {
18+
match self {
19+
BaseCodeEnum::SUCCESS => "200",
20+
BaseCodeEnum::ERROR => "500",
21+
}
22+
}
23+
24+
pub fn get_msg(&self) -> &str {
25+
match self {
26+
BaseCodeEnum::SUCCESS => "Success",
27+
BaseCodeEnum::ERROR => "Error",
28+
}
29+
}
30+
}
31+
32+
impl<T> Response<T> {
33+
pub fn success(data: T) -> Self {
34+
Response {
35+
msg: BaseCodeEnum::SUCCESS.get_msg().to_string(),
36+
code: BaseCodeEnum::SUCCESS.get_code().to_string(),
37+
data: Some(data),
38+
}
39+
}
40+
41+
pub fn fail(msg: String) -> Self {
42+
Response {
43+
msg,
44+
code: BaseCodeEnum::ERROR.get_code().to_string(),
45+
data: None,
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)