using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using xCommons.Extensions; using xModels.Base; using xPushService.Constants; using xPushService.Interfaces; namespace xPushService.Base { public abstract class XBaseEntityHub : XBaseHub, IXBaseEntityHub where TEntity : XBaseEntity { // #region Constructor ... protected XBaseEntityHub(ILogger logger) : base(logger) { } #endregion // #region Actions ... /// /// Notify a New Entity added ... /// /// /// /// public async Task Add( TEntity entity, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Add.GetStringValue(), entity.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify a New Entity added ... /// /// /// /// public async Task AddOrUpdate( TEntity entity, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.AddOrUpdate.GetStringValue(), entity.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Add Many Entities ... /// /// /// /// public async Task AddMany( IEnumerable entities, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.AddOrUpdate.GetStringValue(), entities.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify an Entity Deleted ... /// /// /// /// public async Task Delete( TEntity entity, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Delete.GetStringValue(), entity.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Delete Many Entities ... /// /// /// /// public async Task DeleteMany( IEnumerable entities, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.DeleteMany.GetStringValue(), entities.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify an Entity Updated ... /// /// /// /// public async Task Update( TEntity entity, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Update.GetStringValue(), entity.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Update Many Entities ... /// /// /// /// public async Task UpdateMany( IEnumerable entities, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.UpdateMany.GetStringValue(), entities.ToJSON(camelCase: true), connectionId, cancellationToken ); } } #endregion } }