using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore.Query; using xAiModels.Configurations; using xAiModels.Interfaces.Dtos; using xAiModels.Interfaces.Enrichers; using xAiModels.Interfaces.Entities; using xAiModels.Models.Dtos; using xAiModels.Models.Entities; using xCommons.Extensions; using xDataService.Providers; using xModels.Dtos; namespace xAiModels.Providers.Dtos { public class XAiConversationRepositoryService : XBaseRepositoryService, IXAiConversationRepositoryService { private readonly IXAiConversationEnricher enricher; public XAiConversationRepositoryService( IXAiConversationEnricher enricher, IXAiConversationRepository repository ) : base( repository, mapperProfiles: [ new XAiModelMappersProfile() ] ) { this.enricher = enricher; } // #region Actions ... // #region Add ... /// /// add a new Dto ... /// /// /// /// /// public override async Task AddAsync( XAiConversationDto item, bool saveChanges = true, CancellationToken cancellationToken = default ) { return await AddAsync( item: item, fillMessages: false, saveChanges: saveChanges, cancellationToken: cancellationToken ); } /// /// add or update a Dto (add if not exists/update if exists) ... /// /// /// /// /// public override async Task AddOrUpdateAsync( XAiConversationDto item, bool saveChanges = true, CancellationToken cancellationToken = default ) { return await AddOrUpdateAsync( item: item, fillMessages: false, saveChanges: saveChanges, cancellationToken: cancellationToken ); } #endregion // #region Update ... /// /// Update an Dto values ... /// /// /// /// /// /// public override async Task UpdateAsync( Guid id, XAiConversationDto item, bool saveChanges = true, CancellationToken cancellationToken = default ) { return await UpdateAsync( id: id, item: item, fillMessages: false, saveChanges: saveChanges, cancellationToken: cancellationToken ); } #endregion // #region Remove ... /// /// remove an Dto by it's Id ... /// /// /// /// /// /// public override async Task RemoveAsync( Guid id, bool softDelete = true, bool saveChanges = true, CancellationToken cancellationToken = default ) { return await RemoveAsync( id: id, fillMessages: false, softDelete: softDelete, saveChanges: saveChanges, cancellationToken: cancellationToken ); } /// /// remove an Dto ... /// /// /// /// /// /// public override async Task RemoveAsync( XAiConversationDto item, bool softDelete = true, bool saveChanges = true, CancellationToken cancellationToken = default ) { return await RemoveAsync( item: item, fillMessages: false, softDelete: softDelete, saveChanges: saveChanges, cancellationToken: cancellationToken ); } #endregion // #region Retrieve ... /// /// retrieve an Dto by it's Id ... /// /// /// /// /// /// public override async Task GetAsync( Guid id, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return await GetAsync( id: id, fillMessages: false, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } /// /// retrieve all exists Dtos ... /// /// /// /// /// /// public override async Task> GetAllAsync( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return await GetAllAsync( fillMessages: false, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } /// /// retrieve all exists Dtos /// as Async Enumerable ... /// /// /// /// /// /// public override IAsyncEnumerable GetAllAsAsyncEnumerable( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return GetAllAsAsyncEnumerable( fillMessages: false, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } /// /// find an Dto by providing a Conditional Expression ... /// /// /// /// /// /// public override async Task FindOneAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return await FindOneAsync( fillMessages: false, predicate: predicate, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } /// /// find a collection of Dtos by proving a Conditional Expression ... /// /// /// /// /// /// /// public override async Task> FindManyAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return await FindManyAsync( fillMessages: false, predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } /// /// retrieve Dtos based on XQuery Pagination structure ... /// /// /// /// /// /// /// /// public override async Task> QueryAsync( XQuery query, bool ignoreSoftDeleteds = true, Expression> predicate = null, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, CancellationToken cancellationToken = default ) { return await QueryAsync( query: query, fillMessages: false, predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } #endregion #endregion // #region Actions ... // #region Add ... /// /// add a new Dto ... /// /// /// /// /// /// public virtual async Task AddAsync( XAiConversationDto item, bool saveChanges = true, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.AddAsync( item: item, saveChanges: saveChanges, cancellationToken: cancellationToken ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// add or update a Dto (add if not exists/update if exists) ... /// /// /// /// /// /// public virtual async Task AddOrUpdateAsync( XAiConversationDto item, bool saveChanges = true, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.AddAsync( item: item, saveChanges: saveChanges, cancellationToken: cancellationToken ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } #endregion // #region Update ... /// /// Update an Dto values ... /// /// /// /// /// /// /// public virtual async Task UpdateAsync( Guid id, XAiConversationDto item, bool saveChanges = true, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.UpdateAsync( id: id, item: item, saveChanges: saveChanges, cancellationToken: cancellationToken ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } #endregion // #region Remove ... /// /// remove an Dto by it's Id ... /// /// /// /// /// /// /// public virtual async Task RemoveAsync( Guid id, bool softDelete = true, bool saveChanges = true, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.RemoveAsync( id: id, softDelete: softDelete, saveChanges: saveChanges, cancellationToken: cancellationToken ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// remove an Dto ... /// /// /// /// /// /// /// public virtual async Task RemoveAsync( XAiConversationDto item, bool softDelete = true, bool saveChanges = true, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.RemoveAsync( item: item, softDelete: softDelete, saveChanges: saveChanges, cancellationToken: cancellationToken ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } #endregion // #region Retrieve ... /// /// retrieve an Dto by it's Id ... /// /// /// /// /// /// /// public virtual async Task GetAsync( Guid id, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.GetAsync( id: id, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// retrieve all exists Dtos ... /// /// /// /// /// /// /// public virtual async Task> GetAllAsync( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.GetAllAsync( orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichRangeAsync( sources: result, destinations: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// retrieve all exists Dtos /// as Async Enumerable ... /// /// /// /// /// /// /// public virtual async IAsyncEnumerable GetAllAsAsyncEnumerable( bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, bool fillMessages = true, [EnumeratorCancellation] CancellationToken cancellationToken = default ) { // var enumerable = base.GetAllAsAsyncEnumerable( orderBuilder: orderBuilder, includeBuilder: includeBuilder, ignoreSoftDeleteds: ignoreSoftDeleteds ); await foreach (var dto in enumerable) { // if (cancellationToken.IsCancellationRequested) { yield break; } // await enricher.EnrichAsync( source: dto, destination: dto, fillMessages: fillMessages, cancellationToken: cancellationToken ); yield return dto; } } /// /// find an Dto by providing a Conditional Expression ... /// /// /// /// /// /// /// public virtual async Task FindOneAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IIncludableQueryable> includeBuilder = null, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.FindOneAsync( predicate: predicate, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichAsync( source: result, destination: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// find a collection of Dtos by proving a Conditional Expression ... /// /// /// /// /// /// /// /// public virtual async Task> FindManyAsync( Expression> predicate, bool ignoreSoftDeleteds = true, Func, IOrderedQueryable> orderBuilder = null, Func, IIncludableQueryable> includeBuilder = null, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.FindManyAsync( predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichRangeAsync( sources: result, destinations: result, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } /// /// retrieve Dtos 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, bool fillMessages = true, CancellationToken cancellationToken = default ) { // var result = await base.QueryAsync( query: query, predicate: predicate, orderBuilder: orderBuilder, includeBuilder: includeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // // Do Enrichment if result Exists ... if (!result.IsNullOrDefault()) { // await enricher.EnrichRangeAsync( sources: result.Items, destinations: result.Items, fillMessages: fillMessages, cancellationToken: cancellationToken ); } // return result; } #endregion #endregion } }