215 lines
6.6 KiB
C#
215 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 XBaseDtoHub<TEntity, TDto, TKey> : XBaseHub, IXBaseDtoHub<TEntity, TDto, TKey>
|
|
where TEntity : XBaseEntity<TKey>
|
|
where TDto : XBaseEntityDto<TKey>
|
|
{
|
|
//
|
|
#region Constructor ...
|
|
protected XBaseDtoHub(ILogger<XBaseHub> logger) : base(logger)
|
|
{ }
|
|
#endregion
|
|
|
|
//
|
|
#region Actions ...
|
|
/// <summary>
|
|
/// Notify a New Item added ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify a New Item added ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Add Many Items ...
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task AddMany(
|
|
IEnumerable<TDto> 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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify an Item Deleted ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Delete Many Items ...
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task DeleteMany(
|
|
IEnumerable<TDto> 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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify an Item Updated ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notify Update Many Items ...
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task UpdateMany(
|
|
IEnumerable<TDto> 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
|
|
}
|
|
} |