feat: [阶段1] 项目初始化和基础设置
- 创建 Cargo.toml 配置文件,包含所有必要依赖 - 建立完整的项目模块结构(config, models, handlers, routes, services, storage, middleware, utils) - 实现用户数据模型和内存存储 - 创建基础的 HTTP 处理器和路由配置 - 添加错误处理和 JWT 认证中间件 - 配置环境变量和日志系统 - 创建项目文档和学习指南 - 服务器可以成功编译和启动
This commit is contained in:
5
src/services/mod.rs
Normal file
5
src/services/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
//! 业务逻辑服务模块
|
||||
|
||||
pub mod user_service;
|
||||
|
||||
pub use user_service::UserService;
|
30
src/services/user_service.rs
Normal file
30
src/services/user_service.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
//! 用户业务逻辑服务
|
||||
|
||||
use crate::models::user::User;
|
||||
use crate::storage::memory::MemoryUserStore;
|
||||
use crate::utils::errors::ApiError;
|
||||
|
||||
/// 用户服务
|
||||
pub struct UserService {
|
||||
store: MemoryUserStore,
|
||||
}
|
||||
|
||||
impl UserService {
|
||||
pub fn new(store: MemoryUserStore) -> Self {
|
||||
Self { store }
|
||||
}
|
||||
|
||||
/// 验证用户凭据
|
||||
pub async fn authenticate_user(&self, username: &str, password: &str) -> Result<User, ApiError> {
|
||||
match self.store.get_user_by_username(username).await {
|
||||
Some(user) => {
|
||||
if user.password_hash == format!("hashed_{}", password) {
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(ApiError::Unauthorized)
|
||||
}
|
||||
}
|
||||
None => Err(ApiError::NotFound("用户不存在".to_string())),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user