Api/Brizco.Repository/MartenHandlers/Notifications/CreateNotificationHandlerCo...

33 lines
1.4 KiB
C#

using Brizco.Domain.MartenEntities.Notifications;
namespace Brizco.Repository.MartenHandlers.Notifications;
public class CreateNotificationHandlerCommand : IRequestHandler<CreateNotificationCommand,Guid>
{
private readonly IMartenRepositoryWrapper _martenRepositoryWrapper;
private readonly ICurrentUserService _currentUserService;
public CreateNotificationHandlerCommand(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService)
{
_martenRepositoryWrapper = martenRepositoryWrapper;
_currentUserService = currentUserService;
}
public async Task<Guid> Handle(CreateNotificationCommand 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");
var notification = new Notification
{
Message = request.Message,
UserId = request.UserId,
ComplexId = complexId,
CreatedAt = DateTime.Now
};
await _martenRepositoryWrapper.SetRepository<Notification>()
.AddOrUpdateEntityAsync(notification, cancellationToken);
return notification.Id;
}
}