2022-05-10 15:45:47 -07:00
|
|
|
using Core.Entities;
|
|
|
|
using Core.Interfaces;
|
|
|
|
using Core.Specifications;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace Infrastructure.Data
|
|
|
|
{
|
|
|
|
public class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity
|
|
|
|
{
|
|
|
|
private readonly StoreContext _context;
|
|
|
|
public GenericRepository(StoreContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<T> GetByIdAsync(int id)
|
|
|
|
{
|
|
|
|
return await _context.Set<T>().FindAsync(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<T>> ListAllAsync()
|
|
|
|
{
|
|
|
|
return await _context.Set<T>().ToListAsync();
|
|
|
|
}
|
|
|
|
public async Task<T> GetEntityWithSpec(ISpecification<T> spec)
|
|
|
|
{
|
|
|
|
return await ApplySpecification(spec).FirstOrDefaultAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec)
|
|
|
|
{
|
|
|
|
return await ApplySpecification(spec).ToListAsync();
|
|
|
|
}
|
2022-05-11 16:24:26 -07:00
|
|
|
public async Task<int> CountAsync(ISpecification<T> spec)
|
|
|
|
{
|
|
|
|
return await ApplySpecification(spec).CountAsync();
|
|
|
|
}
|
2022-05-10 15:45:47 -07:00
|
|
|
|
|
|
|
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
|
|
|
{
|
|
|
|
return SpecificationEvaluator<T>.GetQuery(_context.Set<T>().AsQueryable(), spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|