You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuNotificationsBusiness.cs 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Linq;
  3. using Luticate2.Utils.Hubs;
  4. using Luticate2.Utils.Interfaces;
  5. using Microsoft.AspNetCore.SignalR;
  6. using Microsoft.AspNetCore.SignalR.Infrastructure;
  7. namespace Luticate2.Utils.Business
  8. {
  9. public class LuNotificationsBusiness : ILuNotificationsBusiness
  10. {
  11. private readonly LuHubConnectionTracker _connectionTracker;
  12. private readonly IHubContext _hubContext;
  13. public const string EventCreate = "EVENT_CREATE";
  14. public const string EventUpdate = "EVENT_UPDATE";
  15. public const string EventDelete = "EVENT_DELETE";
  16. public LuNotificationsBusiness(IConnectionManager connectionManager, LuHubConnectionTracker connectionTracker)
  17. {
  18. _connectionTracker = connectionTracker;
  19. _hubContext = connectionManager.GetHubContext<LuNotificationsHub>();
  20. }
  21. public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
  22. {
  23. var connectionsIds = _connectionTracker.Get<LuNotificationsHub>();
  24. var selectedIds = filter != null ? connectionsIds.Where(filter).ToList() : connectionsIds;
  25. _hubContext.Clients.Clients(selectedIds).notify(eventName, entityType, oldEntity, newEntity);
  26. }
  27. public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
  28. {
  29. Notify(EventCreate, entityType, null, newEntity, filter);
  30. }
  31. public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
  32. {
  33. Notify(EventUpdate, entityType, oldEntity, newEntity, filter);
  34. }
  35. public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
  36. {
  37. Notify(EventDelete, entityType, oldEntity, null, filter);
  38. }
  39. }
  40. }