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

View File

@@ -11,7 +11,9 @@ use crate::entity::{class_member, comment, journal_entry};
use crate::error::{DiaryError, DiaryResult}; use crate::error::{DiaryError, DiaryResult};
use crate::service::content_safety_service::ContentSafetyService; use crate::service::content_safety_service::ContentSafetyService;
use crate::service::notification_service::NotificationService; 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 事件 // 5. 发布 CommentCreated 事件
event_bus event_bus
.publish( .publish(
DomainEvent::new( DiaryEvent::CommentCreated {
"diary.comment.created", comment_id: id,
tenant_id, journal_id,
serde_json::json!({ teacher_id: author_id,
"comment_id": id, student_id: journal.author_id,
"journal_id": journal_id, }
"teacher_id": author_id, .to_domain_event(tenant_id),
"student_id": journal.author_id,
"content_preview": content.chars().take(50).collect::<String>(),
}),
),
db, db,
) )
.await; .await;

View File

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

View File

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

View File

@@ -11,7 +11,9 @@ use crate::dto::{CreateTopicReq, TopicResp, UpdateTopicReq};
use crate::entity::topic_assignment; use crate::entity::topic_assignment;
use crate::error::{DiaryError, DiaryResult}; use crate::error::{DiaryError, DiaryResult};
use crate::service::notification_service::NotificationService; use crate::service::notification_service::NotificationService;
use erp_core::events::{DomainEvent, EventBus}; use erp_core::events::EventBus;
use crate::event::DiaryEvent;
/// 主题布置服务 — 老师发布日记主题,学生提交对应日记 /// 主题布置服务 — 老师发布日记主题,学生提交对应日记
pub struct TopicService; pub struct TopicService;
@@ -67,15 +69,12 @@ impl TopicService {
// 发布 TopicAssigned 事件 // 发布 TopicAssigned 事件
event_bus event_bus
.publish( .publish(
DomainEvent::new( DiaryEvent::TopicAssigned {
"diary.topic.assigned", topic_id: id,
tenant_id, class_id,
serde_json::json!({ teacher_id,
"topic_id": id, }
"class_id": class_id, .to_domain_event(tenant_id),
"teacher_id": teacher_id,
}),
),
db, db,
) )
.await; .await;