mirror of
				https://github.com/Biarity/Sieve.git
				synced 2025-11-03 18:29:02 +01:00 
			
		
		
		
	Better SieveModel, FilterTerm, and SortTerm generics
This commit is contained in:
		@@ -5,6 +5,8 @@ namespace Sieve.Models
 | 
			
		||||
{
 | 
			
		||||
    public class FilterTerm : IFilterTerm
 | 
			
		||||
    {
 | 
			
		||||
        public FilterTerm() { }
 | 
			
		||||
 | 
			
		||||
        private static readonly string[] Operators = new string[] {
 | 
			
		||||
                    "==*",
 | 
			
		||||
                    "@=*",
 | 
			
		||||
@@ -19,23 +21,27 @@ namespace Sieve.Models
 | 
			
		||||
                    "_="
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        public FilterTerm(string filter)
 | 
			
		||||
        public string Filter
 | 
			
		||||
        {
 | 
			
		||||
            var filterSplits = filter.Split(Operators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray();
 | 
			
		||||
            Names = filterSplits[0].Split('|').Select(t => t.Trim()).ToArray();
 | 
			
		||||
            Value = filterSplits.Length > 1 ? filterSplits[1] : null;
 | 
			
		||||
            Operator = Array.Find(Operators, o => filter.Contains(o)) ?? "==";
 | 
			
		||||
            OperatorParsed = GetOperatorParsed(Operator);
 | 
			
		||||
            OperatorIsCaseInsensitive = Operator.Contains("*");
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray();
 | 
			
		||||
                Names = filterSplits[0].Split('|').Select(t => t.Trim()).ToArray();
 | 
			
		||||
                Value = filterSplits.Length > 1 ? filterSplits[1] : null;
 | 
			
		||||
                Operator = Array.Find(Operators, o => value.Contains(o)) ?? "==";
 | 
			
		||||
                OperatorParsed = GetOperatorParsed(Operator);
 | 
			
		||||
                OperatorIsCaseInsensitive = Operator.Contains("*");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string[] Names { get; }
 | 
			
		||||
        public string[] Names { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public FilterOperator OperatorParsed { get; }
 | 
			
		||||
        public FilterOperator OperatorParsed { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public string Value { get; }
 | 
			
		||||
        public string Value { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public string Operator { get; }
 | 
			
		||||
        public string Operator { get; private set; }
 | 
			
		||||
 | 
			
		||||
        private FilterOperator GetOperatorParsed(string Operator)
 | 
			
		||||
        {
 | 
			
		||||
@@ -65,6 +71,6 @@ namespace Sieve.Models
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public bool OperatorIsCaseInsensitive { get; }
 | 
			
		||||
        public bool OperatorIsCaseInsensitive { get; private set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,11 @@
 | 
			
		||||
{
 | 
			
		||||
    public interface IFilterTerm
 | 
			
		||||
    {
 | 
			
		||||
        string Filter { set; }
 | 
			
		||||
        string[] Names { get; }
 | 
			
		||||
        string Operator { get; }
 | 
			
		||||
        bool OperatorIsCaseInsensitive { get; }
 | 
			
		||||
        FilterOperator OperatorParsed { get; }
 | 
			
		||||
        string Value { get; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,14 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Sieve.Models
 | 
			
		||||
{
 | 
			
		||||
	public interface ISieveModel<TFilterTerm, TSortTerm>
 | 
			
		||||
    public interface ISieveModel : ISieveModel<IFilterTerm, ISortTerm>
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public interface ISieveModel<TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm
 | 
			
		||||
        where TSortTerm : ISortTerm
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,8 @@
 | 
			
		||||
{
 | 
			
		||||
	public interface ISortTerm
 | 
			
		||||
    {
 | 
			
		||||
        string Sort { set; }
 | 
			
		||||
        bool Descending { get; }
 | 
			
		||||
        string Name { get; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,16 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.ComponentModel.DataAnnotations;
 | 
			
		||||
using System.Runtime.Serialization;
 | 
			
		||||
 | 
			
		||||
namespace Sieve.Models
 | 
			
		||||
{
 | 
			
		||||
    public class SieveModel : SieveModel<FilterTerm, SortTerm> { }
 | 
			
		||||
 | 
			
		||||
    [DataContract]
 | 
			
		||||
    public class SieveModel : ISieveModel<IFilterTerm, ISortTerm>
 | 
			
		||||
    public class SieveModel<TFilterTerm, TSortTerm> : ISieveModel<TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm, new()
 | 
			
		||||
        where TSortTerm : ISortTerm, new()
 | 
			
		||||
    {
 | 
			
		||||
        [DataMember]
 | 
			
		||||
        public string Filters { get; set; }
 | 
			
		||||
@@ -18,25 +23,33 @@ namespace Sieve.Models
 | 
			
		||||
 | 
			
		||||
        [DataMember, Range(1, int.MaxValue)]
 | 
			
		||||
        public int? PageSize { get; set; }
 | 
			
		||||
 | 
			
		||||
        public List<IFilterTerm> FiltersParsed
 | 
			
		||||
        
 | 
			
		||||
        public List<TFilterTerm> FiltersParsed
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                if (Filters != null)
 | 
			
		||||
                {
 | 
			
		||||
                    var value = new List<IFilterTerm>();
 | 
			
		||||
                    var value = new List<TFilterTerm>();
 | 
			
		||||
                    foreach (var filter in Filters.Split(','))
 | 
			
		||||
                    {
 | 
			
		||||
                        if (filter.StartsWith("("))
 | 
			
		||||
                        {
 | 
			
		||||
                            var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1);
 | 
			
		||||
                            var subfilters = filter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")", "");
 | 
			
		||||
                            value.Add(new FilterTerm(subfilters + filterOpAndVal));
 | 
			
		||||
                            var filterTerm = new TFilterTerm
 | 
			
		||||
                            {
 | 
			
		||||
                                Filter = subfilters + filterOpAndVal
 | 
			
		||||
                            };
 | 
			
		||||
                            value.Add(filterTerm);
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            value.Add(new FilterTerm(filter));
 | 
			
		||||
                            var filterTerm = new TFilterTerm
 | 
			
		||||
                            {
 | 
			
		||||
                                Filter = filter
 | 
			
		||||
                            };
 | 
			
		||||
                            value.Add(filterTerm);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    return value;
 | 
			
		||||
@@ -48,16 +61,20 @@ namespace Sieve.Models
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<ISortTerm> SortsParsed
 | 
			
		||||
        public List<TSortTerm> SortsParsed
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                if (Sorts != null)
 | 
			
		||||
                {
 | 
			
		||||
                    var value = new List<ISortTerm>();
 | 
			
		||||
                    var value = new List<TSortTerm>();
 | 
			
		||||
                    foreach (var sort in Sorts.Split(','))
 | 
			
		||||
                    {
 | 
			
		||||
                        value.Add(new SortTerm(sort));
 | 
			
		||||
                        var sortTerm = new TSortTerm()
 | 
			
		||||
                        {
 | 
			
		||||
                            Sort = sort
 | 
			
		||||
                        };
 | 
			
		||||
                        value.Add(sortTerm);
 | 
			
		||||
                    }
 | 
			
		||||
                    return value;
 | 
			
		||||
                }
 | 
			
		||||
@@ -65,6 +82,7 @@ namespace Sieve.Models
 | 
			
		||||
                {
 | 
			
		||||
                    return null;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -2,15 +2,20 @@
 | 
			
		||||
{
 | 
			
		||||
    public class SortTerm : ISortTerm
 | 
			
		||||
    {
 | 
			
		||||
        private readonly string _sort;
 | 
			
		||||
        public SortTerm() { }
 | 
			
		||||
 | 
			
		||||
        public SortTerm(string sort)
 | 
			
		||||
        private string _sort;
 | 
			
		||||
        
 | 
			
		||||
        public string Sort
 | 
			
		||||
        {
 | 
			
		||||
            _sort = sort;
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                _sort = value;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Name => (_sort.StartsWith("-")) ? _sort.Substring(1) : _sort;
 | 
			
		||||
 | 
			
		||||
        public bool Descending => _sort.StartsWith("-");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,15 +3,22 @@ using Sieve.Models;
 | 
			
		||||
 | 
			
		||||
namespace Sieve.Services
 | 
			
		||||
{
 | 
			
		||||
    public interface ISieveProcessor : ISieveProcessor<ISieveModel<IFilterTerm, ISortTerm>, IFilterTerm, ISortTerm>
 | 
			
		||||
    public interface ISieveProcessor : ISieveProcessor<SieveModel, FilterTerm, SortTerm>
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public interface ISieveProcessor<TFilterTerm, TSortTerm> : ISieveProcessor<SieveModel<TFilterTerm, TSortTerm>, TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm, new()
 | 
			
		||||
        where TSortTerm : ISortTerm, new()
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public interface ISieveProcessor<TSieveModel, TFilterTerm, TSortTerm>
 | 
			
		||||
        where TSieveModel : class, ISieveModel<TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm
 | 
			
		||||
        where TSortTerm : ISortTerm
 | 
			
		||||
        where TFilterTerm : IFilterTerm, new()
 | 
			
		||||
        where TSortTerm : ISortTerm, new()
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        IQueryable<TEntity> Apply<TEntity>(
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,28 @@ using Sieve.Models;
 | 
			
		||||
 | 
			
		||||
namespace Sieve.Services
 | 
			
		||||
{
 | 
			
		||||
    public class SieveProcessor : SieveProcessor<ISieveModel<IFilterTerm, ISortTerm>, IFilterTerm, ISortTerm>, ISieveProcessor
 | 
			
		||||
    public class SieveProcessor : SieveProcessor<SieveModel, FilterTerm, SortTerm>, ISieveProcessor
 | 
			
		||||
    {
 | 
			
		||||
        public SieveProcessor(IOptions<SieveOptions> options) : base(options)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public SieveProcessor(IOptions<SieveOptions> options, ISieveCustomSortMethods customSortMethods) : base(options, customSortMethods)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public SieveProcessor(IOptions<SieveOptions> options, ISieveCustomFilterMethods customFilterMethods) : base(options, customFilterMethods)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public SieveProcessor(IOptions<SieveOptions> options, ISieveCustomSortMethods customSortMethods, ISieveCustomFilterMethods customFilterMethods) : base(options, customSortMethods, customFilterMethods)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class SieveProcessor<TFilterTerm, TSortTerm> : SieveProcessor<SieveModel<TFilterTerm, TSortTerm>, TFilterTerm, TSortTerm>, ISieveProcessor<TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm, new()
 | 
			
		||||
        where TSortTerm : ISortTerm, new()
 | 
			
		||||
    {
 | 
			
		||||
        public SieveProcessor(IOptions<SieveOptions> options) : base(options)
 | 
			
		||||
        {
 | 
			
		||||
@@ -32,8 +53,8 @@ namespace Sieve.Services
 | 
			
		||||
 | 
			
		||||
    public class SieveProcessor<TSieveModel, TFilterTerm, TSortTerm> : ISieveProcessor<TSieveModel, TFilterTerm, TSortTerm>
 | 
			
		||||
        where TSieveModel : class, ISieveModel<TFilterTerm, TSortTerm>
 | 
			
		||||
        where TFilterTerm : IFilterTerm
 | 
			
		||||
        where TSortTerm : ISortTerm
 | 
			
		||||
        where TFilterTerm : IFilterTerm, new()
 | 
			
		||||
        where TSortTerm : ISortTerm, new()
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOptions<SieveOptions> _options;
 | 
			
		||||
        private readonly ISieveCustomSortMethods _customSortMethods;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user