mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-22 05:22:57 +01:00
CustomMethods in testing
This commit is contained in:
parent
0f7ecfb36c
commit
eed385253c
@ -4,9 +4,9 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Sieve.Services
|
namespace Sieve.Services
|
||||||
{
|
{
|
||||||
public interface ISieveCustomFilterMethods
|
//public interface ISieveCustomFilterMethods
|
||||||
{
|
//{
|
||||||
}
|
//}
|
||||||
|
|
||||||
public interface ISieveCustomFilterMethods<TEntity>
|
public interface ISieveCustomFilterMethods<TEntity>
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
|
@ -4,9 +4,9 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Sieve.Services
|
namespace Sieve.Services
|
||||||
{
|
{
|
||||||
public interface ISieveCustomSortMethods
|
//public interface ISieveCustomSortMethods
|
||||||
{
|
//{
|
||||||
}
|
//}
|
||||||
|
|
||||||
public interface ISieveCustomSortMethods<TEntity>
|
public interface ISieveCustomSortMethods<TEntity>
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
|
@ -43,6 +43,20 @@ namespace Sieve.Services
|
|||||||
_customFilterMethods = customFilterMethods;
|
_customFilterMethods = customFilterMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SieveProcessor(IOptions<SieveOptions> options,
|
||||||
|
ISieveCustomSortMethods<TEntity> customSortMethods)
|
||||||
|
{
|
||||||
|
_options = options;
|
||||||
|
_customSortMethods = customSortMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SieveProcessor(IOptions<SieveOptions> options,
|
||||||
|
ISieveCustomFilterMethods<TEntity> customFilterMethods)
|
||||||
|
{
|
||||||
|
_options = options;
|
||||||
|
_customFilterMethods = customFilterMethods;
|
||||||
|
}
|
||||||
|
|
||||||
public SieveProcessor(IOptions<SieveOptions> options)
|
public SieveProcessor(IOptions<SieveOptions> options)
|
||||||
{
|
{
|
||||||
_options = options;
|
_options = options;
|
||||||
@ -84,8 +98,9 @@ namespace Sieve.Services
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = ApplyCustomMethod(result, sortTerm.Name, _customSortMethods,
|
result = ApplyCustomMethod(result, sortTerm.Name, _customSortMethods,
|
||||||
includeUseThenBy: true,
|
isSorting: true,
|
||||||
useThenBy: useThenBy);
|
useThenBy: useThenBy,
|
||||||
|
desc: sortTerm.Descending);
|
||||||
}
|
}
|
||||||
useThenBy = true;
|
useThenBy = true;
|
||||||
}
|
}
|
||||||
@ -186,14 +201,14 @@ namespace Sieve.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<TEntity> ApplyCustomMethod(IQueryable<TEntity> result, string name, object parent,
|
private IQueryable<TEntity> ApplyCustomMethod(IQueryable<TEntity> result, string name, object parent,
|
||||||
bool includeUseThenBy = false, bool useThenBy = false)
|
bool isSorting = false, bool useThenBy = false, bool desc = false)
|
||||||
{
|
{
|
||||||
var customMethod = parent?.GetType()
|
var customMethod = parent?.GetType()
|
||||||
.GetMethod(name);
|
.GetMethod(name);
|
||||||
|
|
||||||
if (customMethod != null)
|
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)
|
result = customMethod.Invoke(parent, parameters)
|
||||||
as IQueryable<TEntity>;
|
as IQueryable<TEntity>;
|
||||||
}
|
}
|
||||||
|
20
SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs
Normal file
20
SieveTests/Services/SieveCustomFilterMethodsOfPosts.cs
Normal file
@ -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<Post>
|
||||||
|
{
|
||||||
|
public IQueryable<Post> IsNew(IQueryable<Post> source)
|
||||||
|
{
|
||||||
|
var result = source.Where(p => p.LikeCount < 100 &&
|
||||||
|
p.CommentCount < 5);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs
Normal file
23
SieveTests/Services/SieveCustomSortMethodsOfPosts - Copy.cs
Normal file
@ -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<Post>
|
||||||
|
{
|
||||||
|
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc)
|
||||||
|
{
|
||||||
|
var result = useThenBy ?
|
||||||
|
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
|
||||||
|
source.OrderBy(p => p.LikeCount)
|
||||||
|
.ThenBy(p => p.CommentCount)
|
||||||
|
.ThenBy(p => p.DateCreated);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@ using Microsoft.Extensions.Options;
|
|||||||
using Sieve.Models;
|
using Sieve.Models;
|
||||||
using Sieve.Services;
|
using Sieve.Services;
|
||||||
using SieveTests.Entities;
|
using SieveTests.Entities;
|
||||||
|
using SieveTests.Services;
|
||||||
|
|
||||||
namespace SieveTests
|
namespace SieveTests
|
||||||
{
|
{
|
||||||
@ -36,6 +37,8 @@ namespace SieveTests
|
|||||||
|
|
||||||
|
|
||||||
//services.AddScoped<ISieveProcessor, SieveProcessor>();
|
//services.AddScoped<ISieveProcessor, SieveProcessor>();
|
||||||
|
services.AddScoped<ISieveCustomSortMethods<Post>, SieveCustomSortMethodsOfPosts>();
|
||||||
|
services.AddScoped<ISieveCustomFilterMethods<Post>, SieveCustomFilterMethodsOfPosts>();
|
||||||
services.AddScoped<ISieveProcessor<Post>, SieveProcessor<Post>>();
|
services.AddScoped<ISieveProcessor<Post>, SieveProcessor<Post>>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user