From eed385253c92c288828e13c7dd3c83dc667724b5 Mon Sep 17 00:00:00 2001 From: Biarity Date: Sat, 27 Jan 2018 16:37:38 +1000 Subject: [PATCH] CustomMethods in testing --- Sieve/Services/ISieveCustomFilterMethods.cs | 6 ++--- Sieve/Services/ISieveCustomSortMethods.cs | 6 ++--- Sieve/Services/SieveProcessor.cs | 23 +++++++++++++++---- .../SieveCustomFilterMethodsOfPosts.cs | 20 ++++++++++++++++ .../SieveCustomSortMethodsOfPosts - Copy.cs | 23 +++++++++++++++++++ SieveTests/Startup.cs | 3 +++ 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs create mode 100644 SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs diff --git a/Sieve/Services/ISieveCustomFilterMethods.cs b/Sieve/Services/ISieveCustomFilterMethods.cs index 067aa61..e57a1c0 100644 --- a/Sieve/Services/ISieveCustomFilterMethods.cs +++ b/Sieve/Services/ISieveCustomFilterMethods.cs @@ -4,9 +4,9 @@ using System.Text; namespace Sieve.Services { - public interface ISieveCustomFilterMethods - { - } + //public interface ISieveCustomFilterMethods + //{ + //} public interface ISieveCustomFilterMethods where TEntity : class diff --git a/Sieve/Services/ISieveCustomSortMethods.cs b/Sieve/Services/ISieveCustomSortMethods.cs index e10cdce..d72c8ab 100644 --- a/Sieve/Services/ISieveCustomSortMethods.cs +++ b/Sieve/Services/ISieveCustomSortMethods.cs @@ -4,9 +4,9 @@ using System.Text; namespace Sieve.Services { - public interface ISieveCustomSortMethods - { - } + //public interface ISieveCustomSortMethods + //{ + //} public interface ISieveCustomSortMethods where TEntity : class diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index f57ee3f..5370645 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -43,6 +43,20 @@ namespace Sieve.Services _customFilterMethods = customFilterMethods; } + public SieveProcessor(IOptions options, + ISieveCustomSortMethods customSortMethods) + { + _options = options; + _customSortMethods = customSortMethods; + } + + public SieveProcessor(IOptions options, + ISieveCustomFilterMethods customFilterMethods) + { + _options = options; + _customFilterMethods = customFilterMethods; + } + public SieveProcessor(IOptions options) { _options = options; @@ -84,8 +98,9 @@ namespace Sieve.Services else { result = ApplyCustomMethod(result, sortTerm.Name, _customSortMethods, - includeUseThenBy: true, - useThenBy: useThenBy); + isSorting: true, + useThenBy: useThenBy, + desc: sortTerm.Descending); } useThenBy = true; } @@ -186,14 +201,14 @@ namespace Sieve.Services } private IQueryable ApplyCustomMethod(IQueryable result, string name, object parent, - bool includeUseThenBy = false, bool useThenBy = false) + bool isSorting = false, bool useThenBy = false, bool desc = false) { var customMethod = parent?.GetType() .GetMethod(name); if (customMethod != null) { - var parameters = includeUseThenBy ? new object[] { result, useThenBy } : new object[] { result }; + var parameters = isSorting ? new object[] { result, useThenBy, desc } : new object[] { result }; result = customMethod.Invoke(parent, parameters) as IQueryable; } diff --git a/SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs b/SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs new file mode 100644 index 0000000..8f5573a --- /dev/null +++ b/SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs @@ -0,0 +1,20 @@ +using Sieve.Services; +using SieveTests.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SieveTests.Services +{ + public class SieveCustomFilterMethodsOfPosts : ISieveCustomFilterMethods + { + public IQueryable IsNew(IQueryable source) + { + var result = source.Where(p => p.LikeCount < 100 && + p.CommentCount < 5); + + return result; + } + } +} diff --git a/SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs b/SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs new file mode 100644 index 0000000..09c0ffb --- /dev/null +++ b/SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs @@ -0,0 +1,23 @@ +using Sieve.Services; +using SieveTests.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SieveTests.Services +{ + public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods + { + public IQueryable Popularity(IQueryable source, bool useThenBy, bool desc) + { + var result = useThenBy ? + ((IOrderedQueryable)source).ThenBy(p => p.LikeCount) : + source.OrderBy(p => p.LikeCount) + .ThenBy(p => p.CommentCount) + .ThenBy(p => p.DateCreated); + + return result; + } + } +} diff --git a/SieveTests/Startup.cs b/SieveTests/Startup.cs index fd35978..d6c5508 100644 --- a/SieveTests/Startup.cs +++ b/SieveTests/Startup.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Options; using Sieve.Models; using Sieve.Services; using SieveTests.Entities; +using SieveTests.Services; namespace SieveTests { @@ -36,6 +37,8 @@ namespace SieveTests //services.AddScoped(); + services.AddScoped, SieveCustomSortMethodsOfPosts>(); + services.AddScoped, SieveCustomFilterMethodsOfPosts>(); services.AddScoped, SieveProcessor>(); }