Skip to content

Commit 8ddbeb7

Browse files
committed
feat: 重构应用状态管理,整合数据库连接池和微信客户端
1 parent 34de24d commit 8ddbeb7

File tree

9 files changed

+233
-167
lines changed

9 files changed

+233
-167
lines changed

backend/src/api/labelapi.rs

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
1-
use axum::{
2-
Json,
3-
extract::{Path, State},
4-
};
5-
use sqlx::{Pool, Postgres};
6-
use std::sync::Arc;
7-
use uuid::Uuid;
8-
9-
use crate::{
10-
error::{AppError, AppErrorType},
11-
model::models::{
12-
label,
13-
post::{self, PostSummary},
14-
},
15-
};
16-
17-
/// 创建标签
18-
///
19-
/// 接收标签信息并创建新标签
20-
pub async fn create_label(
21-
State(pool): State<Arc<Pool<Postgres>>>,
22-
Json(req): Json<label::CreateLabelRequest>,
23-
) -> Result<Json<label::Label>, AppError> {
24-
// 创建新标签
25-
let label = label::Label::create(pool.as_ref(), req)
26-
.await
27-
.map_err(|e| {
28-
AppError::new_message(&format!("创建标签失败: {}", e), AppErrorType::Internal)
29-
})?;
30-
31-
// 返回创建的标签
32-
Ok(Json(label))
33-
}
34-
35-
/// 获取所有标签
36-
///
37-
/// 返回所有标签列表
38-
pub async fn get_labels(
39-
State(pool): State<Arc<Pool<Postgres>>>,
40-
) -> Result<Json<Vec<label::Label>>, AppError> {
41-
// 获取所有标签
42-
let labels = label::Label::find_all(pool.as_ref()).await.map_err(|e| {
43-
AppError::new_message(&format!("获取标签列表失败: {}", e), AppErrorType::Internal)
44-
})?;
45-
46-
// 返回标签列表
47-
Ok(Json(labels))
48-
}
49-
50-
/// 获取标签下的文章
51-
///
52-
/// 根据标签ID返回该标签下的所有文章
53-
pub async fn get_posts_by_label(
54-
State(pool): State<Arc<Pool<Postgres>>>,
55-
Path(label_id): Path<Uuid>,
56-
) -> Result<Json<Vec<PostSummary>>, AppError> {
57-
// 检查标签是否存在
58-
let label = label::Label::find_by_id(pool.as_ref(), label_id)
59-
.await
60-
.map_err(|e| {
61-
AppError::new_message(&format!("查询标签失败: {}", e), AppErrorType::Internal)
62-
})?;
63-
64-
if label.is_none() {
65-
return Err(AppError::new_message(
66-
&format!("未找到ID为{}的标签", label_id),
67-
AppErrorType::Notfound,
68-
));
69-
}
70-
71-
// 获取标签下的文章
72-
let posts = post::Post::find_by_label_id(pool.as_ref(), label_id, true)
73-
.await
74-
.map_err(|e| {
75-
AppError::new_message(
76-
&format!("获取标签下文章失败: {}", e),
77-
AppErrorType::Internal,
78-
)
79-
})?;
80-
81-
// 返回文章列表
82-
Ok(Json(posts))
83-
}
1+
use axum::{
2+
Json,
3+
extract::{Path, State},
4+
};
5+
use uuid::Uuid;
6+
7+
use crate::{
8+
error::{AppError, AppErrorType},
9+
model::models::{
10+
label,
11+
post::{self, PostSummary},
12+
},
13+
};
14+
15+
/// 创建标签
16+
///
17+
/// 接收标签信息并创建新标签
18+
pub async fn create_label(
19+
State(state): State<crate::state::AppState>,
20+
Json(req): Json<label::CreateLabelRequest>,
21+
) -> Result<Json<label::Label>, AppError> {
22+
// 创建新标签
23+
let label = label::Label::create(state.pool.as_ref(), req)
24+
.await
25+
.map_err(|e| {
26+
AppError::new_message(&format!("创建标签失败: {}", e), AppErrorType::Internal)
27+
})?;
28+
29+
// 返回创建的标签
30+
Ok(Json(label))
31+
}
32+
33+
/// 获取所有标签
34+
///
35+
/// 返回所有标签列表
36+
pub async fn get_labels(
37+
State(state): State<crate::state::AppState>,
38+
) -> Result<Json<Vec<label::Label>>, AppError> {
39+
// 获取所有标签
40+
let labels = label::Label::find_all(state.pool.as_ref())
41+
.await
42+
.map_err(|e| {
43+
AppError::new_message(&format!("获取标签列表失败: {}", e), AppErrorType::Internal)
44+
})?;
45+
46+
// 返回标签列表
47+
Ok(Json(labels))
48+
}
49+
50+
/// 获取标签下的文章
51+
///
52+
/// 根据标签ID返回该标签下的所有文章
53+
pub async fn get_posts_by_label(
54+
State(state): State<crate::state::AppState>,
55+
Path(label_id): Path<Uuid>,
56+
) -> Result<Json<Vec<PostSummary>>, AppError> {
57+
// 检查标签是否存在
58+
let label = label::Label::find_by_id(state.pool.as_ref(), label_id)
59+
.await
60+
.map_err(|e| {
61+
AppError::new_message(&format!("查询标签失败: {}", e), AppErrorType::Internal)
62+
})?;
63+
64+
if label.is_none() {
65+
return Err(AppError::new_message(
66+
&format!("未找到ID为{}的标签", label_id),
67+
AppErrorType::Notfound,
68+
));
69+
}
70+
71+
// 获取标签下的文章
72+
let posts = post::Post::find_by_label_id(state.pool.as_ref(), label_id, true)
73+
.await
74+
.map_err(|e| {
75+
AppError::new_message(
76+
&format!("获取标签下文章失败: {}", e),
77+
AppErrorType::Internal,
78+
)
79+
})?;
80+
81+
// 返回文章列表
82+
Ok(Json(posts))
83+
}

