fix: 公告模块第一次修复
This commit is contained in:
@@ -94,8 +94,8 @@ public sealed class PlatformAnnouncementsController(IMediator mediator, ITenantC
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("platform-announcement:create")]
|
||||
[SwaggerOperation(Summary = "查询平台公告列表", Description = "需要权限:platform-announcement:create")]
|
||||
[PermissionAuthorize("platform-announcement:read", "platform-announcement:create")]
|
||||
[SwaggerOperation(Summary = "查询平台公告列表", Description = "需要权限:platform-announcement:read 或 platform-announcement:create")]
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<TenantAnnouncementDto>>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ApiResponse<PagedResult<TenantAnnouncementDto>>> List([FromQuery] GetTenantsAnnouncementsQuery query, CancellationToken cancellationToken)
|
||||
@@ -126,8 +126,8 @@ public sealed class PlatformAnnouncementsController(IMediator mediator, ITenantC
|
||||
/// </code>
|
||||
/// </remarks>
|
||||
[HttpGet("{announcementId:long}")]
|
||||
[PermissionAuthorize("platform-announcement:create")]
|
||||
[SwaggerOperation(Summary = "获取平台公告详情", Description = "需要权限:platform-announcement:create")]
|
||||
[PermissionAuthorize("platform-announcement:read", "platform-announcement:create")]
|
||||
[SwaggerOperation(Summary = "获取平台公告详情", Description = "需要权限:platform-announcement:read 或 platform-announcement:create")]
|
||||
[ProducesResponseType(typeof(ApiResponse<TenantAnnouncementDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status403Forbidden)]
|
||||
|
||||
@@ -8,6 +8,7 @@ using TakeoutSaaS.Application.App.Tenants.Dto;
|
||||
using TakeoutSaaS.Application.App.Tenants.Queries;
|
||||
using TakeoutSaaS.Module.Authorization.Attributes;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
using TakeoutSaaS.Shared.Web.Api;
|
||||
|
||||
namespace TakeoutSaaS.AdminApi.Controllers;
|
||||
@@ -18,7 +19,7 @@ namespace TakeoutSaaS.AdminApi.Controllers;
|
||||
[ApiVersion("1.0")]
|
||||
[Authorize]
|
||||
[Route("api/admin/v{version:apiVersion}/tenants/{tenantId:long}/announcements")]
|
||||
public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiController
|
||||
public sealed class TenantAnnouncementsController(IMediator mediator, ITenantContextAccessor tenantContextAccessor) : BaseApiController
|
||||
{
|
||||
private const string TenantIdHeaderName = "X-Tenant-Id";
|
||||
|
||||
@@ -49,6 +50,13 @@ public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiC
|
||||
[ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status403Forbidden)]
|
||||
public async Task<ApiResponse<PagedResult<TenantAnnouncementDto>>> Search(long tenantId, [FromQuery] GetTenantsAnnouncementsQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!Request.Headers.TryGetValue(TenantIdHeaderName, out var tenantHeader) || string.IsNullOrWhiteSpace(tenantHeader))
|
||||
{
|
||||
var request = query with { TenantId = 0 };
|
||||
var platformResult = await ExecuteAsPlatformAsync(() => mediator.Send(request, cancellationToken));
|
||||
return ApiResponse<PagedResult<TenantAnnouncementDto>>.Ok(platformResult);
|
||||
}
|
||||
|
||||
var headerError = EnsureTenantHeader<PagedResult<TenantAnnouncementDto>>();
|
||||
if (headerError != null)
|
||||
{
|
||||
@@ -369,4 +377,18 @@ public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiC
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task<T> ExecuteAsPlatformAsync<T>(Func<Task<T>> action)
|
||||
{
|
||||
var original = tenantContextAccessor.Current;
|
||||
tenantContextAccessor.Current = new TenantContext(0, null, "platform");
|
||||
try
|
||||
{
|
||||
return await action();
|
||||
}
|
||||
finally
|
||||
{
|
||||
tenantContextAccessor.Current = original;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 仅允许平台公告");
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace TakeoutSaaS.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[DbContext(typeof(TakeoutAppDbContext))]
|
||||
[Migration("20251225090000_AddTenantAnnouncementRowVersionTrigger")]
|
||||
public partial class AddTenantAnnouncementRowVersionTrigger : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user