diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index 5cfbe87..cd211f5 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -22,6 +22,7 @@ + diff --git a/PyroFetes/Repositories/PyrofetesRepository.cs b/PyroFetes/Repositories/PyrofetesRepository.cs index 0916abd..0f09bd3 100644 --- a/PyroFetes/Repositories/PyrofetesRepository.cs +++ b/PyroFetes/Repositories/PyrofetesRepository.cs @@ -1,8 +1,328 @@ +using System.Linq.Expressions; +using Ardalis.Specification; using Ardalis.Specification.EntityFrameworkCore; +using AutoMapper.QueryableExtensions; +using Microsoft.EntityFrameworkCore; +using Plainquire.Page; namespace PyroFetes.Repositories; -public abstract class PyrofetesRepository(PyroFetesDbContext databaseContext, AutoMapper.IMapper mapper) : RepositoryBase(databaseContext) where T : class +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); + } +} \ No newline at end of file