前端: - fix(app): Isar native 文件直接导入 isar_database_native.dart,消除 5 个条件导出类型错误 - chore(app): build_runner 重新生成 .g.dart 文件 (102 outputs) - fix(app): 移除 secure_token_store_factory 未使用的 kIsWeb import 后端: - refactor(diary): 所有创建端点 POST 返回 201 Created (9 handler, 11 端点) - feat(diary): DiaryApiDoc OpenApi derive — 42 路径 + 32 Schema 汇总到 Swagger - feat(diary): DiaryEvent 枚举添加 event_type/payload/to_domain_event 方法 + 4 测试 测试: 84/84 erp-diary 通过, 509/509 全仓库通过, Flutter analyze 0 error
329 lines
9.8 KiB
Rust
329 lines
9.8 KiB
Rust
// 贴纸与模板 API 处理器
|
|
|
|
use axum::extract::{Extension, FromRef, Path, Query, State};
|
|
use axum::http::StatusCode;
|
|
use axum::response::Json;
|
|
use serde::Deserialize;
|
|
use utoipa::IntoParams;
|
|
use uuid::Uuid;
|
|
use validator::Validate;
|
|
|
|
use erp_core::error::AppError;
|
|
use erp_core::rbac::require_permission;
|
|
use erp_core::types::{ApiResponse, TenantContext};
|
|
|
|
use crate::dto::{CreateStickerPackReq, CreateStickerReq, StickerPackResp, StickerResp, TemplateResp, UpdateStickerPackReq};
|
|
use crate::service::sticker_service::StickerService;
|
|
use crate::state::DiaryState;
|
|
|
|
/// 贴纸包查询参数
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
pub struct StickerPackQuery {
|
|
pub category: Option<String>,
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/v1/diary/sticker-packs",
|
|
params(StickerPackQuery),
|
|
responses(
|
|
(status = 200, description = "成功", body = ApiResponse<Vec<StickerPackResp>>),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// GET /api/v1/diary/sticker-packs
|
|
///
|
|
/// 获取贴纸包列表。需要 `diary.journal.read` 权限。
|
|
pub async fn list_sticker_packs<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Query(query): Query<StickerPackQuery>,
|
|
) -> Result<Json<ApiResponse<Vec<StickerPackResp>>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "diary.journal.read")?;
|
|
|
|
let resp = StickerService::list_sticker_packs(
|
|
ctx.tenant_id,
|
|
query.category,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(ApiResponse::ok(resp)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/v1/diary/sticker-packs/{pack_id}/stickers",
|
|
params(("pack_id" = Uuid, Path, description = "贴纸包ID")),
|
|
responses(
|
|
(status = 200, description = "成功", body = ApiResponse<Vec<StickerResp>>),
|
|
(status = 404, description = "贴纸包不存在"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// GET /api/v1/diary/sticker-packs/:pack_id/stickers
|
|
///
|
|
/// 获取贴纸包内的贴纸列表。需要 `diary.journal.read` 权限。
|
|
pub async fn list_stickers_in_pack<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(pack_id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<Vec<StickerResp>>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "diary.journal.read")?;
|
|
|
|
let resp =
|
|
StickerService::list_stickers_in_pack(ctx.tenant_id, pack_id, &state.db).await?;
|
|
|
|
Ok(Json(ApiResponse::ok(resp)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/v1/diary/sticker-packs",
|
|
request_body = CreateStickerPackReq,
|
|
responses(
|
|
(status = 201, description = "创建成功", body = ApiResponse<StickerPackResp>),
|
|
(status = 400, description = "验证失败"),
|
|
(status = 401, description = "未授权"),
|
|
(status = 403, description = "权限不足"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// POST /api/v1/diary/sticker-packs
|
|
///
|
|
/// 创建贴纸包。需要 `diary.class.manage` 权限(管理端)。
|
|
pub async fn create_sticker_pack<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Json(req): Json<CreateStickerPackReq>,
|
|
) -> Result<(StatusCode, Json<ApiResponse<StickerPackResp>>), AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
req.validate().map_err(|e| AppError::Validation(e.to_string()))?;
|
|
require_permission(&ctx, "diary.class.manage")?;
|
|
|
|
if req.name.trim().is_empty() {
|
|
return Err(AppError::Validation("贴纸包名称不能为空".to_string()));
|
|
}
|
|
|
|
let resp = StickerService::create_sticker_pack(
|
|
ctx.tenant_id,
|
|
ctx.user_id,
|
|
&req,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok((StatusCode::CREATED, Json(ApiResponse::ok(resp))))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
put,
|
|
path = "/api/v1/diary/sticker-packs/{pack_id}",
|
|
params(("pack_id" = Uuid, Path, description = "贴纸包ID")),
|
|
request_body = UpdateStickerPackReq,
|
|
responses(
|
|
(status = 200, description = "更新成功", body = ApiResponse<StickerPackResp>),
|
|
(status = 400, description = "验证失败"),
|
|
(status = 401, description = "未授权"),
|
|
(status = 403, description = "权限不足"),
|
|
(status = 404, description = "贴纸包不存在"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// PUT /api/v1/diary/sticker-packs/:pack_id
|
|
///
|
|
/// 更新贴纸包(部分更新)。需要 `diary.class.manage` 权限。
|
|
pub async fn update_sticker_pack<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(pack_id): Path<Uuid>,
|
|
Json(req): Json<UpdateStickerPackReq>,
|
|
) -> Result<Json<ApiResponse<StickerPackResp>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
req.validate().map_err(|e| AppError::Validation(e.to_string()))?;
|
|
require_permission(&ctx, "diary.class.manage")?;
|
|
|
|
if let Some(ref name) = req.name {
|
|
if name.trim().is_empty() {
|
|
return Err(AppError::Validation("贴纸包名称不能为空".to_string()));
|
|
}
|
|
}
|
|
|
|
let resp = StickerService::update_sticker_pack(
|
|
ctx.tenant_id,
|
|
pack_id,
|
|
ctx.user_id,
|
|
&req,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(ApiResponse::ok(resp)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
delete,
|
|
path = "/api/v1/diary/sticker-packs/{pack_id}",
|
|
params(("pack_id" = Uuid, Path, description = "贴纸包ID")),
|
|
responses(
|
|
(status = 200, description = "删除成功"),
|
|
(status = 401, description = "未授权"),
|
|
(status = 403, description = "权限不足"),
|
|
(status = 404, description = "贴纸包不存在"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// DELETE /api/v1/diary/sticker-packs/:pack_id
|
|
///
|
|
/// 删除贴纸包(软删除)。需要 `diary.class.manage` 权限。
|
|
pub async fn delete_sticker_pack<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(pack_id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<()>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "diary.class.manage")?;
|
|
|
|
StickerService::delete_sticker_pack(ctx.tenant_id, pack_id, ctx.user_id, &state.db).await?;
|
|
|
|
Ok(Json(ApiResponse::ok(())))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/v1/diary/sticker-packs/{pack_id}/stickers",
|
|
params(("pack_id" = Uuid, Path, description = "贴纸包ID")),
|
|
request_body = CreateStickerReq,
|
|
responses(
|
|
(status = 201, description = "创建成功", body = ApiResponse<StickerResp>),
|
|
(status = 400, description = "验证失败"),
|
|
(status = 401, description = "未授权"),
|
|
(status = 403, description = "权限不足"),
|
|
(status = 404, description = "贴纸包不存在"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "贴纸管理"
|
|
)]
|
|
/// POST /api/v1/diary/sticker-packs/:pack_id/stickers
|
|
///
|
|
/// 在贴纸包内添加贴纸。需要 `diary.class.manage` 权限。
|
|
pub async fn create_sticker<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(pack_id): Path<Uuid>,
|
|
Json(req): Json<CreateStickerReq>,
|
|
) -> Result<(StatusCode, Json<ApiResponse<StickerResp>>), AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
req.validate().map_err(|e| AppError::Validation(e.to_string()))?;
|
|
require_permission(&ctx, "diary.class.manage")?;
|
|
|
|
if req.name.trim().is_empty() {
|
|
return Err(AppError::Validation("贴纸名称不能为空".to_string()));
|
|
}
|
|
|
|
let resp = StickerService::create_sticker(
|
|
ctx.tenant_id,
|
|
ctx.user_id,
|
|
pack_id,
|
|
&req,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok((StatusCode::CREATED, Json(ApiResponse::ok(resp))))
|
|
}
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
pub struct TemplateQuery {
|
|
pub category: Option<String>,
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/v1/diary/templates",
|
|
params(TemplateQuery),
|
|
responses(
|
|
(status = 200, description = "成功", body = ApiResponse<Vec<TemplateResp>>),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "模板管理"
|
|
)]
|
|
/// GET /api/v1/diary/templates
|
|
///
|
|
/// 获取模板列表。需要 `diary.journal.read` 权限。
|
|
pub async fn list_templates<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Query(query): Query<TemplateQuery>,
|
|
) -> Result<Json<ApiResponse<Vec<TemplateResp>>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "diary.journal.read")?;
|
|
|
|
let resp = StickerService::list_templates(
|
|
ctx.tenant_id,
|
|
query.category,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(ApiResponse::ok(resp)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/v1/diary/templates/{template_id}",
|
|
params(("template_id" = Uuid, Path, description = "模板ID")),
|
|
responses(
|
|
(status = 200, description = "成功", body = ApiResponse<TemplateResp>),
|
|
(status = 404, description = "模板不存在"),
|
|
),
|
|
security(("bearer_auth" = [])),
|
|
tag = "模板管理"
|
|
)]
|
|
/// GET /api/v1/diary/templates/:template_id
|
|
///
|
|
/// 获取模板详情(含布局数据)。需要 `diary.journal.read` 权限。
|
|
pub async fn get_template<S>(
|
|
State(state): State<DiaryState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(template_id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<TemplateResp>>, AppError>
|
|
where
|
|
DiaryState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "diary.journal.read")?;
|
|
|
|
let resp =
|
|
StickerService::get_template(ctx.tenant_id, template_id, &state.db).await?;
|
|
|
|
Ok(Json(ApiResponse::ok(resp)))
|
|
}
|