Skip to content

Commit

Permalink
Additional filtering by any type
Browse files Browse the repository at this point in the history
- Added support for filtering models by using any additional filter
type.
  • Loading branch information
Muchiachio committed Dec 10, 2015
1 parent 9d26008 commit 8f2a15d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
13 changes: 5 additions & 8 deletions src/Mvc.Lookup.Core/GenericLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected virtual IQueryable<T> FilterById(IQueryable<T> models)
protected virtual IQueryable<T> FilterByAdditionalFilters(IQueryable<T> models)
{
foreach (KeyValuePair<String, Object> filter in CurrentFilter.AdditionalFilters.Where(item => item.Value != null))
models = models.Where(FormEqualsQuery(GetType(filter.Key), filter.Key), filter.Value);
models = models.Where(FormEqualsQuery(filter.Key), filter.Value);

return models;
}
Expand Down Expand Up @@ -185,13 +185,6 @@ private String FormContainsQuery(String propertyName)
{
return $"({FormNotNullQuery(propertyName)} && {propertyName}.ToLower().Contains(@0))";
}
private String FormEqualsQuery(Type type, String propertyName)
{
if (type == typeof(String) || IsNumeric(type))
return String.Format(@"({0} && {1} == @0)", FormNotNullQuery(propertyName), propertyName);

throw new LookupException($"'{type.Name}' type is not supported in dynamic filtering.");
}
private String FormNotNullQuery(String propertyName)
{
List<String> queries = new List<String>();
Expand All @@ -202,6 +195,10 @@ private String FormNotNullQuery(String propertyName)

return String.Join(" && ", queries);
}
private String FormEqualsQuery(String propertyName)
{
return String.Format(@"({0} && {1} == @0)", FormNotNullQuery(propertyName), propertyName);
}

private void AddColumn(Dictionary<String, String> row, String column, T model)
{
Expand Down
31 changes: 3 additions & 28 deletions test/Mvc.Lookup.Tests/Unit/GenericLookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,40 +401,15 @@ public void FilterByAdditionalFilters_SkipsNullValues()
Assert.Equal(expected, actual);
}

[Fact]
public void FilterByAdditionalFilters_NoProperty_Throws()
{
lookup.CurrentFilter.AdditionalFilters.Add("Test", "Value");

LookupException exception = Assert.Throws<LookupException>(() => lookup.BaseFilterByAdditionalFilters(lookup.BaseGetModels()));

String expected = String.Format("'{0}' type does not have property named 'Test'.", typeof(TestModel).Name);
String actual = exception.Message;

Assert.Equal(expected, actual);
}

[Fact]
public void FilterByAdditionalFilters_Filters()
{
lookup.CurrentFilter.AdditionalFilters.Add("Id", "9");
lookup.CurrentFilter.AdditionalFilters.Add("Number", 9);
lookup.CurrentFilter.AdditionalFilters.Add("CreationDate", DateTime.Now.Date.AddDays(9));

IQueryable<TestModel> actual = lookup.BaseFilterByAdditionalFilters(lookup.BaseGetModels());
IQueryable<TestModel> expected = lookup.BaseGetModels().Where(model => model.Id == "9" && model.Number == 9);

Assert.Equal(expected, actual);
}

[Fact]
public void FilterByAdditionalFilters_NotSupportedType_Throws()
{
lookup.CurrentFilter.AdditionalFilters.Add("CreationDate", DateTime.Now);

LookupException exception = Assert.Throws<LookupException>(() => lookup.BaseFilterByAdditionalFilters(lookup.BaseGetModels()));

String expected = String.Format("'DateTime' type is not supported in dynamic filtering.", typeof(DateTime).Name);
String actual = exception.Message;
IEnumerable<TestModel> actual = lookup.BaseFilterByAdditionalFilters(lookup.BaseGetModels());
IEnumerable<TestModel> expected = lookup.BaseGetModels().ToArray().Where(model => model.Id == "9" && model.Number == 9 && model.CreationDate == DateTime.Now.Date.AddDays(9));

Assert.Equal(expected, actual);
}
Expand Down

0 comments on commit 8f2a15d

Please sign in to comment.