Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AddNotificationHandler.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Diligent.WebAPI.Business.Interfaces;
  2. using Diligent.WebAPI.Data.Entities;
  3. using Diligent.WebAPI.Host.Exceptions;
  4. using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
  5. using MediatR;
  6. namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers
  7. {
  8. public class AddNotificationHandler : IRequestHandler<AddNotificationCommand, Unit>
  9. {
  10. private readonly ICustomerService _customerService;
  11. public AddNotificationHandler(ICustomerService customerService)
  12. {
  13. _customerService = customerService;
  14. }
  15. public async Task<Unit> Handle(AddNotificationCommand request, CancellationToken cancellationToken)
  16. {
  17. var receiver = await _customerService.GetCustomerById(request.Notification.ReceiverId);
  18. if (receiver == null)
  19. {
  20. throw new NotFoundException();
  21. }
  22. var notificationExists = receiver.Notifications.Where(x => x.RoomId == request.Notification.RoomId).FirstOrDefault();
  23. if (notificationExists == null)
  24. {
  25. var notification = new Notification
  26. {
  27. Count = 1,
  28. RoomId = request.Notification.RoomId
  29. };
  30. receiver.Notifications.Add(notification);
  31. }
  32. else
  33. {
  34. notificationExists.Count++;
  35. }
  36. await _customerService.AddNotification(receiver);
  37. return new Unit();
  38. }
  39. }
  40. }