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(); } public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func filter = null) { var connectionsIds = _connectionTracker.Get(); 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 filter = null) { Notify(EventCreate, entityType, null, newEntity, filter); } public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func filter = null) { Notify(EventUpdate, entityType, oldEntity, newEntity, filter); } public void NotifyDelete(string entityType, object oldEntity, Func filter = null) { Notify(EventDelete, entityType, oldEntity, null, filter); } } }