2024-08-11 19:20:09 +03:30
|
|
|
|
namespace Brizco.Repository.MartenHandlers.Notifications;
|
2024-05-29 19:17:41 +03:30
|
|
|
|
|
2024-07-25 18:42:18 +03:30
|
|
|
|
public class ReadNotificationCommandHandler(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler<ReadNotificationCommand, bool>
|
2024-05-29 19:17:41 +03:30
|
|
|
|
{
|
|
|
|
|
|
public async Task<bool> Handle(ReadNotificationCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-07-25 18:42:18 +03:30
|
|
|
|
var notification = await martenRepositoryWrapper.SetRepository<Notification>()
|
2024-05-29 19:17:41 +03:30
|
|
|
|
.GetEntityAsync(request.Id, cancellationToken);
|
|
|
|
|
|
if (notification == null)
|
2024-07-25 18:42:18 +03:30
|
|
|
|
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");
|
2024-05-29 19:17:41 +03:30
|
|
|
|
|
|
|
|
|
|
notification.IsRead = true;
|
|
|
|
|
|
|
2024-07-25 18:42:18 +03:30
|
|
|
|
await martenRepositoryWrapper.SetRepository<Notification>()
|
2024-05-29 19:17:41 +03:30
|
|
|
|
.AddOrUpdateEntityAsync(notification, cancellationToken);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|