feat(diary): 数据层 + 班级系统 (Phase F1 + B3)
Flutter 数据层 (Phase F1): - journal_entry.dart: 日记数据模型 (Mood/Weather/tags/version) - journal_element.dart: 元素模型 (text/image/sticker/handwriting_ref/tape) - school_class.dart: 班级模型 - user_settings.dart: 用户设置 (主题/画笔/字号) - isar_database.dart: Isar 初始化 - api_client.dart: Dio + JWT注入 + 离线感知 + 401处理 - journal_repository.dart: 抽象接口 + InMemory实现 (乐观锁) - sync_engine.dart: WiFi同步 + 操作队列 + 重试(5次) + 快照持久化 Rust 班级系统 (Phase B3): - class_service.rs: 创建班级(6位码) + 加入班级 + 成员管理 - topic_service.rs: 老师布置主题 + 主题列表 - comment_service.rs: 老师点评 + 评语列表 - class_handler.rs: 5个API端点 + 权限守卫 - topic_handler.rs: 2个API端点 - comment_handler.rs: 2个API端点 - dto.rs: 新增5个DTO (ClassMemberResp/CreateTopicReq/TopicResp/CreateCommentReq/CommentResp) - 6条新路由注册 验证: cargo check 通过, 433测试全绿, flutter analyze 1 warning
This commit is contained in:
@@ -123,3 +123,58 @@ pub struct ConflictInfo {
|
||||
pub local_version: i32,
|
||||
pub server_version: i32,
|
||||
}
|
||||
|
||||
// ========== 班级成员 ==========
|
||||
|
||||
/// 班级成员响应
|
||||
#[derive(Debug, Serialize, ToSchema)]
|
||||
pub struct ClassMemberResp {
|
||||
pub user_id: uuid::Uuid,
|
||||
pub role: String,
|
||||
pub nickname: Option<String>,
|
||||
pub joined_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
// ========== 主题布置 ==========
|
||||
|
||||
/// 创建主题请求
|
||||
#[derive(Debug, Deserialize, ToSchema)]
|
||||
pub struct CreateTopicReq {
|
||||
/// 主题标题
|
||||
pub title: String,
|
||||
/// 主题描述/要求
|
||||
pub description: Option<String>,
|
||||
/// 截止日期
|
||||
pub due_date: Option<chrono::NaiveDate>,
|
||||
}
|
||||
|
||||
/// 主题响应
|
||||
#[derive(Debug, Serialize, ToSchema)]
|
||||
pub struct TopicResp {
|
||||
pub id: uuid::Uuid,
|
||||
pub class_id: uuid::Uuid,
|
||||
pub teacher_id: uuid::Uuid,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub due_date: Option<chrono::NaiveDate>,
|
||||
pub is_active: bool,
|
||||
}
|
||||
|
||||
// ========== 评语 ==========
|
||||
|
||||
/// 创建评语请求
|
||||
#[derive(Debug, Deserialize, ToSchema)]
|
||||
pub struct CreateCommentReq {
|
||||
/// 评语内容
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
/// 评语响应
|
||||
#[derive(Debug, Serialize, ToSchema)]
|
||||
pub struct CommentResp {
|
||||
pub id: uuid::Uuid,
|
||||
pub journal_id: uuid::Uuid,
|
||||
pub author_id: uuid::Uuid,
|
||||
pub content: String,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
191
crates/erp-diary/src/handler/class_handler.rs
Normal file
191
crates/erp-diary/src/handler/class_handler.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
// 班级 API 处理器 — 创建班级、加入班级、查询班级
|
||||
|
||||
use axum::extract::{Extension, FromRef, Path, State};
|
||||
use axum::response::Json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, TenantContext};
|
||||
|
||||
use crate::dto::{ClassMemberResp, ClassResp, CreateClassReq, JoinClassReq};
|
||||
use crate::service::class_service::ClassService;
|
||||
use crate::state::DiaryState;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/classes",
|
||||
request_body = CreateClassReq,
|
||||
responses(
|
||||
(status = 200, description = "创建成功", body = ApiResponse<ClassResp>),
|
||||
(status = 400, description = "验证失败"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "班级管理"
|
||||
)]
|
||||
/// POST /api/v1/diary/classes
|
||||
///
|
||||
/// 创建班级。需要 `diary.class.manage` 权限(老师角色)。
|
||||
pub async fn create_class<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<CreateClassReq>,
|
||||
) -> Result<Json<ApiResponse<ClassResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.class.manage")?;
|
||||
|
||||
if req.name.trim().is_empty() {
|
||||
return Err(AppError::Validation("班级名称不能为空".to_string()));
|
||||
}
|
||||
|
||||
let resp = ClassService::create_class(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
req.name,
|
||||
req.school_name,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/classes/join",
|
||||
request_body = JoinClassReq,
|
||||
responses(
|
||||
(status = 200, description = "加入成功", body = ApiResponse<ClassResp>),
|
||||
(status = 400, description = "班级码无效或已过期"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "班级管理"
|
||||
)]
|
||||
/// POST /api/v1/diary/classes/join
|
||||
///
|
||||
/// 通过班级码加入班级。需要 `diary.journal.create` 权限(学生使用此权限加入)。
|
||||
pub async fn join_class<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<JoinClassReq>,
|
||||
) -> Result<Json<ApiResponse<ClassResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.create")?;
|
||||
|
||||
if req.class_code.trim().is_empty() {
|
||||
return Err(AppError::Validation("班级码不能为空".to_string()));
|
||||
}
|
||||
|
||||
let resp = ClassService::join_class(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
req.class_code,
|
||||
None, // 昵称暂不通过此接口传递
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/classes/{id}",
|
||||
params(("id" = Uuid, Path, description = "班级ID")),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<ClassResp>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "班级不存在"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "班级管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/classes/:id
|
||||
///
|
||||
/// 获取班级详情。需要 `diary.journal.read` 权限。
|
||||
pub async fn get_class<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<ClassResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = ClassService::get_class(ctx.tenant_id, id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/classes/{id}/members",
|
||||
params(("id" = Uuid, Path, description = "班级ID")),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<Vec<ClassMemberResp>>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "班级不存在"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "班级管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/classes/:id/members
|
||||
///
|
||||
/// 获取班级成员列表。需要 `diary.journal.read` 权限。
|
||||
pub async fn list_members<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<Vec<ClassMemberResp>>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = ClassService::list_members(ctx.tenant_id, id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/classes/my",
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<Vec<ClassResp>>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "班级管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/classes/my
|
||||
///
|
||||
/// 获取当前用户加入的所有班级。需要 `diary.journal.read` 权限。
|
||||
pub async fn my_classes<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
) -> Result<Json<ApiResponse<Vec<ClassResp>>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = ClassService::my_classes(ctx.tenant_id, ctx.user_id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
90
crates/erp-diary/src/handler/comment_handler.rs
Normal file
90
crates/erp-diary/src/handler/comment_handler.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
// 评语 API 处理器 — 老师点评学生日记
|
||||
|
||||
use axum::extract::{Extension, FromRef, Path, State};
|
||||
use axum::response::Json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, TenantContext};
|
||||
|
||||
use crate::dto::{CommentResp, CreateCommentReq};
|
||||
use crate::service::comment_service::CommentService;
|
||||
use crate::state::DiaryState;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/journals/{journal_id}/comments",
|
||||
params(("journal_id" = Uuid, Path, description = "日记ID")),
|
||||
request_body = CreateCommentReq,
|
||||
responses(
|
||||
(status = 200, description = "点评成功", body = ApiResponse<CommentResp>),
|
||||
(status = 400, description = "验证失败或内容安全检查未通过"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "日记不存在"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "评语管理"
|
||||
)]
|
||||
/// POST /api/v1/diary/journals/:journal_id/comments
|
||||
///
|
||||
/// 老师点评日记。需要 `diary.comment.write` 权限。
|
||||
pub async fn create_comment<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(journal_id): Path<Uuid>,
|
||||
Json(req): Json<CreateCommentReq>,
|
||||
) -> Result<Json<ApiResponse<CommentResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.comment.write")?;
|
||||
|
||||
if req.content.trim().is_empty() {
|
||||
return Err(AppError::Validation("评语内容不能为空".to_string()));
|
||||
}
|
||||
|
||||
let resp = CommentService::create_comment(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
journal_id,
|
||||
req.content,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/journals/{journal_id}/comments",
|
||||
params(("journal_id" = Uuid, Path, description = "日记ID")),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<Vec<CommentResp>>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "评语管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/journals/:journal_id/comments
|
||||
///
|
||||
/// 获取日记评语列表。需要 `diary.journal.read` 权限。
|
||||
pub async fn list_comments<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(journal_id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<Vec<CommentResp>>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = CommentService::list_comments(ctx.tenant_id, journal_id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
@@ -2,3 +2,6 @@
|
||||
|
||||
pub mod journal_handler;
|
||||
pub mod sync_handler;
|
||||
pub mod class_handler;
|
||||
pub mod topic_handler;
|
||||
pub mod comment_handler;
|
||||
|
||||
90
crates/erp-diary/src/handler/topic_handler.rs
Normal file
90
crates/erp-diary/src/handler/topic_handler.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
// 主题布置 API 处理器 — 老师布置/查询主题
|
||||
|
||||
use axum::extract::{Extension, FromRef, Path, State};
|
||||
use axum::response::Json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, TenantContext};
|
||||
|
||||
use crate::dto::{CreateTopicReq, TopicResp};
|
||||
use crate::service::topic_service::TopicService;
|
||||
use crate::state::DiaryState;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/classes/{class_id}/topics",
|
||||
params(("class_id" = Uuid, Path, description = "班级ID")),
|
||||
request_body = CreateTopicReq,
|
||||
responses(
|
||||
(status = 200, description = "布置成功", body = ApiResponse<TopicResp>),
|
||||
(status = 400, description = "验证失败"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "班级不存在"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "主题布置"
|
||||
)]
|
||||
/// POST /api/v1/diary/classes/:class_id/topics
|
||||
///
|
||||
/// 布置日记主题。需要 `diary.topic.assign` 权限(老师角色)。
|
||||
pub async fn assign_topic<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(class_id): Path<Uuid>,
|
||||
Json(req): Json<CreateTopicReq>,
|
||||
) -> Result<Json<ApiResponse<TopicResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.topic.assign")?;
|
||||
|
||||
if req.title.trim().is_empty() {
|
||||
return Err(AppError::Validation("主题标题不能为空".to_string()));
|
||||
}
|
||||
|
||||
let resp = TopicService::assign_topic(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
class_id,
|
||||
&req,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/classes/{class_id}/topics",
|
||||
params(("class_id" = Uuid, Path, description = "班级ID")),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<Vec<TopicResp>>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "主题布置"
|
||||
)]
|
||||
/// GET /api/v1/diary/classes/:class_id/topics
|
||||
///
|
||||
/// 获取班级主题列表。需要 `diary.journal.read` 权限。
|
||||
pub async fn list_topics<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(class_id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<Vec<TopicResp>>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = TopicService::list_topics(ctx.tenant_id, class_id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
@@ -10,7 +10,7 @@ pub use state::DiaryState;
|
||||
|
||||
use erp_core::module::ErpModule;
|
||||
|
||||
use crate::handler::{journal_handler, sync_handler};
|
||||
use crate::handler::{journal_handler, sync_handler, class_handler, topic_handler, comment_handler};
|
||||
|
||||
/// 暖记日记业务模块
|
||||
pub struct DiaryModule;
|
||||
@@ -127,5 +127,35 @@ impl DiaryModule {
|
||||
"/diary/sync",
|
||||
axum::routing::post(sync_handler::sync_journals),
|
||||
)
|
||||
// 班级管理
|
||||
.route(
|
||||
"/diary/classes",
|
||||
axum::routing::post(class_handler::create_class)
|
||||
.get(class_handler::my_classes),
|
||||
)
|
||||
.route(
|
||||
"/diary/classes/join",
|
||||
axum::routing::post(class_handler::join_class),
|
||||
)
|
||||
.route(
|
||||
"/diary/classes/{id}",
|
||||
axum::routing::get(class_handler::get_class),
|
||||
)
|
||||
.route(
|
||||
"/diary/classes/{id}/members",
|
||||
axum::routing::get(class_handler::list_members),
|
||||
)
|
||||
// 主题布置
|
||||
.route(
|
||||
"/diary/classes/{class_id}/topics",
|
||||
axum::routing::post(topic_handler::assign_topic)
|
||||
.get(topic_handler::list_topics),
|
||||
)
|
||||
// 评语管理
|
||||
.route(
|
||||
"/diary/journals/{journal_id}/comments",
|
||||
axum::routing::post(comment_handler::create_comment)
|
||||
.get(comment_handler::list_comments),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
300
crates/erp-diary/src/service/class_service.rs
Normal file
300
crates/erp-diary/src/service/class_service.rs
Normal file
@@ -0,0 +1,300 @@
|
||||
// 班级服务 — 创建班级、加入班级、班级查询
|
||||
|
||||
use chrono::{Months, Utc};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, PaginatorTrait, QueryFilter,
|
||||
Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{ClassMemberResp, ClassResp};
|
||||
use crate::entity::{class_member, school_class};
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 班级服务 — 6 位码生成、过期控制、成员管理
|
||||
pub struct ClassService;
|
||||
|
||||
impl ClassService {
|
||||
/// 创建班级(老师)
|
||||
///
|
||||
/// 生成 6 位随机班级码,设置过期时间(6 个月后),
|
||||
/// 自动将老师加入 class_members。
|
||||
pub async fn create_class(
|
||||
tenant_id: Uuid,
|
||||
teacher_id: Uuid,
|
||||
name: String,
|
||||
school_name: Option<String>,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
// 生成唯一班级码(最多重试 10 次)
|
||||
let class_code = Self::generate_unique_code(db).await?;
|
||||
|
||||
// 过期时间:6 个月后
|
||||
let expires_at = now.checked_add_months(Months::new(6));
|
||||
|
||||
// 创建班级记录
|
||||
let class_model = school_class::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
name: Set(name),
|
||||
school_name: Set(school_name),
|
||||
teacher_id: Set(teacher_id),
|
||||
class_code: Set(class_code.clone()),
|
||||
member_count: Set(1), // 老师自动计入
|
||||
is_active: Set(true),
|
||||
expires_at: Set(expires_at),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted_class = class_model.insert(db).await?;
|
||||
|
||||
// 自动将老师加入成员表
|
||||
let member_model = class_member::ActiveModel {
|
||||
class_id: Set(id),
|
||||
user_id: Set(teacher_id),
|
||||
tenant_id: Set(tenant_id),
|
||||
role: Set("teacher".to_string()),
|
||||
nickname: Set(None),
|
||||
joined_at: Set(now),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
member_model.insert(db).await?;
|
||||
|
||||
// 发布 ClassCreated 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.class.created",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"class_id": id,
|
||||
"teacher_id": teacher_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(class_model_to_resp(inserted_class))
|
||||
}
|
||||
|
||||
/// 加入班级(学生通过班级码)
|
||||
///
|
||||
/// 验证班级码有效性和过期状态,检查是否已是成员,
|
||||
/// 创建 class_member 记录并更新 member_count。
|
||||
pub async fn join_class(
|
||||
tenant_id: Uuid,
|
||||
user_id: Uuid,
|
||||
class_code: String,
|
||||
nickname: Option<String>,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let now = Utc::now();
|
||||
|
||||
// 1. 查找班级码对应的班级
|
||||
let class_model = school_class::Entity::find()
|
||||
.filter(school_class::Column::ClassCode.eq(&class_code))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DiaryError::InvalidClassCode)?;
|
||||
|
||||
// 2. 检查班级是否激活
|
||||
if !class_model.is_active {
|
||||
return Err(DiaryError::BadRequest("班级已停用".to_string()));
|
||||
}
|
||||
|
||||
// 3. 检查是否过期
|
||||
if let Some(expires) = class_model.expires_at {
|
||||
if now > expires {
|
||||
return Err(DiaryError::ClassCodeExpired);
|
||||
}
|
||||
}
|
||||
|
||||
let class_id = class_model.id;
|
||||
|
||||
// 4. 检查是否已是成员
|
||||
let existing = class_member::Entity::find()
|
||||
.filter(class_member::Column::ClassId.eq(class_id))
|
||||
.filter(class_member::Column::UserId.eq(user_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?;
|
||||
|
||||
if existing.is_some() {
|
||||
return Err(DiaryError::BadRequest("已是班级成员".to_string()));
|
||||
}
|
||||
|
||||
// 5. 创建成员记录
|
||||
let member_model = class_member::ActiveModel {
|
||||
class_id: Set(class_id),
|
||||
user_id: Set(user_id),
|
||||
tenant_id: Set(tenant_id),
|
||||
role: Set("student".to_string()),
|
||||
nickname: Set(nickname),
|
||||
joined_at: Set(now),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(user_id),
|
||||
updated_by: Set(user_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
member_model.insert(db).await?;
|
||||
|
||||
// 6. 更新 member_count
|
||||
let mut active_class: school_class::ActiveModel = class_model.into();
|
||||
let new_count = active_class.member_count.unwrap() + 1;
|
||||
active_class.member_count = Set(new_count);
|
||||
active_class.updated_at = Set(now);
|
||||
active_class.version = Set(active_class.version.unwrap() + 1);
|
||||
let updated_class = active_class.update(db).await?;
|
||||
|
||||
// 7. 发布 StudentJoinedClass 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.class.student_joined",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"class_id": class_id,
|
||||
"student_id": user_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(class_model_to_resp(updated_class))
|
||||
}
|
||||
|
||||
/// 获取班级详情
|
||||
pub async fn get_class(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<ClassResp> {
|
||||
let model = school_class::Entity::find()
|
||||
.filter(school_class::Column::Id.eq(class_id))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("班级 {} 不存在", class_id)))?;
|
||||
|
||||
Ok(class_model_to_resp(model))
|
||||
}
|
||||
|
||||
/// 获取班级成员列表
|
||||
pub async fn list_members(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<ClassMemberResp>> {
|
||||
let members = class_member::Entity::find()
|
||||
.filter(class_member::Column::ClassId.eq(class_id))
|
||||
.filter(class_member::Column::TenantId.eq(tenant_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(members.into_iter().map(member_model_to_resp).collect())
|
||||
}
|
||||
|
||||
/// 获取我加入的班级列表
|
||||
pub async fn my_classes(
|
||||
tenant_id: Uuid,
|
||||
user_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<ClassResp>> {
|
||||
// 先查用户所在的班级 ID
|
||||
let memberships = class_member::Entity::find()
|
||||
.filter(class_member::Column::UserId.eq(user_id))
|
||||
.filter(class_member::Column::TenantId.eq(tenant_id))
|
||||
.filter(class_member::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
let class_ids: Vec<Uuid> = memberships.iter().map(|m| m.class_id).collect();
|
||||
|
||||
if class_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let classes = school_class::Entity::find()
|
||||
.filter(school_class::Column::Id.is_in(class_ids))
|
||||
.filter(school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(school_class::Column::DeletedAt.is_null())
|
||||
.filter(school_class::Column::IsActive.eq(true))
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(classes.into_iter().map(class_model_to_resp).collect())
|
||||
}
|
||||
|
||||
/// 生成唯一班级码(重试最多 10 次)
|
||||
async fn generate_unique_code(db: &DatabaseConnection) -> DiaryResult<String> {
|
||||
for _ in 0..10 {
|
||||
let code = generate_class_code();
|
||||
let exists = school_class::Entity::find()
|
||||
.filter(school_class::Column::ClassCode.eq(&code))
|
||||
.one(db)
|
||||
.await?
|
||||
.is_some();
|
||||
|
||||
if !exists {
|
||||
return Ok(code);
|
||||
}
|
||||
}
|
||||
Err(DiaryError::Internal("无法生成唯一班级码".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// 生成 6 位班级码(UUID 前 6 位字符)
|
||||
fn generate_class_code() -> String {
|
||||
Uuid::new_v4()
|
||||
.to_string()
|
||||
.replace("-", "")
|
||||
.chars()
|
||||
.take(6)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// school_class::Model -> ClassResp
|
||||
fn class_model_to_resp(model: school_class::Model) -> ClassResp {
|
||||
ClassResp {
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
school_name: model.school_name,
|
||||
teacher_id: model.teacher_id,
|
||||
class_code: model.class_code,
|
||||
member_count: model.member_count,
|
||||
is_active: model.is_active,
|
||||
}
|
||||
}
|
||||
|
||||
/// class_member::Model -> ClassMemberResp
|
||||
fn member_model_to_resp(model: class_member::Model) -> ClassMemberResp {
|
||||
ClassMemberResp {
|
||||
user_id: model.user_id,
|
||||
role: model.role,
|
||||
nickname: model.nickname,
|
||||
joined_at: model.joined_at,
|
||||
}
|
||||
}
|
||||
134
crates/erp-diary/src/service/comment_service.rs
Normal file
134
crates/erp-diary/src/service/comment_service.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
// 评语服务 — 老师点评学生日记
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QueryOrder, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::CommentResp;
|
||||
use crate::entity::{comment, journal_entry};
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 评语服务 — 老师对学生日记的点评
|
||||
pub struct CommentService;
|
||||
|
||||
impl CommentService {
|
||||
/// 添加评语(老师点评学生日记)
|
||||
///
|
||||
/// 验证日记存在,执行基础内容安全检查,
|
||||
/// 创建评论记录,发布 CommentCreated 事件。
|
||||
pub async fn create_comment(
|
||||
tenant_id: Uuid,
|
||||
author_id: Uuid,
|
||||
journal_id: Uuid,
|
||||
content: String,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<CommentResp> {
|
||||
// 1. 验证日记存在
|
||||
let journal = journal_entry::Entity::find()
|
||||
.filter(journal_entry::Column::Id.eq(journal_id))
|
||||
.filter(journal_entry::Column::TenantId.eq(tenant_id))
|
||||
.filter(journal_entry::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("日记 {} 不存在", journal_id)))?;
|
||||
|
||||
// 2. 简单内容安全检查(基础敏感词过滤)
|
||||
if contains_sensitive_words(&content) {
|
||||
return Err(DiaryError::ContentSafetyViolation);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
// 3. 创建评论记录
|
||||
let model = comment::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
journal_id: Set(journal_id),
|
||||
author_id: Set(author_id),
|
||||
content: Set(content),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(author_id),
|
||||
updated_by: Set(author_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted = model.insert(db).await?;
|
||||
|
||||
// 4. 发布 CommentCreated 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.comment.created",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"comment_id": id,
|
||||
"journal_id": journal_id,
|
||||
"teacher_id": author_id,
|
||||
"student_id": journal.author_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(comment_model_to_resp(inserted))
|
||||
}
|
||||
|
||||
/// 获取日记的评语列表
|
||||
///
|
||||
/// 按创建时间正序返回日记下所有未删除的评语。
|
||||
pub async fn list_comments(
|
||||
tenant_id: Uuid,
|
||||
journal_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<CommentResp>> {
|
||||
let comments = comment::Entity::find()
|
||||
.filter(comment::Column::JournalId.eq(journal_id))
|
||||
.filter(comment::Column::TenantId.eq(tenant_id))
|
||||
.filter(comment::Column::DeletedAt.is_null())
|
||||
.order_by_asc(comment::Column::CreatedAt)
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(comments.into_iter().map(comment_model_to_resp).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// comment::Model -> CommentResp
|
||||
fn comment_model_to_resp(model: comment::Model) -> CommentResp {
|
||||
CommentResp {
|
||||
id: model.id,
|
||||
journal_id: model.journal_id,
|
||||
author_id: model.author_id,
|
||||
content: model.content,
|
||||
created_at: model.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
/// 基础敏感词检查
|
||||
///
|
||||
/// Phase 1 使用简单字符串匹配,后续迭代替换为完整词库。
|
||||
fn contains_sensitive_words(content: &str) -> bool {
|
||||
const SENSITIVE_WORDS: &[&str] = &[
|
||||
// 占位 — Phase 1 仅检查是否为空或过短
|
||||
// 完整词库将在后续迭代中添加
|
||||
];
|
||||
|
||||
if content.trim().is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
for word in SENSITIVE_WORDS {
|
||||
if content.contains(word) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
@@ -2,3 +2,6 @@
|
||||
|
||||
pub mod journal_service;
|
||||
pub mod sync_service;
|
||||
pub mod class_service;
|
||||
pub mod topic_service;
|
||||
pub mod comment_service;
|
||||
|
||||
116
crates/erp-diary/src/service/topic_service.rs
Normal file
116
crates/erp-diary/src/service/topic_service.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
// 主题布置服务 — 老师发布日记主题
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter,
|
||||
QueryOrder, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{CreateTopicReq, TopicResp};
|
||||
use crate::entity::topic_assignment;
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 主题布置服务 — 老师发布日记主题,学生提交对应日记
|
||||
pub struct TopicService;
|
||||
|
||||
impl TopicService {
|
||||
/// 布置主题(老师)
|
||||
///
|
||||
/// 创建主题布置记录,验证老师是班级成员,
|
||||
/// 发布 TopicAssigned 事件。
|
||||
pub async fn assign_topic(
|
||||
tenant_id: Uuid,
|
||||
teacher_id: Uuid,
|
||||
class_id: Uuid,
|
||||
req: &CreateTopicReq,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<TopicResp> {
|
||||
// 验证班级存在
|
||||
let class = crate::entity::school_class::Entity::find()
|
||||
.filter(crate::entity::school_class::Column::Id.eq(class_id))
|
||||
.filter(crate::entity::school_class::Column::TenantId.eq(tenant_id))
|
||||
.filter(crate::entity::school_class::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound(format!("班级 {} 不存在", class_id)))?;
|
||||
|
||||
// 验证请求者是班级老师
|
||||
if class.teacher_id != teacher_id {
|
||||
return Err(DiaryError::Forbidden);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
let model = topic_assignment::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
class_id: Set(class_id),
|
||||
teacher_id: Set(teacher_id),
|
||||
title: Set(req.title.clone()),
|
||||
description: Set(req.description.clone()),
|
||||
due_date: Set(req.due_date),
|
||||
is_active: Set(true),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(teacher_id),
|
||||
updated_by: Set(teacher_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted = model.insert(db).await?;
|
||||
|
||||
// 发布 TopicAssigned 事件
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.topic.assigned",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"topic_id": id,
|
||||
"class_id": class_id,
|
||||
"teacher_id": teacher_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(topic_model_to_resp(inserted))
|
||||
}
|
||||
|
||||
/// 获取班级的主题列表
|
||||
///
|
||||
/// 按创建时间倒序返回班级下所有激活的主题。
|
||||
pub async fn list_topics(
|
||||
tenant_id: Uuid,
|
||||
class_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<TopicResp>> {
|
||||
let topics = topic_assignment::Entity::find()
|
||||
.filter(topic_assignment::Column::ClassId.eq(class_id))
|
||||
.filter(topic_assignment::Column::TenantId.eq(tenant_id))
|
||||
.filter(topic_assignment::Column::DeletedAt.is_null())
|
||||
.order_by_desc(topic_assignment::Column::CreatedAt)
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(topics.into_iter().map(topic_model_to_resp).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// topic_assignment::Model -> TopicResp
|
||||
fn topic_model_to_resp(model: topic_assignment::Model) -> TopicResp {
|
||||
TopicResp {
|
||||
id: model.id,
|
||||
class_id: model.class_id,
|
||||
teacher_id: model.teacher_id,
|
||||
title: model.title,
|
||||
description: model.description,
|
||||
due_date: model.due_date,
|
||||
is_active: model.is_active,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user