Files
xPushService/Base/XBaseEntityHub.cs
T

214 lines
6.6 KiB
C#

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<TEntity, TKey> : XBaseHub, IXBaseEntityHub<TEntity, TKey>
where TEntity : XBaseEntity<TKey>
{
//
#region Constructor ...
protected XBaseEntityHub(ILogger<XBaseHub> logger) : base(logger)
{ }
#endregion
//
#region Actions ...
/// <summary>
/// Notify a New Entity added ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
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
);
}
}
/// <summary>
/// Notify a New Entity added ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
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
);
}
}
/// <summary>
/// Notify Add Many Entities ...
/// </summary>
/// <param name="entities"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task AddMany(
IEnumerable<TEntity> 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
);
}
}
/// <summary>
/// Notify an Entity Deleted ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
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
);
}
}
/// <summary>
/// Notify Delete Many Entities ...
/// </summary>
/// <param name="entities"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task DeleteMany(
IEnumerable<TEntity> 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
);
}
}
/// <summary>
/// Notify an Entity Updated ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
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
);
}
}
/// <summary>
/// Notify Update Many Entities ...
/// </summary>
/// <param name="entities"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task UpdateMany(
IEnumerable<TEntity> 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
}
}