diff --git a/README.md b/README.md index 35f24ba3..fcd92015 100644 --- a/README.md +++ b/README.md @@ -361,9 +361,10 @@ The QueryBuilder class is really useful if you want to manually build your query | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | | AddCondition | Adds a string base Filtering query | | AddOrderBy | Adds a string base Ordering query | -| AddPaging | Add and configure Page and PageSize | +| ConfigurePaging | Configure Page and PageSize | | AddQuery | Accepts a GridifyQuery object to configure filtering,ordering and paging | -| AddMapper | Accepts a GridifyMapper to use in build methods | +| UseCustomMapper | Accepts a GridifyMapper to use in build methods | +| UseEmptyMapper | Setup an Empty new GridifyMapper without auto generated mappings | | AddMap | Add a single Map to existing mapper | | RemoveMap | Remove a single Map from existing mapper | | ConfigureDefaultMapper | Configuring default mapper when we didn't use AddMapper method | @@ -380,7 +381,7 @@ usage eg: ```c# var builder = new QueryBuilder() .AddCondition("name=John") - .addOrderBy("age, id"); + .AddOrderBy("age, id"); var query = builder.build(persons); ``` diff --git a/src/Gridify.EntityFramework/Gridify.EntityFramework.csproj b/src/Gridify.EntityFramework/Gridify.EntityFramework.csproj index c7c24bdd..1e868f66 100644 --- a/src/Gridify.EntityFramework/Gridify.EntityFramework.csproj +++ b/src/Gridify.EntityFramework/Gridify.EntityFramework.csproj @@ -9,7 +9,7 @@ netstandard2.0 Gridify.EntityFramework - 2.4.0 + 2.4.1 Alireza Sabouri TuxTeam Gridify (EntityFramework), Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data. diff --git a/src/Gridify/Gridify.csproj b/src/Gridify/Gridify.csproj index 1365f3d5..feade085 100644 --- a/src/Gridify/Gridify.csproj +++ b/src/Gridify/Gridify.csproj @@ -3,7 +3,7 @@ netstandard2.0 Gridify - 2.4.0 + 2.4.1 Alireza Sabouri TuxTeam Gridify, Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data. @@ -12,7 +12,7 @@ latest true enable - + diff --git a/src/Gridify/IQueryBuilder.cs b/src/Gridify/IQueryBuilder.cs index 3a288390..4e95da1d 100644 --- a/src/Gridify/IQueryBuilder.cs +++ b/src/Gridify/IQueryBuilder.cs @@ -14,12 +14,46 @@ public interface IQueryBuilder /// /// /// returns IQueryBuilder - IQueryBuilder AddMapper(IGridifyMapper mapper); + IQueryBuilder UseCustomMapper(IGridifyMapper mapper); + + /// + /// Using this method the default gridify mapper has no predefined mappings and + /// you need to manually add your maps to the mapper using AddMap method. + /// mapper will be used to convert your provided string conditions to a lambda expression. + /// also when you use this method, previous mapper will be replaced, + /// so make sure to use this before AddMap method. + /// + /// optional mapper configuration + /// returns IQueryBuilder + IQueryBuilder UseEmptyMapper(GridifyMapperConfiguration mapperConfiguration); + + /// + IQueryBuilder UseEmptyMapper(Action mapperConfiguration); + + /// + /// Using this method you can add gridify supported string base filtering statements + /// + /// (Name=John,Age>10) + /// string based filtering + /// returns IQueryBuilder IQueryBuilder AddCondition(string condition); + + /// + /// Using this method you can use GridifyQuery to add only filtering part + /// + /// Accepts IGridifyFiltering so we can pass GridifyQuery object + /// returns IQueryBuilder IQueryBuilder AddCondition(IGridifyFiltering condition); + + /// + /// Using this method you can use GridifyQuery object to configure filtering, sorting and paging + /// + /// Accept IGridifyQuery so we can pass GridifyQuery object + /// returns IQueryBuilder IQueryBuilder AddQuery(IGridifyQuery gridifyQuery); + IQueryBuilder AddOrderBy(string orderBy); - IQueryBuilder AddPaging(int page, int pageSize); + IQueryBuilder ConfigurePaging(int page, int pageSize); IQueryBuilder ConfigureDefaultMapper(GridifyMapperConfiguration mapperConfiguration); IQueryBuilder ConfigureDefaultMapper(Action mapperConfiguration); IQueryBuilder AddMap(IGMap map, bool overwrite = true); diff --git a/src/Gridify/QueryBuilder.cs b/src/Gridify/QueryBuilder.cs index ddb01d48..92e56030 100644 --- a/src/Gridify/QueryBuilder.cs +++ b/src/Gridify/QueryBuilder.cs @@ -13,18 +13,36 @@ public class QueryBuilder : IQueryBuilder private string _orderBy = string.Empty; private (int page, int pageSize)? _paging; - public IQueryBuilder AddMapper(IGridifyMapper mapper) + /// + public IQueryBuilder UseCustomMapper(IGridifyMapper mapper) { _mapper = mapper; return this; } + /// + public IQueryBuilder UseEmptyMapper(GridifyMapperConfiguration? mapperConfiguration = null) + { + _mapper = mapperConfiguration != null ? new GridifyMapper(mapperConfiguration) : new GridifyMapper(); + return this; + } + + /// + public IQueryBuilder UseEmptyMapper(Action mapperConfiguration) + { + var mapperConfigurationInstance = new GridifyMapperConfiguration(); + mapperConfiguration(mapperConfigurationInstance); + return UseEmptyMapper(mapperConfigurationInstance); + } + + /// public IQueryBuilder AddCondition(string condition) { _conditions.Add(ConvertConditionToExpression(condition)); return this; } + /// public IQueryBuilder AddCondition(IGridifyFiltering condition) { if (condition.Filter != null) @@ -32,6 +50,7 @@ public IQueryBuilder AddCondition(IGridifyFiltering condition) return this; } + /// public IQueryBuilder AddQuery(IGridifyQuery gridifyQuery) { if (gridifyQuery.Filter != null) @@ -46,18 +65,21 @@ public IQueryBuilder AddQuery(IGridifyQuery gridifyQuery) return this; } + /// public IQueryBuilder AddOrderBy(string orderBy) { - _orderBy = orderBy; + _orderBy = string.IsNullOrEmpty(_orderBy) ? orderBy : $"{_orderBy}, {orderBy}"; return this; } - public IQueryBuilder AddPaging(int page, int pageSize) + /// + public IQueryBuilder ConfigurePaging(int page, int pageSize) { _paging = (page, pageSize); return this; } + /// public IQueryBuilder ConfigureDefaultMapper(GridifyMapperConfiguration mapperConfiguration) { if (_mapper != null && _mapper.GetCurrentMaps().Any()) @@ -65,11 +87,14 @@ public IQueryBuilder ConfigureDefaultMapper(GridifyMapperConfiguration mapper var tempMapper = new GridifyMapper(mapperConfiguration, true); _mapper.GetCurrentMaps().ToList().ForEach(map => tempMapper.AddMap(map)); _mapper = tempMapper; + return this; } + _mapper = new GridifyMapper(mapperConfiguration, true); return this; } + /// public IQueryBuilder ConfigureDefaultMapper(Action mapperConfiguration) { var mapperConfigurationInstance = new GridifyMapperConfiguration(); @@ -77,6 +102,7 @@ public IQueryBuilder ConfigureDefaultMapper(Action public IQueryBuilder AddMap(IGMap map, bool overwrite = true) { _mapper ??= new GridifyMapper(true); @@ -84,6 +110,7 @@ public IQueryBuilder AddMap(IGMap map, bool overwrite = true) return this; } + /// public IQueryBuilder AddMap(string from, Expression> to, Func? convertor = null, bool overwrite = true) { _mapper ??= new GridifyMapper(true); @@ -91,6 +118,7 @@ public IQueryBuilder AddMap(string from, Expression> to, Fun return this; } + /// public IQueryBuilder RemoveMap(IGMap map) { _mapper ??= new GridifyMapper(true); @@ -98,6 +126,7 @@ public IQueryBuilder RemoveMap(IGMap map) return this; } + /// public Expression> BuildFilteringExpression() { if (_conditions.Count == 0) @@ -107,15 +136,17 @@ public Expression> BuildFilteringExpression() => x is null ? y : x.And(y)) as Expression>)!; } + /// public IEnumerable>> BuildOrderingExpression() { if (string.IsNullOrEmpty(_orderBy)) throw new GridifyOrderingException("Please use 'AddOrderBy' to specify at least an single order"); - var gm = new GridifyQuery() { OrderBy = _orderBy }; + var gm = new GridifyQuery { OrderBy = _orderBy }; _mapper ??= new GridifyMapper(true); return gm.GetOrderingExpressions(_mapper); } + /// public Func, bool> BuildQueryableEvaluator() { return collection => @@ -126,6 +157,7 @@ public Func, bool> BuildQueryableEvaluator() }; } + /// public Func, bool> BuildCollectionEvaluator() { return collection => @@ -136,16 +168,19 @@ public Func, bool> BuildCollectionEvaluator() }; } + /// public bool Evaluate(IQueryable query) { return BuildQueryableEvaluator()(query); } + /// public bool Evaluate(IEnumerable collection) { return BuildCollectionEvaluator()(collection); } + /// public IQueryable Build(IQueryable context) { var query = context; @@ -162,6 +197,7 @@ public IQueryable Build(IQueryable context) return query; } + /// public IEnumerable Build(IEnumerable collection) { if (_conditions.Count > 0) @@ -176,18 +212,21 @@ public IEnumerable Build(IEnumerable collection) return collection; } + /// public Paging BuildWithPaging(IEnumerable collection) { var query = collection.AsQueryable(); return BuildWithPaging(query); } + /// public Paging BuildWithPaging(IQueryable collection) { var (count, query) = BuildWithQueryablePaging(collection); return new Paging(count, query); } + /// public QueryablePaging BuildWithQueryablePaging(IQueryable collection) { var query = collection;