diff --git a/Interfaces/IXBaseProvider.cs b/Interfaces/IXBaseProvider.cs new file mode 100644 index 0000000..383f1c2 --- /dev/null +++ b/Interfaces/IXBaseProvider.cs @@ -0,0 +1,202 @@ +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 xDataService.Configuration; +using xDataService.Interfaces; +using xIdentityService.Interfaces; +using xModels.Base; +using xModels.Dtos; +using xPushService.Base; + +namespace xPushService.Interfaces +{ + public interface IXBaseProvider + where TEntity : XBaseEntity + where TDto : XBaseEntityDto + { + // + #region Props ... + // + IXIdentityProvider IdentityProvider { get; } + XDataServiceConfiguration DataConfiguration { get; } + IHubContext> Hub { get; } + IXBaseRepositoryService RepositoryService { get; } + #endregion + + // + #region Actions ... + /// + /// add a new Dto ... + /// + /// + /// + /// + /// + /// + Task AddAsync( + TDto item, + bool saveChanges = true, + string connectionId = null, + CancellationToken cancellationToken = default + ); + + /// + /// Update an Dto values ... + /// + /// + /// + /// + /// + /// + Task UpdateAsync( + TKey id, + TDto item, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + + /// + /// remove an Dto by it's Id ... + /// + /// + /// + /// + /// + /// + Task RemoveAsync( + TKey id, + bool softDelete = true, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Dtos ... + /// + /// + /// + /// + Task CountAsync( + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// Check a Dto exists or not ... + /// + /// + /// + /// + /// + Task IsExistsAsync( + TKey id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve an Dto by it's Id ... + /// + /// + /// + /// + /// + /// + Task GetAsync( + TKey id, + bool ignoreSoftDeleteds = true, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve all exists Dtos ... + /// + /// + /// + /// + /// + /// + Task> GetAllAsync( + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find an Dto by providing a Conditional Expression ... + /// + /// + /// + /// + /// + /// + Task FindOneAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find a collection of Dtos by proving a Conditional Expression ... + /// + /// + /// + /// + /// + /// + /// + Task> FindManyAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve Dtos based on XQuery Pagination structure ... + /// + /// + /// + /// + /// + /// + /// + /// + Task> QueryAsync( + XQuery query, + bool ignoreSoftDeleteds = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Hub Actions ... + /// + /// Send Custom Push Message ... + /// + /// + /// + /// + /// + /// + Task SendPush( + string action, + string payLoad, + string connectionId = null, + CancellationToken cancellationToken = default + ); + #endregion + } +} \ No newline at end of file diff --git a/Providers/XBaseProvider.cs b/Providers/XBaseProvider.cs new file mode 100644 index 0000000..fa34dcb --- /dev/null +++ b/Providers/XBaseProvider.cs @@ -0,0 +1,349 @@ +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.Base; +using xPushService.Constants; +using xPushService.Interfaces; + +namespace xPushService.Providers +{ + public abstract class XBaseProvider : IXBaseProvider + where TEntity : XBaseEntity + where TDto : XBaseEntityDto + { + // + #region Props ... + public IXIdentityProvider IdentityProvider { get; } + public XDataServiceConfiguration DataConfiguration { get; } + public IHubContext> Hub { get; } + public IXBaseRepositoryService RepositoryService { get; } + #endregion + + // + #region Actions ... + // + #region Add ... + /// + /// add a new Dto ... + /// + /// + /// + /// + /// + /// + public async Task AddAsync( + TDto item, + bool saveChanges = true, + string connectionId = null, + CancellationToken cancellationToken = default + ) + { + // + TDto result = await RepositoryService.AddAsync( + item: item, + saveChanges: saveChanges, + cancellationToken: cancellationToken + ); + + // + if (!result.IsNullOrDefault()) + { + // + await SendPush( + action: XBaseEntityHubAction.Add.GetStringValue(), + payLoad: result.ToJSON(camelCase: true), + connectionId: connectionId + ); + } + + // + return result; + } + + // + #region Update ... + /// + /// Update an Dto values ... + /// + /// + /// + /// + /// + /// + public async Task UpdateAsync( + TKey id, + TDto item, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + + /// + /// Update a range of Dtos ... + /// + /// + /// + /// + /// + public async Task UpdateRangeAsync( + IEnumerable items, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Remove ... + /// + /// remove an Dto by it's Id ... + /// + /// + /// + /// + /// + /// + public async Task RemoveAsync( + TKey id, + bool softDelete = true, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + + /// + /// remove an Dto ... + /// + /// + /// + /// + /// + /// + public async Task RemoveAsync( + TDto item, + bool softDelete = true, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + + /// + /// remove a range of exists Dtos ... + /// + /// + /// + /// + /// + /// + public async Task RemoveRangeAsync( + IEnumerable items, + bool softDelete = true, + bool saveChanges = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Count ... + /// + /// count all exists Dtos ... + /// + /// + /// + /// + public async Task CountAsync( + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + + /// + /// count all exists Entities Pages by providing page size ... + /// + /// + /// + /// + /// + /// + public async Task PagesCountAsync( + int pageSize, + int? totalItems = null, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Retrieve ... + /// + /// retrieve an Dto by it's Id ... + /// + /// + /// + /// + /// + /// + public async Task GetAsync( + TKey id, + bool ignoreSoftDeleteds = true, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve all exists Dtos ... + /// + /// + /// + /// + /// + /// + public async Task> GetAllAsync( + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find an Dto by providing a Conditional Expression ... + /// + /// + /// + /// + /// + /// + public async Task FindOneAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// find a collection of Dtos by proving a Conditional Expression ... + /// + /// + /// + /// + /// + /// + /// + public async Task> FindManyAsync( + Expression> predicate, + bool ignoreSoftDeleteds = true, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + + /// + /// retrieve Dtos based on XQuery Pagination structure ... + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> QueryAsync( + XQuery query, + bool ignoreSoftDeleteds = true, + Expression> predicate = null, + Func, IOrderedQueryable> orderBuilder = null, + Func, IIncludableQueryable> includeBuilder = null, + CancellationToken cancellationToken = default + ); + #endregion + + // + #region Exists ... + /// + /// Check a Dto exists or not ... + /// + /// + /// + /// + /// + public async Task IsExistsAsync( + TKey id, + bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ); + #endregion + #endregion + + // + #region Hub Actions ... + /// + /// Send Custom Push Message ... + /// + /// + /// + /// + /// + /// + public async Task SendPush( + string action, + string payLoad, + string connectionId = null, + CancellationToken cancellationToken = default + ) + { + // + var actions = new List + { + 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() && + actions.Contains(action); + if (!isValid) + { + return; + } + + // + var clients = Hub.Clients.All; + if (!connectionId.IsNullOrEmpty()) + { + clients = Hub.Clients.AllExcept(connectionId); + } + + // + try + { + // + await clients.SendAsync( + action, + payLoad, + connectionId, + cancellationToken + ); + } + catch { } + } + #endregion + } + + internal class XTagDto + { + } +} \ No newline at end of file