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 : IXBaseEntityProvider where TEntity : XBaseEntity where THub : XBaseEntityHub { // #region Properties ... public IHubContext Hub { get; } public IXIdentityProvider IdentityProvider { get; } public IXBaseRepository Repository { get; } public XDataServiceConfiguration DataConfiguration { get; } #endregion // #region Constructor ... protected XBaseEntityProvider( IHubContext hub, IXBaseRepository repository, XDataServiceConfiguration dataConfiguration, IXIdentityProvider identityProvider = null ) { // Hub = hub; Repository = repository; IdentityProvider = identityProvider; DataConfiguration = dataConfiguration; } #endregion // #region Actions ... /// /// add a new Item ... /// /// /// /// /// /// public virtual async Task 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; } /// /// Update an Item values ... /// /// /// /// /// /// /// public virtual async Task 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; } /// /// remove an Item by it's Id ... /// /// /// /// /// /// /// public virtual async Task 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; } /// /// count all exists Items ... /// /// /// /// public virtual async Task CountAsync( bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // var result = await Repository.CountAsync( cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// Check an Item exists or not ... /// /// /// /// /// public virtual async Task IsExistsAsync( TKey id, bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // var result = await Repository.IsExistsAsync( id: id, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// retrieve an Item by it's Id ... /// /// /// /// /// /// public virtual async Task GetAsync( TKey id, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await Repository.GetAsync( id: id, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// retrieve all exists Items ... /// /// /// /// /// /// public virtual async Task> GetAllAsync( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await Repository.GetAllAsync( orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// find an Item by providing a Conditional Expression ... /// /// /// /// /// /// public virtual async Task FindOneAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await Repository.FindOneAsync( predicate: predicate, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// find a collection of Items by proving a Conditional Expression ... /// /// /// /// /// /// /// public virtual async Task> FindManyAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await Repository.FindManyAsync( predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// retrieve Items based on XQuery Pagination structure ... /// /// /// /// /// /// /// /// public virtual async Task> QueryAsync( XQuery query, bool ignoreSoftDeleteds = true, Expression> predicate = null, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> 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 ... /// /// 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() && !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 } }