128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Extensions.Logging;
|
|
using xCommons.Extensions;
|
|
using xModels.Base;
|
|
using xPushService.Constants;
|
|
|
|
namespace xPushService.Base
|
|
{
|
|
public abstract class XBaseEntityHub<TEntity, TKey> : XBaseHub
|
|
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>
|
|
/// <returns></returns>
|
|
public async Task Add(TEntity entity)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.Add.GetStringValue(), entity, connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify a New Entity added ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task AddOrUpdate(TEntity entity)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.AddOrUpdate.GetStringValue(), entity, connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Add Many Entities ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task AddMany(IEnumerable<TEntity> entities)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.AddOrUpdate.GetStringValue(), entities.ToJSON(), connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify an Entity Deleted ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task Delete(TEntity entity)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.Delete.GetStringValue(), entity, connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Delete Many Entities ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task DeleteMany(IEnumerable<TEntity> entities)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.DeleteMany.GetStringValue(), entities.ToJSON(), connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify an Entity Updated ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task Update(TEntity entity)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.Update.GetStringValue(), entity, connectionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Update Many Entities ...
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateMany(IEnumerable<TEntity> entities)
|
|
{
|
|
//
|
|
var connectionId = Context.ConnectionId;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
await Clients.AllExcept(connectionId).SendAsync(XBaseEntityHubAction.UpdateMany.GetStringValue(), entities.ToJSON(), connectionId);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |