Configuration effective, added ISieveModel, better DSL

This commit is contained in:
Biarity 2018-01-27 21:49:24 +10:00
parent 5b68fe2d09
commit 0d94191bc8
7 changed files with 82 additions and 7 deletions

View File

@ -7,6 +7,7 @@ namespace Sieve.Models
public enum FilterOperator public enum FilterOperator
{ {
Equals, Equals,
NotEquals,
GreaterThan, GreaterThan,
LessThan, LessThan,
GreaterThanOrEqualTo, GreaterThanOrEqualTo,

View File

@ -8,6 +8,15 @@ namespace Sieve.Models
public class FilterTerm public class FilterTerm
{ {
private string _filter; private string _filter;
private string[] operators = new string[] {
"==",
"!=",
">",
"<",
">=",
"<=",
"@=",
"_=" };
public FilterTerm(string filter) public FilterTerm(string filter)
{ {
@ -18,7 +27,8 @@ namespace Sieve.Models
{ {
get get
{ {
return _filter.Split(' ')[0]; return _filter.Split(operators, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
} }
} }
@ -26,7 +36,15 @@ namespace Sieve.Models
{ {
get get
{ {
return _filter.Split(' ')[1]; foreach (var op in operators)
{
if (_filter.IndexOf(op) != -1)
{
return op;
}
}
return "";
} }
} }
@ -34,7 +52,8 @@ namespace Sieve.Models
public string Value { public string Value {
get get
{ {
return _filter.Split(' ')[2]; var tokens = _filter.Split(operators, StringSplitOptions.RemoveEmptyEntries);
return tokens.Length > 1 ? tokens[1].Trim() : null;
} }
} }
@ -47,6 +66,10 @@ namespace Sieve.Models
case "eq": case "eq":
case "==": case "==":
return FilterOperator.Equals; return FilterOperator.Equals;
case "notequals":
case "nq":
case "!=":
return FilterOperator.NotEquals;
case "lessthan": case "lessthan":
case "lt": case "lt":
case "<": case "<":

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{
public interface ISieveModel
{
string Filters { get; set; }
string Sorts { get; set; }
int? Page { get; set; }
int? PageSize { get; set; }
List<FilterTerm> FilterParsed { get; }
List<SortTerm> SortParsed { get; }
}
}

View File

@ -5,7 +5,7 @@ using System.Text;
namespace Sieve.Models namespace Sieve.Models
{ {
public class SieveModel public class SieveModel : ISieveModel
{ {
public string Filters { get; set; } public string Filters { get; set; }

View File

@ -6,5 +6,8 @@ namespace Sieve.Models
{ {
public class SieveOptions public class SieveOptions
{ {
public bool CaseSensitive { get; set; } = false;
public int DefaultPageSize { get; set; } = 0;
} }
} }

View File

@ -57,6 +57,27 @@ namespace Sieve.Services
_customFilterMethods = customFilterMethods; _customFilterMethods = customFilterMethods;
} }
//public SieveProcessor(ISieveCustomSortMethods<TEntity> customSortMethods,
// ISieveCustomFilterMethods<TEntity> customFilterMethods)
//{
// _options = new SieveOptions();
// _customSortMethods = customSortMethods;
// _customFilterMethods = customFilterMethods;
//}
//
//public SieveProcessor(ISieveCustomSortMethods<TEntity> customSortMethods)
//{
// _options = options;
// _customSortMethods = customSortMethods;
//}
//
//public SieveProcessor(ISieveCustomFilterMethods<TEntity> customFilterMethods)
//{
// _options = options;
// _customFilterMethods = customFilterMethods;
//}
public SieveProcessor(IOptions<SieveOptions> options) public SieveProcessor(IOptions<SieveOptions> options)
{ {
_options = options; _options = options;
@ -180,6 +201,9 @@ namespace Sieve.Services
public IQueryable<TEntity> ApplyPagination(SieveModel model, IQueryable<TEntity> result) public IQueryable<TEntity> ApplyPagination(SieveModel model, IQueryable<TEntity> result)
{ {
if (model?.Page == null || model?.PageSize == null) if (model?.Page == null || model?.PageSize == null)
if (_options.Value.DefaultPageSize > 0)
return result.Take(_options.Value.DefaultPageSize);
else
return result; return result;
result = result.Skip((model.Page.Value - 1) * model.PageSize.Value) result = result.Skip((model.Page.Value - 1) * model.PageSize.Value)
@ -194,7 +218,8 @@ namespace Sieve.Services
if (p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute) if (p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute)
if ((canSortRequired ? sieveAttribute.CanSort : true) && if ((canSortRequired ? sieveAttribute.CanSort : true) &&
(canFilterRequired ? sieveAttribute.CanFilter : true) && (canFilterRequired ? sieveAttribute.CanFilter : true) &&
((sieveAttribute.Name ?? p.Name) == name)) ((sieveAttribute.Name ?? p.Name).Equals(name,
_options.Value.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)))
return true; return true;
return false; return false;
}); });

View File

@ -2,8 +2,9 @@
"ConnectionStrings": { "ConnectionStrings": {
"TestSqlServer": "Server=(localdb)\\MSSQLLocalDB; Database=SieveTests;" "TestSqlServer": "Server=(localdb)\\MSSQLLocalDB; Database=SieveTests;"
}, },
"SieveConfig": { "Sieve": {
"CaseSensitive": false,
"DefaultPageSize": 10
}, },
"Logging": { "Logging": {
"IncludeScopes": false, "IncludeScopes": false,