mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-22 13:32:33 +01:00
Version 2.0
This commit is contained in:
parent
c861ada8fa
commit
033a920bec
@ -6,9 +6,12 @@ namespace Sieve.Services
|
|||||||
{
|
{
|
||||||
public interface ISieveProcessor
|
public interface ISieveProcessor
|
||||||
{
|
{
|
||||||
IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> source, object[] dataForCustomMethods = null);
|
IQueryable<TEntity> Apply<TEntity>(
|
||||||
IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
|
ISieveModel<IFilterTerm, ISortTerm> model,
|
||||||
IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
|
IQueryable<TEntity> source,
|
||||||
IQueryable<TEntity> ApplyPagination<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result);
|
object[] dataForCustomMethods = null,
|
||||||
|
bool applyFiltering = true,
|
||||||
|
bool applySorting = true,
|
||||||
|
bool applyPagination = true);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -62,43 +62,58 @@ namespace Sieve.Services
|
|||||||
/// <param name="model">An instance of ISieveModel</param>
|
/// <param name="model">An instance of ISieveModel</param>
|
||||||
/// <param name="source">Data source</param>
|
/// <param name="source">Data source</param>
|
||||||
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
|
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
|
||||||
|
/// <param name="applyFiltering">Should the data be filtered? Defaults to true.</param>
|
||||||
|
/// <param name="applySorting">Should the data be sorted? Defaults to true.</param>
|
||||||
|
/// <param name="applyPagination">Should the data be paginated? Defaults to true.</param>
|
||||||
/// <returns>Returns a transformed version of `source`</returns>
|
/// <returns>Returns a transformed version of `source`</returns>
|
||||||
public IQueryable<TEntity> ApplyAll<TEntity>(
|
public IQueryable<TEntity> Apply<TEntity>(
|
||||||
ISieveModel<IFilterTerm, ISortTerm> model,
|
ISieveModel<IFilterTerm, ISortTerm> model,
|
||||||
IQueryable<TEntity> source,
|
IQueryable<TEntity> source,
|
||||||
object[] dataForCustomMethods = null)
|
object[] dataForCustomMethods = null,
|
||||||
|
bool applyFiltering = true,
|
||||||
|
bool applySorting = true,
|
||||||
|
bool applyPagination = true)
|
||||||
{
|
{
|
||||||
var result = source;
|
var result = source;
|
||||||
|
|
||||||
if (model == null)
|
if (model == null)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
// Filter
|
// Filter
|
||||||
|
if (applyFiltering)
|
||||||
result = ApplyFiltering(model, result, dataForCustomMethods);
|
result = ApplyFiltering(model, result, dataForCustomMethods);
|
||||||
|
|
||||||
// Sort
|
// Sort
|
||||||
|
if (applySorting)
|
||||||
result = ApplySorting(model, result, dataForCustomMethods);
|
result = ApplySorting(model, result, dataForCustomMethods);
|
||||||
|
|
||||||
// Paginate
|
// Paginate
|
||||||
|
if (applyPagination)
|
||||||
result = ApplyPagination(model, result);
|
result = ApplyPagination(model, result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (_options.Value.ThrowExceptions)
|
||||||
|
{
|
||||||
|
if (ex is SieveException)
|
||||||
|
throw;
|
||||||
|
throw new SieveException(ex.Message, ex);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
private IQueryable<TEntity> ApplyFiltering<TEntity>(
|
||||||
/// Apply filtering parameters found in `model` to `source`
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <param name="model">An instance of ISieveModel</param>
|
|
||||||
/// <param name="source">Data source</param>
|
|
||||||
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
|
|
||||||
/// <returns>Returns a transformed version of `source`</returns>
|
|
||||||
public IQueryable<TEntity> ApplyFiltering<TEntity>(
|
|
||||||
ISieveModel<IFilterTerm, ISortTerm> model,
|
ISieveModel<IFilterTerm, ISortTerm> model,
|
||||||
IQueryable<TEntity> result,
|
IQueryable<TEntity> result,
|
||||||
object[] dataForCustomMethods = null)
|
object[] dataForCustomMethods = null)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (model?.FiltersParsed == null)
|
if (model?.FiltersParsed == null)
|
||||||
return result;
|
return result;
|
||||||
@ -185,35 +200,11 @@ namespace Sieve.Services
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (_options.Value.ThrowExceptions)
|
|
||||||
{
|
|
||||||
if (ex is SieveException)
|
|
||||||
throw;
|
|
||||||
throw new SieveException(ex.Message, ex);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
private IQueryable<TEntity> ApplySorting<TEntity>(
|
||||||
/// Apply sorting parameters found in `model` to `source`
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <param name="model">An instance of ISieveModel</param>
|
|
||||||
/// <param name="source">Data source</param>
|
|
||||||
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
|
|
||||||
/// <returns>Returns a transformed version of `source`</returns>
|
|
||||||
public IQueryable<TEntity> ApplySorting<TEntity>(
|
|
||||||
ISieveModel<IFilterTerm, ISortTerm> model,
|
ISieveModel<IFilterTerm, ISortTerm> model,
|
||||||
IQueryable<TEntity> result,
|
IQueryable<TEntity> result,
|
||||||
object[] dataForCustomMethods = null)
|
object[] dataForCustomMethods = null)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (model?.SortsParsed == null)
|
if (model?.SortsParsed == null)
|
||||||
return result;
|
return result;
|
||||||
@ -242,34 +233,10 @@ namespace Sieve.Services
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (_options.Value.ThrowExceptions)
|
|
||||||
{
|
|
||||||
if (ex is SieveException)
|
|
||||||
throw;
|
|
||||||
throw new SieveException(ex.Message, ex);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
private IQueryable<TEntity> ApplyPagination<TEntity>(
|
||||||
/// Apply pagination parameters found in `model` to `source`
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <param name="model">An instance of ISieveModel</param>
|
|
||||||
/// <param name="source">Data source</param>
|
|
||||||
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
|
|
||||||
/// <returns>Returns a transformed version of `source`</returns>
|
|
||||||
public IQueryable<TEntity> ApplyPagination<TEntity>(
|
|
||||||
ISieveModel<IFilterTerm, ISortTerm> model,
|
ISieveModel<IFilterTerm, ISortTerm> model,
|
||||||
IQueryable<TEntity> result)
|
IQueryable<TEntity> result)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var page = model?.Page ?? 1;
|
var page = model?.Page ?? 1;
|
||||||
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
|
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;
|
||||||
@ -282,20 +249,6 @@ namespace Sieve.Services
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (_options.Value.ThrowExceptions)
|
|
||||||
{
|
|
||||||
if (ex is SieveException)
|
|
||||||
throw;
|
|
||||||
throw new SieveException(ex.Message, ex);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected virtual SievePropertyMapper MapProperties(SievePropertyMapper mapper)
|
protected virtual SievePropertyMapper MapProperties(SievePropertyMapper mapper)
|
||||||
|
@ -28,7 +28,7 @@ namespace SieveTests.Controllers
|
|||||||
{
|
{
|
||||||
var result = _dbContext.Posts.AsNoTracking();
|
var result = _dbContext.Posts.AsNoTracking();
|
||||||
|
|
||||||
result = _sieveProcessor.ApplyAll(sieveModel, result);
|
result = _sieveProcessor.Apply(sieveModel, result);
|
||||||
|
|
||||||
return Json(result.ToList());
|
return Json(result.ToList());
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "Title@=*a"
|
Filters = "Title@=*a"
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyFiltering(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.AreEqual(result.First().Id, 0);
|
Assert.AreEqual(result.First().Id, 0);
|
||||||
Assert.IsTrue(result.Count() == 1);
|
Assert.IsTrue(result.Count() == 1);
|
||||||
@ -72,7 +72,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "Title@=a",
|
Filters = "Title@=a",
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyFiltering(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.IsTrue(result.Count() == 0);
|
Assert.IsTrue(result.Count() == 0);
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "IsDraft==false"
|
Filters = "IsDraft==false"
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyAll(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.IsTrue(result.Count() == 2);
|
Assert.IsTrue(result.Count() == 2);
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ namespace SieveUnitTests
|
|||||||
Sorts = "-IsDraft"
|
Sorts = "-IsDraft"
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyAll(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.AreEqual(result.First().Id, 0);
|
Assert.AreEqual(result.First().Id, 0);
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ namespace SieveUnitTests
|
|||||||
Console.WriteLine(model.FiltersParsed.First().Operator);
|
Console.WriteLine(model.FiltersParsed.First().Operator);
|
||||||
Console.WriteLine(model.FiltersParsed.First().OperatorParsed);
|
Console.WriteLine(model.FiltersParsed.First().OperatorParsed);
|
||||||
|
|
||||||
var result = _processor.ApplyFiltering(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "Isnew",
|
Filters = "Isnew",
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyFiltering(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.IsFalse(result.Any(p => p.Id == 0));
|
Assert.IsFalse(result.Any(p => p.Id == 0));
|
||||||
Assert.IsTrue(result.Count() == 3);
|
Assert.IsTrue(result.Count() == 3);
|
||||||
@ -146,7 +146,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "does not exist",
|
Filters = "does not exist",
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.ApplyFiltering(model, _posts));
|
Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@ -157,7 +157,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "TestComment",
|
Filters = "TestComment",
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.ThrowsException<SieveIncompatibleMethodException>(() => _processor.ApplyFiltering(model, _posts));
|
Assert.ThrowsException<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@ -168,7 +168,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "(Title|LikeCount)==3",
|
Filters = "(Title|LikeCount)==3",
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyFiltering(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.AreEqual(result.First().Id, 3);
|
Assert.AreEqual(result.First().Id, 3);
|
||||||
Assert.IsTrue(result.Count() == 1);
|
Assert.IsTrue(result.Count() == 1);
|
||||||
|
@ -53,7 +53,7 @@ namespace SieveUnitTests
|
|||||||
Filters = "shortname@=A",
|
Filters = "shortname@=A",
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplyAll(model, _posts);
|
var result = _processor.Apply(model, _posts);
|
||||||
|
|
||||||
Assert.AreEqual(result.First().ThisHasNoAttributeButIsAccessible, "A");
|
Assert.AreEqual(result.First().ThisHasNoAttributeButIsAccessible, "A");
|
||||||
|
|
||||||
@ -69,9 +69,9 @@ namespace SieveUnitTests
|
|||||||
Sorts = "OnlySortableViaFluentApi"
|
Sorts = "OnlySortableViaFluentApi"
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = _processor.ApplySorting(model, _posts);
|
var result = _processor.Apply(model, _posts, applyFiltering: false, applyPagination: false);
|
||||||
|
|
||||||
Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.ApplyAll(model, _posts));
|
Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
|
||||||
|
|
||||||
Assert.AreEqual(result.First().Id, 3);
|
Assert.AreEqual(result.First().Id, 3);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user