fix: 公告模块第一次修复

This commit is contained in:
2025-12-27 18:22:30 +08:00
parent bc09d9ca2e
commit 57f4c2d394
7 changed files with 63 additions and 11 deletions

View File

@@ -37,6 +37,11 @@ public sealed class CreateTenantAnnouncementCommandHandler(
throw new BusinessException(ErrorCodes.ValidationFailed, "目标受众类型不能为空");
}
if (request.EffectiveTo.HasValue && request.EffectiveFrom >= request.EffectiveTo.Value)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "生效开始时间必须早于结束时间");
}
if (request.TenantId == 0 && request.PublisherScope != PublisherScope.Platform)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "TenantId=0 仅允许平台公告");

View File

@@ -58,8 +58,15 @@ public sealed class PublishAnnouncementCommandHandler(
announcement.RevokedAt = null;
announcement.RowVersion = request.RowVersion;
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
try
{
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
}
catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException")
{
throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试");
}
// 4. 发布领域事件
await eventPublisher.PublishAsync(

View File

@@ -52,8 +52,15 @@ public sealed class RevokeAnnouncementCommandHandler(
announcement.RevokedAt = DateTime.UtcNow;
announcement.RowVersion = request.RowVersion;
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
try
{
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
}
catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException")
{
throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试");
}
// 4. 发布领域事件
await eventPublisher.PublishAsync(

View File

@@ -52,8 +52,15 @@ public sealed class UpdateTenantAnnouncementCommandHandler(ITenantAnnouncementRe
announcement.RowVersion = request.RowVersion;
// 4. 持久化
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
try
{
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
}
catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException")
{
throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试");
}
// 5. 返回 DTO
return announcement.ToDto(false, null);