2022-05-10 15:45:47 -07:00
|
|
|
using Core.Entities;
|
|
|
|
using Core.Specifications;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace Infrastructure.Data
|
|
|
|
{
|
|
|
|
public class SpecificationEvaluator<TEntity> where TEntity : BaseEntity
|
|
|
|
{
|
|
|
|
public static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery, ISpecification<TEntity> spec)
|
|
|
|
{
|
|
|
|
var query = inputQuery;
|
|
|
|
|
|
|
|
if (spec.Criteria != null)
|
|
|
|
{
|
|
|
|
query = query.Where(spec.Criteria);
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:24:26 -07:00
|
|
|
if (spec.OrderBy != null)
|
|
|
|
{
|
|
|
|
query = query.OrderBy(spec.OrderBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec.OrderByDecending != null)
|
|
|
|
{
|
|
|
|
query = query.OrderByDescending(spec.OrderByDecending);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec.IsPagingEnabled)
|
|
|
|
{
|
|
|
|
query = query.Skip(spec.Skip).Take(spec.Take);
|
|
|
|
}
|
|
|
|
|
2022-05-10 15:45:47 -07:00
|
|
|
query = spec.Includes.Aggregate(query, (current, include) => current.Include(include));
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|