Api/Brizco.Repository/MartenHandlers/Notifications/GetNotificationsQueryHandle...

37 lines
1.7 KiB
C#

namespace Brizco.Repository.MartenHandlers.Notifications;
public class GetNotificationsQueryHandler(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler<GetNotificationsQuery,List<Notification>>
{
public async Task<List<Notification>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken)
{
if (currentUserService.ComplexId == null)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is null");
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is wrong");
Guid userId;
if (request.UserId == null)
{
if (currentUserService.UserId == null)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "User id is null");
if (!Guid.TryParse(currentUserService.UserId, out userId))
throw new BaseApiException(ApiResultStatusCode.BadRequest, "User id is wrong");
}
else
userId = request.UserId.Value;
if (userId == default)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "User id is null");
var count = request.Count ?? 10;
var notifications = await martenRepositoryWrapper.SetRepository<Notification>()
.GetEntitiesAsync(n => n.ComplexId == complexId && n.UserId == userId,cancellationToken);
var response = notifications.OrderByDescending(n=>n.CreatedAt)
.Take(new Range(request.Page * count, (request.Page * count) + count))
.ToList();
return response;
}
}