Files
TakeoutSaaS.TenantApi/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/SearchTenantNotificationsQueryHandler.cs

37 lines
1.4 KiB
C#

using System.Linq;
using MediatR;
using TakeoutSaaS.Application.App.Tenants.Dto;
using TakeoutSaaS.Application.App.Tenants.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Results;
namespace TakeoutSaaS.Application.App.Tenants.Handlers;
/// <summary>
/// 通知分页查询处理器。
/// </summary>
public sealed class SearchTenantNotificationsQueryHandler(ITenantNotificationRepository notificationRepository)
: IRequestHandler<SearchTenantNotificationsQuery, PagedResult<TenantNotificationDto>>
{
public async Task<PagedResult<TenantNotificationDto>> Handle(SearchTenantNotificationsQuery request, CancellationToken cancellationToken)
{
// 1. 查询通知
var notifications = await notificationRepository.SearchAsync(
request.TenantId,
request.Severity,
request.UnreadOnly,
null,
null,
cancellationToken);
// 2. 排序与分页
var ordered = notifications.OrderByDescending(x => x.SentAt).ToList();
var page = request.Page <= 0 ? 1 : request.Page;
var size = request.PageSize <= 0 ? 20 : request.PageSize;
var items = ordered.Skip((page - 1) * size).Take(size).Select(x => x.ToDto()).ToList();
// 3. 返回分页结果
return new PagedResult<TenantNotificationDto>(items, page, size, ordered.Count);
}
}