Skip to content

Commit

Permalink
v2.4.1 improve builder APIs, fix few bugs (#41)
Browse files Browse the repository at this point in the history
* Allow multiple AddOrderBy usage

* update QueryBuilder APIs

* fix builder example usage

* add summary comments and UseEmptyMapper to builder

* fix UseEmptyMapper bug

* cleanup builder class

* update to v2.4.1
  • Loading branch information
alirezanet authored Nov 18, 2021
1 parent c8d5c14 commit d137e58
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -380,7 +381,7 @@ usage eg:
```c#
var builder = new QueryBuilder<Person>()
.AddCondition("name=John")
.addOrderBy("age, id");
.AddOrderBy("age, id");

var query = builder.build(persons);
```
Expand Down
2 changes: 1 addition & 1 deletion src/Gridify.EntityFramework/Gridify.EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Gridify.EntityFramework</PackageId>
<Version>2.4.0</Version>
<Version>2.4.1</Version>
<Authors>Alireza Sabouri</Authors>
<Company>TuxTeam</Company>
<PackageDescription>Gridify (EntityFramework), Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.</PackageDescription>
Expand Down
4 changes: 2 additions & 2 deletions src/Gridify/Gridify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Gridify</PackageId>
<Version>2.4.0</Version>
<Version>2.4.1</Version>
<Authors>Alireza Sabouri</Authors>
<Company>TuxTeam</Company>
<PackageDescription>Gridify, Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.</PackageDescription>
Expand All @@ -12,7 +12,7 @@
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Nullable>enable</Nullable>

<!-- <GeneratePackageOnBuild>true</GeneratePackageOnBuild> -->
</PropertyGroup>

Expand Down
38 changes: 36 additions & 2 deletions src/Gridify/IQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,46 @@ public interface IQueryBuilder<T>
/// </summary>
/// <param name="mapper"></param>
/// <returns>returns IQueryBuilder</returns>
IQueryBuilder<T> AddMapper(IGridifyMapper<T> mapper);
IQueryBuilder<T> UseCustomMapper(IGridifyMapper<T> mapper);

/// <summary>
/// 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.
/// </summary>
/// <param name="mapperConfiguration">optional mapper configuration</param>
/// <returns>returns IQueryBuilder</returns>
IQueryBuilder<T> UseEmptyMapper(GridifyMapperConfiguration mapperConfiguration);

/// <inheritdoc cref="UseEmptyMapper(Gridify.GridifyMapperConfiguration)" />
IQueryBuilder<T> UseEmptyMapper(Action<GridifyMapperConfiguration> mapperConfiguration);

/// <summary>
/// Using this method you can add gridify supported string base filtering statements
/// </summary>
/// <example> (Name=John,Age>10) </example>
/// <param name="condition">string based filtering</param>
/// <returns>returns IQueryBuilder</returns>
IQueryBuilder<T> AddCondition(string condition);

/// <summary>
/// Using this method you can use GridifyQuery to add only filtering part
/// </summary>
/// <param name="condition">Accepts IGridifyFiltering so we can pass GridifyQuery object</param>
/// <returns>returns IQueryBuilder</returns>
IQueryBuilder<T> AddCondition(IGridifyFiltering condition);

/// <summary>
/// Using this method you can use GridifyQuery object to configure filtering, sorting and paging
/// </summary>
/// <param name="gridifyQuery">Accept IGridifyQuery so we can pass GridifyQuery object</param>
/// <returns>returns IQueryBuilder</returns>
IQueryBuilder<T> AddQuery(IGridifyQuery gridifyQuery);

IQueryBuilder<T> AddOrderBy(string orderBy);
IQueryBuilder<T> AddPaging(int page, int pageSize);
IQueryBuilder<T> ConfigurePaging(int page, int pageSize);
IQueryBuilder<T> ConfigureDefaultMapper(GridifyMapperConfiguration mapperConfiguration);
IQueryBuilder<T> ConfigureDefaultMapper(Action<GridifyMapperConfiguration> mapperConfiguration);
IQueryBuilder<T> AddMap(IGMap<T> map, bool overwrite = true);
Expand Down
47 changes: 43 additions & 4 deletions src/Gridify/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,44 @@ public class QueryBuilder<T> : IQueryBuilder<T>
private string _orderBy = string.Empty;
private (int page, int pageSize)? _paging;

public IQueryBuilder<T> AddMapper(IGridifyMapper<T> mapper)
/// <inheritdoc />
public IQueryBuilder<T> UseCustomMapper(IGridifyMapper<T> mapper)
{
_mapper = mapper;
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> UseEmptyMapper(GridifyMapperConfiguration? mapperConfiguration = null)
{
_mapper = mapperConfiguration != null ? new GridifyMapper<T>(mapperConfiguration) : new GridifyMapper<T>();
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> UseEmptyMapper(Action<GridifyMapperConfiguration> mapperConfiguration)
{
var mapperConfigurationInstance = new GridifyMapperConfiguration();
mapperConfiguration(mapperConfigurationInstance);
return UseEmptyMapper(mapperConfigurationInstance);
}

/// <inheritdoc />
public IQueryBuilder<T> AddCondition(string condition)
{
_conditions.Add(ConvertConditionToExpression(condition));
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> AddCondition(IGridifyFiltering condition)
{
if (condition.Filter != null)
_conditions.Add(ConvertConditionToExpression(condition.Filter));
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> AddQuery(IGridifyQuery gridifyQuery)
{
if (gridifyQuery.Filter != null)
Expand All @@ -46,58 +65,68 @@ public IQueryBuilder<T> AddQuery(IGridifyQuery gridifyQuery)
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> AddOrderBy(string orderBy)
{
_orderBy = orderBy;
_orderBy = string.IsNullOrEmpty(_orderBy) ? orderBy : $"{_orderBy}, {orderBy}";
return this;
}

public IQueryBuilder<T> AddPaging(int page, int pageSize)
/// <inheritdoc />
public IQueryBuilder<T> ConfigurePaging(int page, int pageSize)
{
_paging = (page, pageSize);
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> ConfigureDefaultMapper(GridifyMapperConfiguration mapperConfiguration)
{
if (_mapper != null && _mapper.GetCurrentMaps().Any())
{
var tempMapper = new GridifyMapper<T>(mapperConfiguration, true);
_mapper.GetCurrentMaps().ToList().ForEach(map => tempMapper.AddMap(map));
_mapper = tempMapper;
return this;
}

_mapper = new GridifyMapper<T>(mapperConfiguration, true);
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> ConfigureDefaultMapper(Action<GridifyMapperConfiguration> mapperConfiguration)
{
var mapperConfigurationInstance = new GridifyMapperConfiguration();
mapperConfiguration(mapperConfigurationInstance);
return ConfigureDefaultMapper(mapperConfigurationInstance);
}

/// <inheritdoc />
public IQueryBuilder<T> AddMap(IGMap<T> map, bool overwrite = true)
{
_mapper ??= new GridifyMapper<T>(true);
_mapper.AddMap(map, overwrite);
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> AddMap(string from, Expression<Func<T, object?>> to, Func<string, object>? convertor = null, bool overwrite = true)
{
_mapper ??= new GridifyMapper<T>(true);
_mapper.AddMap(from, to, convertor, overwrite);
return this;
}

/// <inheritdoc />
public IQueryBuilder<T> RemoveMap(IGMap<T> map)
{
_mapper ??= new GridifyMapper<T>(true);
_mapper.RemoveMap(map);
return this;
}

/// <inheritdoc />
public Expression<Func<T, bool>> BuildFilteringExpression()
{
if (_conditions.Count == 0)
Expand All @@ -107,15 +136,17 @@ public Expression<Func<T, bool>> BuildFilteringExpression()
=> x is null ? y : x.And(y)) as Expression<Func<T, bool>>)!;
}

/// <inheritdoc />
public IEnumerable<Expression<Func<T, object>>> 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<T>(true);
return gm.GetOrderingExpressions(_mapper);
}

/// <inheritdoc />
public Func<IQueryable<T>, bool> BuildQueryableEvaluator()
{
return collection =>
Expand All @@ -126,6 +157,7 @@ public Func<IQueryable<T>, bool> BuildQueryableEvaluator()
};
}

/// <inheritdoc />
public Func<IEnumerable<T>, bool> BuildCollectionEvaluator()
{
return collection =>
Expand All @@ -136,16 +168,19 @@ public Func<IEnumerable<T>, bool> BuildCollectionEvaluator()
};
}

/// <inheritdoc />
public bool Evaluate(IQueryable<T> query)
{
return BuildQueryableEvaluator()(query);
}

/// <inheritdoc />
public bool Evaluate(IEnumerable<T> collection)
{
return BuildCollectionEvaluator()(collection);
}

/// <inheritdoc />
public IQueryable<T> Build(IQueryable<T> context)
{
var query = context;
Expand All @@ -162,6 +197,7 @@ public IQueryable<T> Build(IQueryable<T> context)
return query;
}

/// <inheritdoc />
public IEnumerable<T> Build(IEnumerable<T> collection)
{
if (_conditions.Count > 0)
Expand All @@ -176,18 +212,21 @@ public IEnumerable<T> Build(IEnumerable<T> collection)
return collection;
}

/// <inheritdoc />
public Paging<T> BuildWithPaging(IEnumerable<T> collection)
{
var query = collection.AsQueryable();
return BuildWithPaging(query);
}

/// <inheritdoc />
public Paging<T> BuildWithPaging(IQueryable<T> collection)
{
var (count, query) = BuildWithQueryablePaging(collection);
return new Paging<T>(count, query);
}

/// <inheritdoc />
public QueryablePaging<T> BuildWithQueryablePaging(IQueryable<T> collection)
{
var query = collection;
Expand Down

0 comments on commit d137e58

Please sign in to comment.