From c81826752671ea345def95d85124a7f20f64915e Mon Sep 17 00:00:00 2001 From: Biarity Date: Fri, 30 Nov 2018 09:08:39 +1000 Subject: [PATCH] Document & test multiple values for 2.2.0. Closes #46 --- README.md | 11 ++++++++++- Sieve/Sieve.csproj | 4 ++-- SieveTests/Services/SieveCustomFilterMethods.cs | 2 +- SieveUnitTests/General.cs | 14 ++++++++++++++ .../Services/SieveCustomFilterMethods.cs | 13 ++++++++++--- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index af2c87c..b8b2859 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ And `SieveCustomFilterMethods`: ```C# public class SieveCustomFilterMethods : ISieveCustomFilterMethods { - public IQueryable IsNew(IQueryable source, string op, string value) // The method is given the {Operator} & {Value} + public IQueryable IsNew(IQueryable source, string op, string[] values) // The method is given the {Operator} & {Value} { var result = source.Where(p => p.LikeCount < 100 && p.CommentCount < 5); @@ -181,6 +181,15 @@ It is recommended that you write exception-handling middleware to globally handl ### Example project You can find an example project incorporating most Sieve concepts in [SieveTests](https://github.com/Biarity/Sieve/tree/master/SieveTests). + +## Upgrading to v2.2.0 + +2.2.0 introduced OR logic for filter values. This means your custom filters will need to accept multiple values rather than just the one. + +* In all your custom filter methods, change the last argument to be a `string[] values` instead of `string value` +* The first value can then be found to be `values[0]` rather than `value` +* Multiple values will be present if the client uses OR logic + ## Upgrading from v1.* to v2.* * Changes to the `SieveProcessor` API: diff --git a/Sieve/Sieve.csproj b/Sieve/Sieve.csproj index 2257fea..d090c4f 100644 --- a/Sieve/Sieve.csproj +++ b/Sieve/Sieve.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 2.1.5 + 2.2 Sieve is a simple, clean, and extensible framework for .NET Core that adds sorting, filtering, and pagination functionality out of the box. Most common use case would be for serving ASP.NET Core GET queries. Documentation available on GitHub: https://github.com/Biarity/Sieve/ Copyright 2018 @@ -10,7 +10,7 @@ https://github.com/Biarity/Sieve https://emojipedia-us.s3.amazonaws.com/thumbs/240/twitter/120/alembic_2697.png - Fix custom sort methods not working after 2.1 + Added OR logic for value fields, allowed escaping special characters true true Biarity diff --git a/SieveTests/Services/SieveCustomFilterMethods.cs b/SieveTests/Services/SieveCustomFilterMethods.cs index 0111402..b1df4c7 100644 --- a/SieveTests/Services/SieveCustomFilterMethods.cs +++ b/SieveTests/Services/SieveCustomFilterMethods.cs @@ -6,7 +6,7 @@ namespace SieveTests.Services { public class SieveCustomFilterMethods : ISieveCustomFilterMethods { - public IQueryable IsNew(IQueryable source, string op, string value) + public IQueryable IsNew(IQueryable source, string op, string[] values) => source.Where(p => p.LikeCount < 100 && p.CommentCount < 5); } } diff --git a/SieveUnitTests/General.cs b/SieveUnitTests/General.cs index be98853..eefaff6 100644 --- a/SieveUnitTests/General.cs +++ b/SieveUnitTests/General.cs @@ -172,6 +172,20 @@ namespace SieveUnitTests Assert.IsTrue(result.Count() == 3); } + [TestMethod] + public void CustomFiltersWithOperatorsWork() + { + var model = new SieveModel() + { + Filters = "HasInTitle==A", + }; + + var result = _processor.Apply(model, _posts); + + Assert.IsTrue(result.Any(p => p.Id == 0)); + Assert.IsTrue(result.Count() == 1); + } + [TestMethod] public void CustomFiltersMixedWithUsualWork1() { diff --git a/SieveUnitTests/Services/SieveCustomFilterMethods.cs b/SieveUnitTests/Services/SieveCustomFilterMethods.cs index aaac842..68a6132 100644 --- a/SieveUnitTests/Services/SieveCustomFilterMethods.cs +++ b/SieveUnitTests/Services/SieveCustomFilterMethods.cs @@ -7,21 +7,28 @@ namespace SieveUnitTests.Services { public class SieveCustomFilterMethods : ISieveCustomFilterMethods { - public IQueryable IsNew(IQueryable source, string op, string value) + public IQueryable IsNew(IQueryable source, string op, string[] values) { var result = source.Where(p => p.LikeCount < 100); return result; } - public IQueryable IsNew(IQueryable source, string op, string value) + public IQueryable HasInTitle(IQueryable source, string op, string[] values) + { + var result = source.Where(p => p.Title.Contains(values[0])); + + return result; + } + + public IQueryable IsNew(IQueryable source, string op, string[] values) { var result = source.Where(c => c.DateCreated > DateTimeOffset.UtcNow.AddDays(-2)); return result; } - public IQueryable TestComment(IQueryable source, string op, string value) + public IQueryable TestComment(IQueryable source, string op, string[] values) { return source; }