| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 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<AddNotificationCommand, Unit>
- {
- private readonly ICustomerRepository _customerService;
-
- public AddNotificationHandler(ICustomerRepository customerRepository)
- {
- _customerService = customerRepository;
- }
-
- public async Task<Unit> 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();
- }
- }
- }
|