Files
TakeoutSaaS.AdminApi/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/UpdateTenantAnnouncementCommandHandler.cs
MSuMshk 755b61a044 fix: 修复公告模块核心问题并完善功能
主要修复内容:
1. 修复 RowVersion 并发控制
   - 配置 EF Core RowVersion 映射为 bytea 类型
   - 添加 PostgreSQL 触发器自动生成 RowVersion
   - 在更新/发布/撤销操作中添加 RowVersion 校验
   - 移除 Application 层对 EF Core 的直接依赖

2. 修复 API 路由和校验问题
   - 添加平台公告列表路由的版本化别名
   - 租户公告接口添加 X-Tenant-Id 必填校验,返回 400
   - 生效时间校验返回 422 而非 500
   - 修复 FluentValidation 异常命名冲突

3. 实现关键词搜索功能
   - 在查询参数中添加 keyword 字段
   - 使用 PostgreSQL ILIKE 实现大小写不敏感搜索
   - 支持标题和内容字段的模糊匹配

4. 数据库迁移
   - 新增 RowVersion 触发器迁移文件
   - 回填现有公告记录的 RowVersion
2025-12-26 09:16:07 +08:00

63 lines
2.5 KiB
C#

using MediatR;
using TakeoutSaaS.Application.App.Tenants.Commands;
using TakeoutSaaS.Application.App.Tenants.Dto;
using TakeoutSaaS.Domain.Tenants.Enums;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
namespace TakeoutSaaS.Application.App.Tenants.Handlers;
/// <summary>
/// 更新公告处理器。
/// </summary>
public sealed class UpdateTenantAnnouncementCommandHandler(ITenantAnnouncementRepository announcementRepository)
: IRequestHandler<UpdateTenantAnnouncementCommand, TenantAnnouncementDto?>
{
public async Task<TenantAnnouncementDto?> Handle(UpdateTenantAnnouncementCommand request, CancellationToken cancellationToken)
{
// 1. 校验输入
if (string.IsNullOrWhiteSpace(request.Title) || string.IsNullOrWhiteSpace(request.Content))
{
throw new BusinessException(ErrorCodes.ValidationFailed, "公告标题和内容不能为空");
}
if (request.RowVersion == null || request.RowVersion.Length == 0)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "RowVersion 不能为空");
}
// 2. 查询公告
var announcement = await announcementRepository.FindByIdAsync(request.TenantId, request.AnnouncementId, cancellationToken);
if (announcement == null)
{
return null;
}
if (announcement.Status != AnnouncementStatus.Draft)
{
if (announcement.Status == AnnouncementStatus.Published)
{
throw new BusinessException(ErrorCodes.Conflict, "已发布公告不可编辑,要编辑已发布公告,请先撤销");
}
throw new BusinessException(ErrorCodes.Conflict, "仅草稿公告允许编辑");
}
// 3. 更新字段
announcement.Title = request.Title.Trim();
announcement.Content = request.Content;
announcement.TargetType = string.IsNullOrWhiteSpace(request.TargetType) ? announcement.TargetType : request.TargetType.Trim();
announcement.TargetParameters = request.TargetParameters;
announcement.IsActive = false;
announcement.RowVersion = request.RowVersion;
// 4. 持久化
await announcementRepository.UpdateAsync(announcement, cancellationToken);
await announcementRepository.SaveChangesAsync(cancellationToken);
// 5. 返回 DTO
return announcement.ToDto(false, null);
}
}