using BuildFlow.Shared.Domain.Exceptions; using MediatR; using BuildFlow.Platform.Application.Roles.DTOs; using BuildFlow.Platform.Domain.Entities.Role; using BuildFlow.Platform.Infrastructure.Persistence; using BuildFlow.Platform.Application.Roles.Commands; namespace BuildFlow.Platform.Application.Roles.Handlers; /// /// Create角色Command处理器。执行业务校验后持久化。 /// public class CreateRoleCommandHandler(PlatformDbContext db) : IRequestHandler { public async Task Handle(CreateRoleCommand request, CancellationToken ct) { if (await db.Roles.AnyAsync(r => r.Name == request.Name, ct)) throw new BusinessException("角色名已存在"); var role = new RoleEntity { Name = request.Name, Description = request.Description }; await db.Roles.AddAsync(role, ct); await db.SaveChangesAsync(ct); return new RoleDto(role.Id, role.Name, role.Description, role.CreatedAt, []); } }