refactor(diary): Service 层改用 DiaryEvent 枚举替代字符串事件
Some checks failed
Main Merge / backend (push) Has been cancelled
Main Merge / frontend (push) Has been cancelled

- journal_service: 3 处 (JournalCreated/Updated/Deleted)
- class_service: 2 处 (ClassCreated/StudentJoinedClass)
- comment_service: 1 处 (CommentCreated)
- topic_service: 1 处 (TopicAssigned)
- parent_service: 1 处 confirm_binding → ParentBound

保留 DomainEvent::new 的场景:
- class_service deactivate_class (diary.class.deactivated)
- parent_service bind_child (diary.parent.binding_requested)
- parent_service delete_child_data (diary.parent.data_deleted)
以上事件不在 DiaryEvent 枚举中(非核心创建事件)

测试: 509/509 全部通过
This commit is contained in:
iven
2026-06-03 17:15:00 +08:00
parent 38592d61ce
commit 1766cefde9
5 changed files with 58 additions and 74 deletions

View File

@@ -12,6 +12,8 @@ use crate::entity::{class_member, school_class};
use crate::error::{DiaryError, DiaryResult};
use erp_core::events::{DomainEvent, EventBus};
use crate::event::DiaryEvent;
/// 班级服务 — 6 位码生成、过期控制、成员管理
pub struct ClassService;
@@ -84,14 +86,11 @@ impl ClassService {
// 发布 ClassCreated 事件
event_bus
.publish(
DomainEvent::new(
"diary.class.created",
tenant_id,
serde_json::json!({
"class_id": id,
"teacher_id": teacher_id,
}),
),
DiaryEvent::ClassCreated {
class_id: id,
teacher_id,
}
.to_domain_event(tenant_id),
db,
)
.await;
@@ -229,14 +228,11 @@ impl ClassService {
// 8. 发布 StudentJoinedClass 事件
event_bus
.publish(
DomainEvent::new(
"diary.class.student_joined",
tenant_id,
serde_json::json!({
"class_id": class_id,
"student_id": user_id,
}),
),
DiaryEvent::StudentJoinedClass {
class_id,
student_id: user_id,
}
.to_domain_event(tenant_id),
db,
)
.await;

View File

@@ -11,7 +11,9 @@ use crate::entity::{class_member, comment, journal_entry};
use crate::error::{DiaryError, DiaryResult};
use crate::service::content_safety_service::ContentSafetyService;
use crate::service::notification_service::NotificationService;
use erp_core::events::{DomainEvent, EventBus};
use erp_core::events::EventBus;
use crate::event::DiaryEvent;
/// 评语服务 — 老师对学生日记的点评
///
@@ -81,17 +83,13 @@ impl CommentService {
// 5. 发布 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,
"content_preview": content.chars().take(50).collect::<String>(),
}),
),
DiaryEvent::CommentCreated {
comment_id: id,
journal_id,
teacher_id: author_id,
student_id: journal.author_id,
}
.to_domain_event(tenant_id),
db,
)
.await;

View File

@@ -11,7 +11,9 @@ use crate::dto::{CreateJournalReq, JournalResp, UpdateJournalReq};
use crate::entity::journal_entry;
use crate::error::{DiaryError, DiaryResult};
use erp_core::error::check_version;
use erp_core::events::{DomainEvent, EventBus};
use erp_core::events::EventBus;
use crate::event::DiaryEvent;
/// 日记 CRUD 服务 — 创建、读取、更新、软删除日记条目
pub struct JournalService;
@@ -57,15 +59,12 @@ impl JournalService {
// 发布领域事件
event_bus
.publish(
DomainEvent::new(
"diary.created",
tenant_id,
serde_json::json!({
"journal_id": id,
"author_id": author_id,
"class_id": req.class_id,
}),
),
DiaryEvent::JournalCreated {
journal_id: id,
author_id,
class_id: req.class_id,
}
.to_domain_event(tenant_id),
db,
)
.await;
@@ -153,15 +152,12 @@ impl JournalService {
// 发布领域事件
event_bus
.publish(
DomainEvent::new(
"diary.updated",
tenant_id,
serde_json::json!({
"journal_id": id,
"author_id": operator_id,
"version": new_version,
}),
),
DiaryEvent::JournalUpdated {
journal_id: id,
author_id: operator_id,
version: new_version,
}
.to_domain_event(tenant_id),
db,
)
.await;
@@ -206,14 +202,11 @@ impl JournalService {
// 发布领域事件
event_bus
.publish(
DomainEvent::new(
"diary.deleted",
tenant_id,
serde_json::json!({
"journal_id": id,
"author_id": operator_id,
}),
),
DiaryEvent::JournalDeleted {
journal_id: id,
author_id: operator_id,
}
.to_domain_event(tenant_id),
db,
)
.await;

View File

@@ -12,6 +12,8 @@ use crate::entity::parent_child_binding;
use crate::error::{DiaryError, DiaryResult};
use erp_core::events::{DomainEvent, EventBus};
use crate::event::DiaryEvent;
/// 家长中心服务 — 绑定管理、数据查阅、导出、删除
pub struct ParentService;
@@ -127,15 +129,11 @@ impl ParentService {
event_bus
.publish(
DomainEvent::new(
"diary.parent.binding_confirmed",
tenant_id,
serde_json::json!({
"parent_id": updated.parent_id,
"child_id": child_id,
"binding_id": binding_id,
}),
),
DiaryEvent::ParentBound {
parent_id: updated.parent_id,
child_id,
}
.to_domain_event(tenant_id),
db,
)
.await;

View File

@@ -11,7 +11,9 @@ use crate::dto::{CreateTopicReq, TopicResp, UpdateTopicReq};
use crate::entity::topic_assignment;
use crate::error::{DiaryError, DiaryResult};
use crate::service::notification_service::NotificationService;
use erp_core::events::{DomainEvent, EventBus};
use erp_core::events::EventBus;
use crate::event::DiaryEvent;
/// 主题布置服务 — 老师发布日记主题,学生提交对应日记
pub struct TopicService;
@@ -67,15 +69,12 @@ impl TopicService {
// 发布 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,
}),
),
DiaryEvent::TopicAssigned {
topic_id: id,
class_id,
teacher_id,
}
.to_domain_event(tenant_id),
db,
)
.await;