add some new features in data service ...
This commit is contained in:
@@ -0,0 +1,423 @@
|
||||
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 XBaseDtoProvider<TEntity, TDto, TKey> : IXBaseDtoProvider<TEntity, TDto, TKey>
|
||||
where TEntity : XBaseEntity<TKey>
|
||||
where TDto : XBaseEntityDto<TKey>
|
||||
{
|
||||
//
|
||||
#region Properties ...
|
||||
public IXIdentityProvider IdentityProvider { get; }
|
||||
public XDataServiceConfiguration DataConfiguration { get; }
|
||||
public IHubContext<XBaseDtoHub<TEntity, TDto, TKey>> Hub { get; }
|
||||
public IXBaseRepositoryService<TEntity, TDto, TKey> Service { get; }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
protected XBaseDtoProvider(
|
||||
IXIdentityProvider identityProvider,
|
||||
XDataServiceConfiguration dataConfiguration,
|
||||
IHubContext<XBaseDtoHub<TEntity, TDto, TKey>> hub,
|
||||
IXBaseRepositoryService<TEntity, TDto, TKey> service
|
||||
)
|
||||
{
|
||||
//
|
||||
Hub = hub;
|
||||
Service = service;
|
||||
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<TDto> AddAsync(
|
||||
TDto item,
|
||||
bool saveChanges = true,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await Service.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<TDto> UpdateAsync(
|
||||
TKey id,
|
||||
TDto item,
|
||||
bool saveChanges = true,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await Service.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<TDto> RemoveAsync(
|
||||
TKey id,
|
||||
bool softDelete = true,
|
||||
bool saveChanges = true,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await Service.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 Service.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 Service.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<TDto> GetAsync(
|
||||
TKey id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await Service.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<TDto>> 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 Service.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<TDto> FindOneAsync(
|
||||
Expression<Func<TEntity, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includeBuilder = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await Service.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<TDto>> 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 Service.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<TDto>> 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 Service.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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user