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 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 ... /// /// add a new Dto ... /// /// /// /// /// /// public async Task AddAsync( TDto item, bool saveChanges = true, string connectionId = null, CancellationToken cancellationToken = default ) { // var 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; } /// /// Update an Dto values ... /// /// /// /// /// /// /// public async Task UpdateAsync( TKey id, TDto item, bool saveChanges = true, string connectionId = null, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.UpdateAsync( id: id, item: item, saveChanges: saveChanges, cancellationToken: cancellationToken ); // if (!result.IsNullOrDefault()) { // await SendPush( action: XBaseEntityHubAction.Update.GetStringValue(), payLoad: result.ToJSON(camelCase: true), connectionId: connectionId ); } // return result; } /// /// remove an Dto by it's Id ... /// /// /// /// /// /// /// public async Task RemoveAsync( TKey id, bool softDelete = true, bool saveChanges = true, string connectionId = null, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.RemoveAsync( id: id, softDelete: softDelete, saveChanges: saveChanges, cancellationToken: cancellationToken ); // if (!result.IsNullOrDefault()) { // await SendPush( action: XBaseEntityHubAction.Update.GetStringValue(), payLoad: result.ToJSON(camelCase: true), connectionId: connectionId ); } // return result; } /// /// count all exists Dtos ... /// /// /// /// public async Task CountAsync( bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.CountAsync( cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// Check a Dto exists or not ... /// /// /// /// /// public async Task IsExistsAsync( TKey id, bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.IsExistsAsync( id: id, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// retrieve an Dto by it's Id ... /// /// /// /// /// /// public async Task GetAsync( TKey id, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.GetAsync( id: id, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// retrieve all exists Dtos ... /// /// /// /// /// /// public async Task> GetAllAsync( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.GetAllAsync( orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// find an Dto by providing a Conditional Expression ... /// /// /// /// /// /// public async Task FindOneAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { // var result = await RepositoryService.FindOneAsync( predicate: predicate, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// 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 ) { // var result = await RepositoryService.FindManyAsync( predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return result; } /// /// 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 ) { // var result = await RepositoryService.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() && 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 } }