using System.Linq.Expressions; using Ardalis.Specification; using Ardalis.Specification.EntityFrameworkCore; using AutoMapper.QueryableExtensions; using Microsoft.EntityFrameworkCore; using Plainquire.Page; namespace PyroFetes.Repositories; public class PyrofetesRepository(DbContext databaseContext, AutoMapper.IMapper mapper) : RepositoryBase(databaseContext) where T : class { private readonly DbContext _databaseContext = databaseContext; /// /// /// /// /// public async Task FirstOrDefaultAsync(CancellationToken cancellationToken = default) { return await _databaseContext.Set().AsQueryable().FirstOrDefaultAsync(cancellationToken); } /// /// /// /// /// public async Task FirstAsync(CancellationToken cancellationToken = default) { return await _databaseContext.Set().AsQueryable().FirstAsync(cancellationToken); } /// /// /// /// /// /// public async Task SingleAsync(ISpecification specification, CancellationToken cancellationToken = default) { return await ApplySpecification(specification).SingleAsync(cancellationToken); } /// /// /// /// /// public async Task SingleAsync(CancellationToken cancellationToken = default) { return await _databaseContext.Set().AsQueryable().SingleAsync(cancellationToken); } /// /// /// /// /// /// /// public async Task SumAsync( ISpecification specification, Expression> selector, CancellationToken cancellationToken = default) { return await ApplySpecification(specification).SumAsync(selector, cancellationToken); } // /// // /// // /// // /// // /// // /// // /// // public async Task SumAsync( // ISpecification specification, // Expression> selector, // CancellationToken cancellationToken = default) // { // return await ApplySpecification(specification).SumAsync(selector, cancellationToken); // } /// /// /// /// /// /// /// /// public async Task ProjectToSingleOrDefaultAsync( ISpecification specification, CancellationToken cancellationToken = default) { return await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .SingleOrDefaultAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// /// public async Task ProjectToSingleOrDefaultAsync( ISpecification specification, Action postProcessor, CancellationToken cancellationToken = default) { TResult? result = await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .SingleOrDefaultAsync(cancellationToken: cancellationToken); postProcessor(result); return result; } // /// // /// // /// // /// // /// // /// // public async Task ProjectToSingleOrDefaultAsync( // CancellationToken cancellationToken = default) // { // return await _databaseContext.Set().AsQueryable().ProjectTo(mapper.ConfigurationProvider).SingleOrDefaultAsync(cancellationToken: cancellationToken); // } /// /// /// /// /// /// /// public async Task ProjectToSingleAsync( ISpecification specification, CancellationToken cancellationToken = default) { return await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .SingleAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// /// public async Task ProjectToSingleAsync( ISpecification specification, Action postProcessor, CancellationToken cancellationToken = default) { TResult result = await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .SingleAsync(cancellationToken: cancellationToken); postProcessor(result); return result; } /// /// /// /// /// /// public async Task ProjectToSingleAsync( CancellationToken cancellationToken = default) { return await _databaseContext.Set() .AsQueryable() .ProjectTo(mapper.ConfigurationProvider) .SingleAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// public async Task> ProjectToListAsync( CancellationToken cancellationToken = default) { return await _databaseContext.Set() .AsQueryable() .ProjectTo(mapper.ConfigurationProvider) .ToListAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// public async Task> ProjectToListAsync( Action> postProcessor, CancellationToken cancellationToken = default) { List results = await _databaseContext.Set() .AsQueryable() .ProjectTo(mapper.ConfigurationProvider) .ToListAsync(cancellationToken: cancellationToken); postProcessor(results); return results; } /// /// /// /// /// /// /// /// public async Task> ProjectToListAsync( ISpecification specification, Action> postProcessor, CancellationToken cancellationToken = default) { List results = await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .ToListAsync(cancellationToken: cancellationToken); postProcessor(results); return results; } /// /// /// /// /// /// /// public async Task> ProjectToListAsync( ISpecification specification, CancellationToken cancellationToken = default) { return await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .ToListAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// /// /// public async Task> ProjectToListAsync( ISpecification specification, EntityPage page, Action> postProcessor, CancellationToken cancellationToken = default) { List results = await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .Page(page) .ToListAsync(cancellationToken: cancellationToken); postProcessor(results); return results; } /// /// /// /// /// /// /// /// public async Task> ProjectToListAsync( ISpecification specification, EntityPage page, CancellationToken cancellationToken = default) { return await ApplySpecification(specification) .ProjectTo(mapper.ConfigurationProvider) .Page(page) .ToListAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// /// public async Task> SelectManyAsync( ISpecification specification, Expression>> selector, CancellationToken cancellationToken = default) { return await ApplySpecification(specification).SelectMany(selector).ToListAsync(cancellationToken: cancellationToken); } /// /// /// /// /// /// /// /// public async Task> SelectAsync( ISpecification specification, Expression> selector, CancellationToken cancellationToken = default) { return await ApplySpecification(specification).Select(selector).ToListAsync(cancellationToken: cancellationToken); } }