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
{
Equals,
NotEquals,
GreaterThan,
LessThan,
GreaterThanOrEqualTo,

View File

@ -8,6 +8,15 @@ namespace Sieve.Models
public class FilterTerm
{
private string _filter;
private string[] operators = new string[] {
"==",
"!=",
">",
"<",
">=",
"<=",
"@=",
"_=" };
public FilterTerm(string filter)
{
@ -18,7 +27,8 @@ namespace Sieve.Models
{
get
{
return _filter.Split(' ')[0];
return _filter.Split(operators, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
}
}
@ -26,7 +36,15 @@ namespace Sieve.Models
{
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 {
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 "==":
return FilterOperator.Equals;
case "notequals":
case "nq":
case "!=":
return FilterOperator.NotEquals;
case "lessthan":
case "lt":
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
{
public class SieveModel
public class SieveModel : ISieveModel
{
public string Filters { get; set; }

View File

@ -6,5 +6,8 @@ namespace Sieve.Models
{
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;
}
//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)
{
_options = options;
@ -180,6 +201,9 @@ namespace Sieve.Services
public IQueryable<TEntity> ApplyPagination(SieveModel model, IQueryable<TEntity> result)
{
if (model?.Page == null || model?.PageSize == null)
if (_options.Value.DefaultPageSize > 0)
return result.Take(_options.Value.DefaultPageSize);
else
return result;
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 ((canSortRequired ? sieveAttribute.CanSort : 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 false;
});

View File

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