using Diligent.WebAPI.Business.Interfaces; using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Host.Exceptions; using Diligent.WebAPI.Host.Mediator.Notifications.Commands; using MediatR; namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers { public class AddNotificationHandler : IRequestHandler { private readonly ICustomerRepository _customerService; public AddNotificationHandler(ICustomerRepository customerRepository) { _customerService = customerRepository; } public async Task Handle(AddNotificationCommand request, CancellationToken cancellationToken) { var receiver = await _customerService.GetCustomerById(request.Notification.ReceiverId); if (receiver == null) { throw new NotFoundException(); } var notificationExists = receiver.Notifications.Where(x => x.RoomId == request.Notification.RoomId).FirstOrDefault(); if (notificationExists == null) { var notification = new Notification { Count = 1, RoomId = request.Notification.RoomId }; receiver.Notifications.Add(notification); } else { notificationExists.Count++; } await _customerService.AddNotification(receiver); return new Unit(); } } }