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

44 lines
2.0 KiB
C#

using Brizco.Domain.MartenEntities.Notifications;
namespace Brizco.Repository.MartenHandlers.Notifications;
public class GetNotificationsQueryHandler : IRequestHandler<GetNotificationsQuery,List<Notification>>
{
private readonly IMartenRepositoryWrapper _martenRepositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public GetNotificationsQueryHandler(IMartenRepositoryWrapper martenRepositoryWrapper,ICurrentUserService currentUserService)
{
_martenRepositoryWrapper = martenRepositoryWrapper;
_currentUserService = currentUserService;
}
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");
if (request.UserId == default)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "User id is null");
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;
var notifications = await _martenRepositoryWrapper.SetRepository<Notification>()
.GetQueryAsync(n => n.ComplexId == complexId && n.UserId == userId);
var response = await notifications.Take(new Range(request.Page * request.Count, (request.Page * request.Count) + request.Count))
.ToListAsync(cancellationToken);
return response;
}
}