Skip to content

Commit d8a2999

Browse files
MnatsakanMargaryanMnatsakanMargaryan
authored andcommitted
ability to set default ordering
1 parent 3665177 commit d8a2999

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GridifyExtensions.Abstractions;
2+
3+
public interface IOrderThenBy
4+
{
5+
IOrderThenBy ThenBy(string column);
6+
IOrderThenBy ThenByDescending(string column);
7+
}

src/GridifyExtensions/Extensions/QueryableExtensions.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static async Task<PagedResponse<TDto>> FilterOrderAndGetPagedAsync<TEntit
1717
where TEntity : class
1818
{
1919
var mapper = EntityGridifyMapperByType[typeof(TEntity)] as FilterMapper<TEntity>;
20+
21+
model.OrderBy ??= mapper!.GetDefaultOrderExpression();
2022

2123
query = query.ApplyFilteringAndOrdering(model, mapper);
2224

@@ -48,6 +50,8 @@ public static IQueryable<TEntity> ApplyOrder<TEntity>(this IQueryable<TEntity> q
4850
{
4951
var mapper = EntityGridifyMapperByType[typeof(TEntity)] as FilterMapper<TEntity>;
5052

53+
model.OrderBy ??= mapper!.GetDefaultOrderExpression();
54+
5155
return query.AsNoTracking().ApplyOrdering(model, mapper);
5256
}
5357

@@ -86,7 +90,6 @@ public static async Task<PagedResponse<object>> ColumnDistinctValuesAsync<TEntit
8690
return new PagedResponse<object>(result, model.Page, model.PageSize, result.Count);
8791
}
8892

89-
9093
return await query
9194
.ApplyFiltering(model, mapper)
9295
.ApplySelect(model.PropertyName, mapper)
@@ -109,15 +112,6 @@ public static async Task<PagedResponse<object>> ColumnDistinctValuesAsync<TEntit
109112
return new PagedResponse<object>([decryptedItem], 1, 1, 1);
110113
}
111114

112-
private static Expression<Func<T, object>> CreateSelector<T>(string propertyName)
113-
{
114-
var parameter = Expression.Parameter(typeof(T), "x");
115-
var property = Expression.Property(parameter, propertyName);
116-
var converted = Expression.Convert(property, typeof(object));
117-
118-
return Expression.Lambda<Func<T, object>>(converted, parameter);
119-
}
120-
121115
public static async Task<object> AggregateAsync<TEntity>(this IQueryable<TEntity> query,
122116
AggregateQueryModel model,
123117
CancellationToken cancellationToken = default)
@@ -155,4 +149,13 @@ public static IEnumerable<MappingModel> GetMappings<TEntity>()
155149
: x.To.Body.Type.Name,
156150
});
157151
}
152+
153+
private static Expression<Func<T, object>> CreateSelector<T>(string propertyName)
154+
{
155+
var parameter = Expression.Parameter(typeof(T), "x");
156+
var property = Expression.Property(parameter, propertyName);
157+
var converted = Expression.Convert(property, typeof(object));
158+
159+
return Expression.Lambda<Func<T, object>>(converted, parameter);
160+
}
158161
}

src/GridifyExtensions/GridifyExtensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.2.2</Version>
11+
<Version>1.3.0</Version>
1212
<PackageId>Pandatech.GridifyExtensions</PackageId>
1313
<Title>Pandatech.Gridify.Extensions</Title>
1414
<PackageTags>Pandatech, library, Gridify, Pagination, Filters</PackageTags>
1515
<Description>Pandatech.Gridify.Extensions simplifies and extends the functionality of the Gridify NuGet package. It provides additional extension methods and functionality to streamline data filtering and pagination, making it more intuitive and powerful to use in .NET applications. Our enhancements ensure more flexibility, reduce boilerplate code, and improve overall developer productivity when working with Gridify.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-gridify-extensions</RepositoryUrl>
17-
<PackageReleaseNotes>Ability to set encrypted columns or array type columns in mapper</PackageReleaseNotes>
17+
<PackageReleaseNotes>Ability to set default ordering using fluent API.</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

src/GridifyExtensions/Models/FilterMapper.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
using Gridify;
2+
using GridifyExtensions.Abstractions;
23
using System.Linq.Expressions;
34

45
namespace GridifyExtensions.Models;
56

6-
public class FilterMapper<T> : GridifyMapper<T>
7+
8+
public class FilterMapper<T> : GridifyMapper<T>, IOrderThenBy
79
{
8-
private HashSet<string> _encryptedColumns { get; set; } = [];
9-
private HashSet<string> _arrayColumns { get; set; } = [];
10+
internal const string Desc = " desc";
11+
internal const string Separator = ", ";
12+
13+
private string _defaultOrderExpression = string.Empty;
14+
private readonly HashSet<string> _encryptedColumns = [];
15+
private readonly HashSet<string> _arrayColumns = [];
16+
17+
internal bool IsArray(string column) => _arrayColumns.Contains(column);
18+
internal bool IsEncrypted(string column) => _encryptedColumns.Contains(column);
1019

11-
public bool IsArray(string column) => _arrayColumns.Contains(column);
12-
public bool IsEncrypted(string column) => _encryptedColumns.Contains(column);
20+
internal string GetDefaultOrderExpression() => _defaultOrderExpression;
21+
22+
public IOrderThenBy AddDefaultOrderBy(string column)
23+
{
24+
_defaultOrderExpression = column;
25+
return this;
26+
}
27+
28+
public IOrderThenBy AddDefaultOrderByDescending(string column)
29+
{
30+
_defaultOrderExpression = column + Desc;
31+
return this;
32+
}
33+
34+
IOrderThenBy IOrderThenBy.ThenBy(string column)
35+
{
36+
_defaultOrderExpression += Separator + column;
37+
38+
return this;
39+
}
40+
41+
IOrderThenBy IOrderThenBy.ThenByDescending(string column)
42+
{
43+
_defaultOrderExpression += Separator + column + Desc;
44+
45+
return this;
46+
}
1347

1448
public IGridifyMapper<T> AddMap(string from,
1549
Expression<Func<T, object?>> to,

0 commit comments

Comments
 (0)