123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Linq;
- using Luticate2.Utils.Hubs;
- using Microsoft.AspNetCore.SignalR;
- using Microsoft.AspNetCore.SignalR.Infrastructure;
-
- namespace Luticate2.Utils.Business
- {
- public class LuNotificationsBusiness
- {
- private readonly LuHubConnectionTracker _connectionTracker;
- private readonly IHubContext _hubContext;
-
- public const string EventCreate = "EVENT_CREATE";
-
- public const string EventUpdate = "EVENT_UPDATE";
-
- public const string EventDelete = "EVENT_DELETE";
-
- public LuNotificationsBusiness(IConnectionManager connectionManager, LuHubConnectionTracker connectionTracker)
- {
- _connectionTracker = connectionTracker;
- _hubContext = connectionManager.GetHubContext<LuNotificationsHub>();
- }
-
- public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
- {
- var connectionsIds = _connectionTracker.Get<LuNotificationsHub>();
- var selectedIds = filter != null ? connectionsIds.Where(filter).ToList() : connectionsIds;
- _hubContext.Clients.Clients(selectedIds).notify(eventName, entityType, oldEntity, newEntity);
- }
-
- public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
- {
- Notify(EventCreate, entityType, null, newEntity, filter);
- }
-
- public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
- {
- Notify(EventUpdate, entityType, oldEntity, newEntity, filter);
- }
-
- public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
- {
- Notify(EventDelete, entityType, oldEntity, null, filter);
- }
- }
- }
|