Api/Brizco.Repository/MartenHandlers/Notifications/GetNotificationsCountQueryH...

25 lines
1.2 KiB
C#

namespace Brizco.Repository.MartenHandlers.Notifications;
public class GetNotificationsCountQueryHandler(IMartenRepositoryWrapper martenRepositoryWrapper,ICurrentUserService currentUserService) : IRequestHandler<GetNotificationsCountQuery,int>
{
public async Task<int> Handle(GetNotificationsCountQuery 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 (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");
var notifications = await martenRepositoryWrapper.SetRepository<Notification>()
.GetEntitiesAsync(n => n.ComplexId == complexId && n.UserId == userId && !n.IsRead, cancellationToken);
return notifications.Count;
}
}