Files
TakeoutSaaS.TenantApi/src/Application/TakeoutSaaS.Application/Identity/Handlers/UpdateMenuCommandHandler.cs
MSuMshk 5a26f82628 refactor: 将 Permission 和 MenuDefinition 改为系统级实体
- Permission 和 MenuDefinition 改为继承 AuditableEntityBase(移除 TenantId)
- 添加 PortalType 枚举区分平台端/租户端
- Repository 使用 IgnoreQueryFilters() 查询系统级数据
- 更新所有相关 Handler 和 DTO,移除 TenantId 引用
- 与 AdminApi 保持一致的设计

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 14:49:27 +08:00

56 lines
2.2 KiB
C#

using MediatR;
using TakeoutSaaS.Application.Identity;
using TakeoutSaaS.Application.Identity.Commands;
using TakeoutSaaS.Application.Identity.Contracts;
using TakeoutSaaS.Domain.Identity.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
namespace TakeoutSaaS.Application.Identity.Handlers;
/// <summary>
/// 更新菜单处理器。
/// </summary>
public sealed class UpdateMenuCommandHandler(IMenuRepository menuRepository)
: IRequestHandler<UpdateMenuCommand, MenuDefinitionDto?>
{
/// <inheritdoc />
public async Task<MenuDefinitionDto?> Handle(UpdateMenuCommand request, CancellationToken cancellationToken)
{
// 1. 菜单固定时禁止修改
if (!MenuPolicy.CanMaintainMenus)
{
throw new BusinessException(ErrorCodes.Forbidden, "菜单已固定,禁止修改");
}
// 2. 校验存在
var entity = await menuRepository.FindByIdAsync(request.Id, cancellationToken)
?? throw new BusinessException(ErrorCodes.NotFound, "菜单不存在");
// 3. 更新字段
entity.ParentId = request.ParentId;
entity.Name = request.Name.Trim();
entity.Path = request.Path.Trim();
entity.Component = request.Component.Trim();
entity.Title = request.Title.Trim();
entity.Icon = request.Icon?.Trim();
entity.IsIframe = request.IsIframe;
entity.Link = string.IsNullOrWhiteSpace(request.Link) ? null : request.Link.Trim();
entity.KeepAlive = request.KeepAlive;
entity.SortOrder = request.SortOrder;
entity.RequiredPermissions = MenuMapper.JoinCodes(request.RequiredPermissions);
entity.MetaPermissions = MenuMapper.JoinCodes(request.MetaPermissions);
entity.MetaRoles = MenuMapper.JoinCodes(request.MetaRoles);
entity.AuthListJson = request.AuthList.Count == 0
? null
: System.Text.Json.JsonSerializer.Serialize(request.AuthList);
// 4. 持久化
await menuRepository.UpdateAsync(entity, cancellationToken);
await menuRepository.SaveChangesAsync(cancellationToken);
// 5. 返回 DTO
return MenuMapper.ToDto(entity);
}
}