backend/src/api/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ use axum::{
1313
routing::{delete, get, post, put},
1414
};
1515

16-
use sqlx::{Pool, Postgres};
17-
use std::sync::Arc;
18-
1916
use crate::middleware::auth;
17+
use crate::state::AppState;
2018

2119
/// 创建API路由
22-
pub fn create_routes() -> Router<Arc<Pool<Postgres>>> {
20+
pub fn create_routes() -> Router<AppState> {
2321
// 公共路由 - 不需要认证
2422
let public_routes = Router::new()
2523
.route("/users/register", post(userapi::register_user))

backend/src/api/postapi.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use axum::{
22
Json,
33
extract::{Path, State},
44
};
5-
use sqlx::{Pool, Postgres};
6-
use std::sync::Arc;
75
use uuid::Uuid;
86

97
use crate::{
@@ -15,10 +13,10 @@ use crate::{
1513
///
1614
/// 返回所有已发布的文章列表,包含标签信息
1715
pub async fn get_posts(
18-
State(pool): State<Arc<Pool<Postgres>>>,
16+
State(state): State<crate::state::AppState>,
1917
) -> Result<Json<Vec<post::PostSummaryWithLabels>>, AppError> {
2018
// 获取所有已发布的文章(包含标签)
21-
let posts = post::Post::find_all_with_labels(pool.as_ref(), true).await?;
19+
let posts = post::Post::find_all_with_labels(state.pool.as_ref(), true).await?;
2220

2321
// 返回文章列表
2422
Ok(Json(posts))
@@ -28,11 +26,11 @@ pub async fn get_posts(
2826
///
2927
/// 接收文章信息并创建新文章
3028
pub async fn create_post(
31-
State(pool): State<Arc<Pool<Postgres>>>,
29+
State(state): State<crate::state::AppState>,
3230
Json(req): Json<post::CreatePostRequest>,
3331
) -> Result<Json<post::Post>, AppError> {
3432
// 创建新文章
35-
let post = post::Post::create(pool.as_ref(), req).await?;
33+
let post = post::Post::create(state.pool.as_ref(), req).await?;
3634

3735
// 返回创建的文章
3836
Ok(Json(post))
@@ -42,11 +40,11 @@ pub async fn create_post(
4240
///
4341
/// 返回指定ID的文章详情,包含完整内容
4442
pub async fn get_post_by_id(
45-
State(pool): State<Arc<Pool<Postgres>>>,
43+
State(state): State<crate::state::AppState>,
4644
Path(id): Path<Uuid>,
4745
) -> Result<Json<post::PostWithLabels>, AppError> {
4846
// 根据ID查找文章
49-
match post::Post::find_by_id_with_labels(pool.as_ref(), id).await? {
47+
match post::Post::find_by_id_with_labels(state.pool.as_ref(), id).await? {
5048
Some(post) => Ok(Json(post)),
5149
None => Err(AppError::new_message(
5250
&format!("未找到ID为{}的文章", id),
@@ -59,11 +57,11 @@ pub async fn get_post_by_id(
5957
///
6058
/// 返回指定文章ID的所有标签
6159
pub async fn get_post_labels(
62-
State(pool): State<Arc<Pool<Postgres>>>,
60+
State(state): State<crate::state::AppState>,
6361
Path(post_id): Path<Uuid>,
6462
) -> Result<Json<Vec<label::Label>>, AppError> {
6563
// 首先检查文章是否存在
66-
let post = post::Post::find_by_id(pool.as_ref(), post_id).await?;
64+
let post = post::Post::find_by_id(state.pool.as_ref(), post_id).await?;
6765
if post.is_none() {
6866
return Err(AppError::new_message(
6967
&format!("未找到ID为{}的文章", post_id),
@@ -72,7 +70,7 @@ pub async fn get_post_labels(
7270
}
7371

7472
// 获取文章的所有标签
75-
let labels = label::Label::find_by_post_id(pool.as_ref(), post_id)
73+
let labels = label::Label::find_by_post_id(state.pool.as_ref(), post_id)
7674
.await
7775
.map_err(|e| {
7876
AppError::new_message(&format!("获取文章标签失败: {}", e), AppErrorType::Internal)

backend/src/api/userapi.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
55
use axum::{Json, extract::State};
66
use bcrypt::{DEFAULT_COST, hash};
7-
use sqlx::{Pool, Postgres};
8-
use std::sync::Arc;
97

108
use crate::error::{AppError, AppErrorType};
119
use crate::middleware::auth;
@@ -15,19 +13,19 @@ use crate::model::models::user::{CreateUserRequest, LoginRequest, User};
1513
///
1614
/// 接收用户注册信息,验证数据有效性,然后创建新用户
1715
pub async fn register_user(
18-
State(pool): State<Arc<Pool<Postgres>>>,
16+
State(state): State<crate::state::AppState>,
1917
Json(req): Json<CreateUserRequest>,
2018
) -> Result<Json<User>, AppError> {
2119
// 验证用户名是否已存在
22-
if let Ok(Some(_)) = User::find_by_username(&pool, &req.username).await {
20+
if let Ok(Some(_)) = User::find_by_username(&state.pool, &req.username).await {
2321
return Err(AppError::new_message(
2422
"用户名已被使用",
2523
AppErrorType::Duplicate,
2624
));
2725
}
2826

2927
// 验证邮箱是否已存在
30-
if let Ok(Some(_)) = User::find_by_email(&pool, &req.email).await {
28+
if let Ok(Some(_)) = User::find_by_email(&state.pool, &req.email).await {
3129
return Err(AppError::new_message(
3230
"邮箱已被注册",
3331
AppErrorType::Duplicate,
@@ -52,7 +50,7 @@ pub async fn register_user(
5250
};
5351

5452
// 创建新用户
55-
match User::create(&pool, req_with_hashed_password).await {
53+
match User::create(&state.pool, req_with_hashed_password).await {
5654
Ok(user) => Ok(Json(user)),
5755
Err(e) => Err(AppError::new(e, AppErrorType::Db)),
5856
}
@@ -62,11 +60,11 @@ pub async fn register_user(
6260
///
6361
/// 验证用户凭据并生成JWT令牌
6462
pub async fn login_user(
65-
State(pool): State<Arc<Pool<Postgres>>>,
63+
State(state): State<crate::state::AppState>,
6664
Json(req): Json<LoginRequest>,
6765
) -> Result<Json<serde_json::Value>, AppError> {
6866
// 尝试登录用户
69-
match User::login(&pool, req).await {
67+
match User::login(&state.pool, req).await {
7068
Ok(Some(user)) => {
7169
// 生成JWT令牌
7270
let token = auth::generate_token(&user)?;

0 commit comments

Comments
 (0)