fix(config): add individual menu CRUD endpoints and fix frontend menu data handling

- Add POST /config/menus (create single menu)
- Add PUT /config/menus/{id} (update single menu)
- Add DELETE /config/menus/{id} (soft delete single menu)
- Frontend MenuConfig was calling individual CRUD routes that didn't exist,
  causing 404 errors on all create/update/delete operations
- Fix frontend to correctly handle nested tree response from backend
This commit is contained in:
iven
2026-04-11 14:34:48 +08:00
parent 0d7d3af0a8
commit 4bfd9573db
3 changed files with 95 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
use axum::Extension;
use axum::extract::{FromRef, Json, State};
use axum::extract::{FromRef, Json, Path, State};
use axum::response::Json as JsonResponse;
use validator::Validate;
@@ -12,11 +12,9 @@ use crate::config_state::ConfigState;
use crate::dto::{BatchSaveMenusReq, CreateMenuReq, MenuResp};
use crate::service::menu_service::MenuService;
/// GET /api/v1/menus
/// GET /api/v1/config/menus
///
/// 获取当前租户下当前用户角色可见的菜单树。
/// 根据用户关联的角色过滤菜单可见性。
/// 需要 `menu.list` 权限。
pub async fn get_menus<S>(
State(state): State<ConfigState>,
Extension(ctx): Extension<TenantContext>,
@@ -38,11 +36,75 @@ where
Ok(JsonResponse(ApiResponse::ok(menus)))
}
/// PUT /api/v1/menus/batch
/// POST /api/v1/config/menus
///
/// 创建单个菜单项。
pub async fn create_menu<S>(
State(state): State<ConfigState>,
Extension(ctx): Extension<TenantContext>,
Json(req): Json<CreateMenuReq>,
) -> Result<JsonResponse<ApiResponse<MenuResp>>, AppError>
where
ConfigState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "menu.update")?;
req.validate()
.map_err(|e| AppError::Validation(e.to_string()))?;
let resp = MenuService::create(
ctx.tenant_id,
ctx.user_id,
&req,
&state.db,
&state.event_bus,
)
.await?;
Ok(JsonResponse(ApiResponse::ok(resp)))
}
/// PUT /api/v1/config/menus/{id}
///
/// 更新单个菜单项。
pub async fn update_menu<S>(
State(state): State<ConfigState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<Uuid>,
Json(req): Json<crate::dto::UpdateMenuReq>,
) -> Result<JsonResponse<ApiResponse<MenuResp>>, AppError>
where
ConfigState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "menu.update")?;
let resp = MenuService::update(id, ctx.tenant_id, ctx.user_id, &req, &state.db).await?;
Ok(JsonResponse(ApiResponse::ok(resp)))
}
/// DELETE /api/v1/config/menus/{id}
///
/// 软删除单个菜单项。
pub async fn delete_menu<S>(
State(state): State<ConfigState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<Uuid>,
) -> Result<JsonResponse<ApiResponse<()>>, AppError>
where
ConfigState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "menu.update")?;
MenuService::delete(id, ctx.tenant_id, ctx.user_id, &state.db, &state.event_bus).await?;
Ok(JsonResponse(ApiResponse::ok(())))
}
/// PUT /api/v1/config/menus/batch
///
/// 批量保存菜单列表。
/// 对每个菜单项:有 id 的执行更新,没有 id 的执行创建。
/// 需要 `menu.update` 权限。
pub async fn batch_save_menus<S>(
State(state): State<ConfigState>,
Extension(ctx): Extension<TenantContext>,

View File

@@ -56,7 +56,14 @@ impl ConfigModule {
// Menu routes
.route(
"/config/menus",
get(menu_handler::get_menus).put(menu_handler::batch_save_menus),
get(menu_handler::get_menus)
.post(menu_handler::create_menu)
.put(menu_handler::batch_save_menus),
)
.route(
"/config/menus/{id}",
put(menu_handler::update_menu)
.delete(menu_handler::delete_menu),
)
// Setting routes
.route(