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.8KB

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