422 lines
14 KiB
C#
422 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
using xCommons.Extensions;
|
|
using xDataService.Configuration;
|
|
using xDataService.Interfaces;
|
|
using xIdentityService.Interfaces;
|
|
using xModels.Base;
|
|
using xModels.Dtos;
|
|
using xPushService.Constants;
|
|
using xPushService.Interfaces;
|
|
|
|
namespace xPushService.Base
|
|
{
|
|
public abstract class XBaseEntityProvider<TEntity, TKey> : IXBaseEntityProviderr<TEntity, TKey>
|
|
where TEntity : XBaseEntity<TKey>
|
|
{
|
|
//
|
|
#region Properties ...
|
|
public IXIdentityProvider IdentityProvider { get; }
|
|
public IXBaseRepository<TEntity, TKey> Repository { get; }
|
|
public XDataServiceConfiguration DataConfiguration { get; }
|
|
public IHubContext<XBaseEntityHub<TEntity, TKey>> Hub { get; }
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
protected XBaseEntityProvider(
|
|
IXIdentityProvider identityProvider,
|
|
IXBaseRepository<TEntity, TKey> repository,
|
|
XDataServiceConfiguration dataConfiguration,
|
|
IHubContext<XBaseEntityHub<TEntity, TKey>> hub
|
|
)
|
|
{
|
|
//
|
|
Hub = hub;
|
|
Repository = repository;
|
|
IdentityProvider = identityProvider;
|
|
DataConfiguration = dataConfiguration;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Actions ...
|
|
/// <summary>
|
|
/// add a new Item ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="saveChanges"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> AddAsync(
|
|
TEntity item,
|
|
bool saveChanges = true,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.AddAsync(
|
|
item: item,
|
|
saveChanges: saveChanges,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
var isValid = !result.IsNullOrDefault();
|
|
if (isValid)
|
|
{
|
|
//
|
|
await SendPush(
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken,
|
|
payLoad: result.ToJSON(camelCase: true),
|
|
action: XBaseEntityHubAction.Add.GetStringValue()
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update an Item values ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="item"></param>
|
|
/// <param name="saveChanges"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> UpdateAsync(
|
|
TKey id,
|
|
TEntity item,
|
|
bool saveChanges = true,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.UpdateAsync(
|
|
id: id,
|
|
item: item,
|
|
saveChanges: saveChanges,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
var isValid = !result.IsNullOrDefault();
|
|
if (isValid)
|
|
{
|
|
//
|
|
await SendPush(
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken,
|
|
payLoad: result.ToJSON(camelCase: true),
|
|
action: XBaseEntityHubAction.Update.GetStringValue()
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// remove an Item by it's Id ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="softDelete"></param>
|
|
/// <param name="saveChanges"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> RemoveAsync(
|
|
TKey id,
|
|
bool softDelete = true,
|
|
bool saveChanges = true,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.RemoveAsync(
|
|
id: id,
|
|
softDelete: softDelete,
|
|
saveChanges: saveChanges,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
var isValid = !result.IsNullOrDefault();
|
|
if (isValid)
|
|
{
|
|
//
|
|
await SendPush(
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken,
|
|
payLoad: result.ToJSON(camelCase: true),
|
|
action: XBaseEntityHubAction.Delete.GetStringValue()
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// count all exists Items ...
|
|
/// </summary>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<int> CountAsync(
|
|
bool ignoreSoftDeleteds = true,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.CountAsync(
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an Item exists or not ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> IsExistsAsync(
|
|
TKey id,
|
|
bool ignoreSoftDeleteds = true,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.IsExistsAsync(
|
|
id: id,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve an Item by it's Id ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="includeBuilder"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> GetAsync(
|
|
TKey id,
|
|
bool ignoreSoftDeleteds = true,
|
|
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.GetAsync(
|
|
id: id,
|
|
includeBuilder: includeBuilder,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve all exists Items ...
|
|
/// </summary>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="orderBuilder"></param>
|
|
/// <param name="includeBuilder"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(
|
|
bool ignoreSoftDeleteds = true,
|
|
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBuilder = null,
|
|
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.GetAllAsync(
|
|
orderBuilder: orderBuilder,
|
|
includeBuilder: includeBuilder,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// find an Item by providing a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="predicate"></param>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="includeBuilder"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> FindOneAsync(
|
|
Expression<Func<TEntity, bool>> predicate,
|
|
bool ignoreSoftDeleteds = true,
|
|
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.FindOneAsync(
|
|
predicate: predicate,
|
|
includeBuilder: includeBuilder,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// find a collection of Items by proving a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="predicate"></param>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="orderBuilder"></param>
|
|
/// <param name="includeBuilder"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<TEntity>> FindManyAsync(
|
|
Expression<Func<TEntity, bool>> predicate,
|
|
bool ignoreSoftDeleteds = true,
|
|
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBuilder = null,
|
|
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.FindManyAsync(
|
|
predicate: predicate,
|
|
orderBuilder: orderBuilder,
|
|
includeBuilder: includeBuilder,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Items based on XQuery Pagination structure ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="ignoreSoftDeleteds"></param>
|
|
/// <param name="predicate"></param>
|
|
/// <param name="orderBuilder"></param>
|
|
/// <param name="includeBuilder"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XQueryResult<TEntity>> QueryAsync(
|
|
XQuery query,
|
|
bool ignoreSoftDeleteds = true,
|
|
Expression<Func<TEntity, bool>> predicate = null,
|
|
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBuilder = null,
|
|
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await Repository.QueryAsync(
|
|
query: query,
|
|
predicate: predicate,
|
|
orderBuilder: orderBuilder,
|
|
includeBuilder: includeBuilder,
|
|
cancellationToken: cancellationToken,
|
|
ignoreSoftDeleteds: ignoreSoftDeleteds
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Hub Actions ...
|
|
/// <summary>
|
|
/// Send Custom Push Message ...
|
|
/// </summary>
|
|
/// <param name="action"></param>
|
|
/// <param name="payLoad"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task SendPush(
|
|
string action,
|
|
string payLoad,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var actions = new List<string>
|
|
{
|
|
XBaseEntityHubAction.Add.GetStringValue(),
|
|
XBaseEntityHubAction.Update.GetStringValue(),
|
|
XBaseEntityHubAction.Delete.GetStringValue(),
|
|
XBaseEntityHubAction.AddMany.GetStringValue(),
|
|
XBaseEntityHubAction.DeleteMany.GetStringValue(),
|
|
XBaseEntityHubAction.UpdateMany.GetStringValue(),
|
|
XBaseEntityHubAction.AddOrUpdate.GetStringValue(),
|
|
};
|
|
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!Hub.IsNull() &&
|
|
!action.IsNullOrEmpty() &&
|
|
!payLoad.IsNullOrEmpty() &&
|
|
actions.Contains(action);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Retrieve Connection Id ...
|
|
var clients = Hub.Clients.All;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
clients = Hub.Clients.AllExcept(connectionId);
|
|
}
|
|
|
|
//
|
|
await clients.SendAsync(
|
|
action,
|
|
payLoad,
|
|
connectionId,
|
|
cancellationToken
|
|
);
|
|
}
|
|
#endregion
|
|
}
|
|
} |