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 XBaseDtoHub : XBaseHub, IXBaseDtoHub where TEntity : XBaseEntity where TDto : XBaseEntityDto { // #region Constructor ... protected XBaseDtoHub(ILogger logger) : base(logger) { } #endregion // #region Actions ... /// /// Notify a New Item added ... /// /// /// /// public async Task Add( TDto item, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Add.GetStringValue(), item.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify a New Item added ... /// /// /// /// public async Task AddOrUpdate( TDto item, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.AddOrUpdate.GetStringValue(), item.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Add Many Items ... /// /// /// /// public async Task AddMany( IEnumerable items, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.AddOrUpdate.GetStringValue(), items.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify an Item Deleted ... /// /// /// /// public async Task Delete( TDto item, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Delete.GetStringValue(), item.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Delete Many Items ... /// /// /// /// public async Task DeleteMany( IEnumerable items, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.DeleteMany.GetStringValue(), items.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify an Item Updated ... /// /// /// /// public async Task Update( TDto item, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.Update.GetStringValue(), item.ToJSON(camelCase: true), connectionId, cancellationToken ); } } /// /// Notify Update Many Items ... /// /// /// /// public async Task UpdateMany( IEnumerable items, CancellationToken cancellationToken = default ) { // var connectionId = Context.ConnectionId; if (!connectionId.IsNullOrEmpty()) { // await Clients .AllExcept(connectionId) .SendAsync( XBaseEntityHubAction.UpdateMany.GetStringValue(), items.ToJSON(camelCase: true), connectionId, cancellationToken ); } } #endregion } }