Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/AutSoft.Linq/AutSoft.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<Nullable>enable</Nullable>
<ErrorReport>send</ErrorReport>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutSoft.Core\AutSoft.Common.csproj" />
</ItemGroup>
Expand Down
84 changes: 0 additions & 84 deletions src/AutSoft.Linq/Queryable/OrderByExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using AutoMapper;
using AutoMapper.Internal;

using AutSoft.Common.Exceptions;
using AutSoft.Linq.Models;

using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -75,7 +71,6 @@ public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(
/// <returns><see cref="IQueryable{T}" /> with ordering information</returns>
/// <remarks>
/// This overload fits if the page request contains ordering information in entity model level.
/// If you use AutoMapper's <see cref="IMapper.ProjectTo"/> consider to use overloads with TDto type parameters
/// </remarks>
public static IOrderedQueryable<TSource> OrderBy<TSource>(
this IQueryable<TSource> source,
Expand All @@ -88,83 +83,4 @@ public static IOrderedQueryable<TSource> OrderBy<TSource>(
: e => EF.Property<object>(e!, pageRequest.OrderBy),
pageRequest.OrderDirection);
}

/// <summary>
/// OrderBy where the desired ordering is coming from a <see cref="PageRequest"/>
/// and mapping expression calculated based on the provided mapping configuration
/// </summary>
/// <typeparam name="TSource">Element's type</typeparam>
/// <typeparam name="TDto">Target DTO's type to find mapping information</typeparam>
/// <param name="source">An <see cref="IQueryable{T}" /> to order</param>
/// <param name="pageRequest">A page request which contains the desired column's name to order.</param>
/// <param name="defaultOrderingSelector">If the page request does not define any ordering information</param>
/// <param name="mappings">AutoMapper mapping configuration which contains mapping expressions for TSource -> TDto type conversion</param>
/// <returns><see cref="IQueryable{T}" /> with ordering information</returns>
public static IOrderedQueryable<TSource> OrderBy<TSource, TDto>(
this IQueryable<TSource> source,
PageRequest pageRequest,
Expression<Func<TSource, object?>> defaultOrderingSelector,
IConfigurationProvider mappings)
{
var orderKeySelector = GetOrderKeySelector<TSource, TDto>(pageRequest, defaultOrderingSelector, mappings);

return source.OrderBy(orderKeySelector, pageRequest.OrderDirection);
}

/// <summary>
/// OrderBy where the desired ordering is coming from a <see cref="PageRequest"/>
/// and mapping expression calculated based on the provided mapping configuration
/// </summary>
/// <typeparam name="TSource">Element's type</typeparam>
/// <typeparam name="TDto">Target DTO's type to find mapping information</typeparam>
/// <param name="source">An <see cref="IQueryable{T}" /> to order</param>
/// <param name="pageRequest">A page request which contains the desired column's name to order.</param>
/// <param name="defaultOrderingSelector">If the page request does not define any ordering information</param>
/// <param name="defaultOrderDirection">Ordering direction for <paramref name="defaultOrderingSelector"/></param>
/// <param name="mappings">AutoMapper mapping configuration which contains mapping expressions for TSource -> TDto type conversion</param>
/// <returns><see cref="IQueryable{T}" /> with ordering information</returns>
public static IOrderedQueryable<TSource> OrderBy<TSource, TDto>(
this IQueryable<TSource> source,
PageRequest pageRequest,
Expression<Func<TSource, object?>> defaultOrderingSelector,
OrderDirection defaultOrderDirection,
IConfigurationProvider mappings)
{
var orderKeySelector = GetOrderKeySelector<TSource, TDto>(pageRequest, defaultOrderingSelector, mappings);

if (orderKeySelector == defaultOrderingSelector)
return source.OrderBy(orderKeySelector, defaultOrderDirection);

return source.OrderBy(orderKeySelector, pageRequest.OrderDirection);
}

private static Expression<Func<TSource, object?>> GetOrderKeySelector<TSource, TDto>(
PageRequest pageRequest,
Expression<Func<TSource, object?>> defaultOrderingSelector,
IConfigurationProvider mappings)
{
if (!string.IsNullOrEmpty(pageRequest.OrderBy))
{
// The caller want to order based on a not existed or an unsortable property
var pi = typeof(TDto).GetProperty(pageRequest.OrderBy);
if (pi?.IsSortable() != true)
throw new ValidationException(pageRequest.OrderBy, "Cannot sort based on this property!");
}

var orderKeySelector = defaultOrderingSelector;

if (!string.IsNullOrEmpty(pageRequest.OrderBy))
{
var expression = mappings.Internal().FindTypeMapFor<TSource, TDto>()
?.PropertyMaps
?.FirstOrDefault(m => m.CustomMapExpression != null && m.DestinationName == pageRequest.OrderBy)
?.CustomMapExpression;

orderKeySelector = expression != null
? Expression.Lambda<Func<TSource, object?>>(Expression.Convert(expression.Body, typeof(object)), expression.Parameters)
: e => EF.Property<object>(e!, pageRequest.OrderBy);
}

return orderKeySelector;
}
}

This file was deleted.

Loading