Api/Brizco.Repository/MartenHandlers/Notifications/ReadNotificationCommandHand...

22 lines
1.1 KiB
C#

namespace Brizco.Repository.MartenHandlers.Notifications;
public class ReadNotificationCommandHandler(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler<ReadNotificationCommand, bool>
{
public async Task<bool> Handle(ReadNotificationCommand request, CancellationToken cancellationToken)
{
var notification = await martenRepositoryWrapper.SetRepository<Notification>()
.GetEntityAsync(request.Id, cancellationToken);
if (notification == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Notification not found");
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
throw new BaseApiException(ApiResultStatusCode.NotFound, "User id is null");
if (notification.UserId != userId)
throw new BaseApiException(ApiResultStatusCode.NotFound, "This is not your notification");
notification.IsRead = true;
await martenRepositoryWrapper.SetRepository<Notification>()
.AddOrUpdateEntityAsync(notification, cancellationToken);
return true;
}
}