feat(diary): 手写引擎 + 日记 CRUD + 同步 API (Phase F3 + B2)
Flutter 手写引擎 (Phase F3): - stroke_model.dart: 笔画数据模型 (StrokePoint/Stroke/BrushType) - stroke_renderer.dart: perfect_freehand 渲染管线 + 四画笔参数 - handwriting_canvas.dart: Listener 输入 + 掌心抑制 + 去抖过滤 - editor_bloc.dart: BLoC 状态管理 + 撤销/重做 (50步) Rust 日记 CRUD + 同步 (Phase B2): - journal_service.rs: CRUD + 软删除 + 分页列表 + 事件发布 - sync_service.rs: 版本号同步 + 冲突检测 - journal_handler.rs: 5个API端点 + utoipa注解 + 权限守卫 - sync_handler.rs: 同步API端点 - error.rs: From<DiaryError> for AppError + 8个单元测试 - 路由注册: /diary/journals + /diary/sync 验证: - cargo check: 0 error - cargo test: 433 测试全通过 - flutter analyze: 1 warning (unused private param)
This commit is contained in:
263
crates/erp-diary/src/handler/journal_handler.rs
Normal file
263
crates/erp-diary/src/handler/journal_handler.rs
Normal file
@@ -0,0 +1,263 @@
|
||||
// 日记 API 处理器 — CRUD + 列表
|
||||
|
||||
use axum::extract::{Extension, FromRef, Path, Query, State};
|
||||
use axum::response::Json;
|
||||
use serde::Deserialize;
|
||||
use utoipa::IntoParams;
|
||||
use uuid::Uuid;
|
||||
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
|
||||
use crate::dto::{CreateJournalReq, JournalResp, UpdateJournalReq};
|
||||
use crate::service::journal_service::JournalService;
|
||||
use crate::state::DiaryState;
|
||||
|
||||
/// 日记列表查询参数
|
||||
#[derive(Debug, Deserialize, IntoParams)]
|
||||
pub struct JournalListParams {
|
||||
/// 按作者筛选
|
||||
pub author_id: Option<Uuid>,
|
||||
/// 按心情筛选 (happy/calm/sad/angry/thinking)
|
||||
pub mood: Option<String>,
|
||||
/// 日期范围起始
|
||||
pub date_from: Option<chrono::NaiveDate>,
|
||||
/// 日期范围结束
|
||||
pub date_to: Option<chrono::NaiveDate>,
|
||||
/// 按班级筛选
|
||||
pub class_id: Option<Uuid>,
|
||||
/// 页码(默认 1)
|
||||
pub page: Option<u64>,
|
||||
/// 每页条数(默认 20,最大 100)
|
||||
pub page_size: Option<u64>,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/journals",
|
||||
request_body = CreateJournalReq,
|
||||
responses(
|
||||
(status = 200, description = "创建成功", body = ApiResponse<JournalResp>),
|
||||
(status = 400, description = "验证失败"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记管理"
|
||||
)]
|
||||
/// POST /api/v1/diary/journals
|
||||
///
|
||||
/// 创建日记条目。需要 `diary.journal.create` 权限。
|
||||
pub async fn create_journal<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<CreateJournalReq>,
|
||||
) -> Result<Json<ApiResponse<JournalResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.create")?;
|
||||
|
||||
// 基础验证
|
||||
if req.title.trim().is_empty() {
|
||||
return Err(AppError::Validation("标题不能为空".to_string()));
|
||||
}
|
||||
|
||||
let resp = JournalService::create(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
&req,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/journals/{id}",
|
||||
params(("id" = Uuid, Path, description = "日记ID")),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<JournalResp>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "日记不存在"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/journals/:id
|
||||
///
|
||||
/// 获取日记详情。需要 `diary.journal.read` 权限。
|
||||
pub async fn get_journal<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<JournalResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = JournalService::get_by_id(ctx.tenant_id, id, &state.db).await?;
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/api/v1/diary/journals/{id}",
|
||||
params(("id" = Uuid, Path, description = "日记ID")),
|
||||
request_body = UpdateJournalReq,
|
||||
responses(
|
||||
(status = 200, description = "更新成功", body = ApiResponse<JournalResp>),
|
||||
(status = 400, description = "验证失败"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "日记不存在"),
|
||||
(status = 409, description = "版本冲突"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记管理"
|
||||
)]
|
||||
/// PUT /api/v1/diary/journals/:id
|
||||
///
|
||||
/// 更新日记。需要 `diary.journal.update` 权限。
|
||||
/// 请求体中必须包含 `version` 字段用于乐观锁检查。
|
||||
pub async fn update_journal<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(req): Json<UpdateJournalReq>,
|
||||
) -> Result<Json<ApiResponse<JournalResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.update")?;
|
||||
|
||||
let resp = JournalService::update(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
id,
|
||||
&req,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
|
||||
/// 删除日记请求体(包含版本号)
|
||||
#[derive(Debug, Deserialize, utoipa::ToSchema)]
|
||||
pub struct DeleteJournalReq {
|
||||
/// 当前版本号(乐观锁)
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/api/v1/diary/journals/{id}",
|
||||
params(("id" = Uuid, Path, description = "日记ID")),
|
||||
request_body = DeleteJournalReq,
|
||||
responses(
|
||||
(status = 200, description = "删除成功"),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 404, description = "日记不存在"),
|
||||
(status = 409, description = "版本冲突"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记管理"
|
||||
)]
|
||||
/// DELETE /api/v1/diary/journals/:id
|
||||
///
|
||||
/// 软删除日记。需要 `diary.journal.delete` 权限。
|
||||
/// 请求体中必须包含 `version` 字段用于乐观锁检查。
|
||||
pub async fn delete_journal<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(req): Json<DeleteJournalReq>,
|
||||
) -> Result<Json<ApiResponse<()>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.delete")?;
|
||||
|
||||
JournalService::delete(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
id,
|
||||
req.version,
|
||||
&state.db,
|
||||
&state.event_bus,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse {
|
||||
success: true,
|
||||
data: None,
|
||||
message: Some("日记已删除".to_string()),
|
||||
}))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/v1/diary/journals",
|
||||
params(JournalListParams),
|
||||
responses(
|
||||
(status = 200, description = "成功", body = ApiResponse<PaginatedResponse<JournalResp>>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记管理"
|
||||
)]
|
||||
/// GET /api/v1/diary/journals
|
||||
///
|
||||
/// 获取日记列表(分页 + 筛选)。需要 `diary.journal.read` 权限。
|
||||
/// 支持按作者、心情、日期范围、班级筛选。
|
||||
pub async fn list_journals<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Query(params): Query<JournalListParams>,
|
||||
) -> Result<Json<ApiResponse<PaginatedResponse<JournalResp>>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let page = params.page.unwrap_or(1);
|
||||
let page_size = params.page_size.unwrap_or(20).min(100);
|
||||
|
||||
let (items, total) = JournalService::list(
|
||||
ctx.tenant_id,
|
||||
params.author_id,
|
||||
params.mood,
|
||||
params.date_from,
|
||||
params.date_to,
|
||||
params.class_id,
|
||||
page,
|
||||
page_size,
|
||||
&state.db,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let total_pages = total.div_ceil(page_size);
|
||||
|
||||
Ok(Json(ApiResponse::ok(PaginatedResponse {
|
||||
data: items,
|
||||
total,
|
||||
page,
|
||||
page_size,
|
||||
total_pages,
|
||||
})))
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
// erp-diary API 处理器占位
|
||||
// 后续 Phase B2-B7 会实现 ~10 个处理器
|
||||
// erp-diary API 处理器
|
||||
|
||||
pub mod journal_handler;
|
||||
pub mod sync_handler;
|
||||
|
||||
53
crates/erp-diary/src/handler/sync_handler.rs
Normal file
53
crates/erp-diary/src/handler/sync_handler.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
// 日记同步 API 处理器
|
||||
|
||||
use axum::extract::{Extension, FromRef, State};
|
||||
use axum::response::Json;
|
||||
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, TenantContext};
|
||||
|
||||
use crate::dto::SyncReq;
|
||||
use crate::dto::SyncResp;
|
||||
use crate::service::sync_service::SyncService;
|
||||
use crate::state::DiaryState;
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/v1/diary/sync",
|
||||
request_body = SyncReq,
|
||||
responses(
|
||||
(status = 200, description = "同步成功", body = ApiResponse<SyncResp>),
|
||||
(status = 401, description = "未授权"),
|
||||
(status = 403, description = "权限不足"),
|
||||
(status = 409, description = "存在版本冲突"),
|
||||
),
|
||||
security(("bearer_auth" = [])),
|
||||
tag = "日记同步"
|
||||
)]
|
||||
/// POST /api/v1/diary/sync
|
||||
///
|
||||
/// 日记同步端点。客户端提交本地变更,服务端返回服务端变更和冲突列表。
|
||||
/// 需要 `diary.journal.read` 权限。
|
||||
pub async fn sync_journals<S>(
|
||||
State(state): State<DiaryState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<SyncReq>,
|
||||
) -> Result<Json<ApiResponse<SyncResp>>, AppError>
|
||||
where
|
||||
DiaryState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "diary.journal.read")?;
|
||||
|
||||
let resp = SyncService::sync(
|
||||
ctx.tenant_id,
|
||||
ctx.user_id,
|
||||
req.last_sync_time,
|
||||
req.changes,
|
||||
&state.db,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ApiResponse::ok(resp)))
|
||||
}
|
||||
Reference in New Issue
Block a user