diff --git a/src/AutSoft.Linq/AutSoft.Linq.csproj b/src/AutSoft.Linq/AutSoft.Linq.csproj index 6c7848f..2cf09f5 100644 --- a/src/AutSoft.Linq/AutSoft.Linq.csproj +++ b/src/AutSoft.Linq/AutSoft.Linq.csproj @@ -5,9 +5,6 @@ enable send - - - diff --git a/src/AutSoft.Linq/Queryable/OrderByExtensions.cs b/src/AutSoft.Linq/Queryable/OrderByExtensions.cs index 8ca4424..f0d986a 100644 --- a/src/AutSoft.Linq/Queryable/OrderByExtensions.cs +++ b/src/AutSoft.Linq/Queryable/OrderByExtensions.cs @@ -1,7 +1,3 @@ -using AutoMapper; -using AutoMapper.Internal; - -using AutSoft.Common.Exceptions; using AutSoft.Linq.Models; using Microsoft.EntityFrameworkCore; @@ -75,7 +71,6 @@ public static IOrderedQueryable ThenBy( /// with ordering information /// /// This overload fits if the page request contains ordering information in entity model level. - /// If you use AutoMapper's consider to use overloads with TDto type parameters /// public static IOrderedQueryable OrderBy( this IQueryable source, @@ -88,83 +83,4 @@ public static IOrderedQueryable OrderBy( : e => EF.Property(e!, pageRequest.OrderBy), pageRequest.OrderDirection); } - - /// - /// OrderBy where the desired ordering is coming from a - /// and mapping expression calculated based on the provided mapping configuration - /// - /// Element's type - /// Target DTO's type to find mapping information - /// An to order - /// A page request which contains the desired column's name to order. - /// If the page request does not define any ordering information - /// AutoMapper mapping configuration which contains mapping expressions for TSource -> TDto type conversion - /// with ordering information - public static IOrderedQueryable OrderBy( - this IQueryable source, - PageRequest pageRequest, - Expression> defaultOrderingSelector, - IConfigurationProvider mappings) - { - var orderKeySelector = GetOrderKeySelector(pageRequest, defaultOrderingSelector, mappings); - - return source.OrderBy(orderKeySelector, pageRequest.OrderDirection); - } - - /// - /// OrderBy where the desired ordering is coming from a - /// and mapping expression calculated based on the provided mapping configuration - /// - /// Element's type - /// Target DTO's type to find mapping information - /// An to order - /// A page request which contains the desired column's name to order. - /// If the page request does not define any ordering information - /// Ordering direction for - /// AutoMapper mapping configuration which contains mapping expressions for TSource -> TDto type conversion - /// with ordering information - public static IOrderedQueryable OrderBy( - this IQueryable source, - PageRequest pageRequest, - Expression> defaultOrderingSelector, - OrderDirection defaultOrderDirection, - IConfigurationProvider mappings) - { - var orderKeySelector = GetOrderKeySelector(pageRequest, defaultOrderingSelector, mappings); - - if (orderKeySelector == defaultOrderingSelector) - return source.OrderBy(orderKeySelector, defaultOrderDirection); - - return source.OrderBy(orderKeySelector, pageRequest.OrderDirection); - } - - private static Expression> GetOrderKeySelector( - PageRequest pageRequest, - Expression> 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() - ?.PropertyMaps - ?.FirstOrDefault(m => m.CustomMapExpression != null && m.DestinationName == pageRequest.OrderBy) - ?.CustomMapExpression; - - orderKeySelector = expression != null - ? Expression.Lambda>(Expression.Convert(expression.Body, typeof(object)), expression.Parameters) - : e => EF.Property(e!, pageRequest.OrderBy); - } - - return orderKeySelector; - } } diff --git a/test/AutSoft.Linq.Tests/Queryable/OrderByExtensions/OrderByExtensionsTests.OrderByFromMappings.cs b/test/AutSoft.Linq.Tests/Queryable/OrderByExtensions/OrderByExtensionsTests.OrderByFromMappings.cs deleted file mode 100644 index 70a447c..0000000 --- a/test/AutSoft.Linq.Tests/Queryable/OrderByExtensions/OrderByExtensionsTests.OrderByFromMappings.cs +++ /dev/null @@ -1,145 +0,0 @@ -using AutoMapper.QueryableExtensions; - -using AutSoft.Common.Exceptions; -using AutSoft.Linq.Models; -using AutSoft.Linq.Queryable; - -using FluentAssertions; - -using System.Linq.Expressions; - -namespace AutSoft.Linq.Tests.Queryable.OrderByExtensions; - -public partial class OrderByExtensionsTests -{ - public class OrderByFromMappings : OrderByExtensionsTests - { - public static List<(string orderColumn, Expression> expectedOrderExpression)> OrderExpression() => new() - { - (nameof(PersonDto.Id), p => p.Id), - (nameof(PersonDto.Name), p => p.Name), - (nameof(PersonDto.Age), p => p.Age), - }; - - [Theory] - [CombinatorialData] - public void Should_ReturnOrdered( - [CombinatorialMemberData(nameof(OrderExpression))] (string orderColumn, Expression> expectedOrderExpression) ordering, - OrderDirection orderDirection) - { - // Act - var ordered = Subject - .OrderBy( - new PageRequest { OrderBy = ordering.orderColumn, OrderDirection = orderDirection }, - p => p.Id, - Mapper.ConfigurationProvider) - .ProjectTo(Mapper.ConfigurationProvider) - .ToList(); - - // Assert - ordered.Should().HaveCount(Subject.Count()).And.HaveCountGreaterThan(0); - if (orderDirection is OrderDirection.Ascending) - { - ordered.Should().BeInAscendingOrder(ordering.expectedOrderExpression); - } - else if (orderDirection is OrderDirection.Descending) - { - ordered.Should().BeInDescendingOrder(ordering.expectedOrderExpression); - } - else - { - throw new NotSupportedException("Not valid test case input"); - } - } - - public static List<(Expression> defaultOrderExpression, Expression> expectedOrderExpression)> DefaultOrderExpression() => new() - { - (p => p.Id, p => p.Id), - (p => p.Name, p => p.Name), - (p => p.Address, p => p.Address), - }; - - [Theory] - [CombinatorialData] - public void Should_ReturnDefaultOrdered_WithoutOrderingInfo( - [CombinatorialMemberData(nameof(DefaultOrderExpression))] - (Expression> defaultOrderExpression, Expression> expectedOrderExpression) expressions, - OrderDirection orderDirection) - { - // Act - var ordered = Subject - .OrderBy( - new PageRequest() { OrderDirection = orderDirection }, - expressions.defaultOrderExpression, - Mapper.ConfigurationProvider) - .ProjectTo(Mapper.ConfigurationProvider) - .ToList(); - - // Assert - ordered.Should().HaveCount(Subject.Count()).And.HaveCountGreaterThan(0); - if (orderDirection is OrderDirection.Ascending) - { - ordered.Should().BeInAscendingOrder(expressions.expectedOrderExpression); - } - else if (orderDirection is OrderDirection.Descending) - { - ordered.Should().BeInDescendingOrder(expressions.expectedOrderExpression); - } - else - { - throw new NotSupportedException("Not valid test case input"); - } - } - - [Theory] - [CombinatorialData] - public void Should_ReturnDefaultOrdered_WithDefaultOrderDirection( - [CombinatorialMemberData(nameof(DefaultOrderExpression))] - (Expression> defaultOrderExpression, Expression> expectedOrderExpression) expressions, - OrderDirection defaultOrderDirection) - { - // Act - var ordered = Subject - .OrderBy(new PageRequest(), expressions.defaultOrderExpression, defaultOrderDirection, Mapper.ConfigurationProvider) - .ProjectTo(Mapper.ConfigurationProvider) - .ToList(); - - // Assert - ordered.Should().HaveCount(Subject.Count()).And.HaveCountGreaterThan(0); - if (defaultOrderDirection is OrderDirection.Ascending) - { - ordered.Should().BeInAscendingOrder(expressions.expectedOrderExpression); - } - else if (defaultOrderDirection is OrderDirection.Descending) - { - ordered.Should().BeInDescendingOrder(expressions.expectedOrderExpression); - } - else - { - throw new NotSupportedException("Not valid test case input"); - } - } - - [Fact] - public void Should_ThrowOnNotSortableColumn() - { - // Act - var func = () => Subject - .OrderBy(new PageRequest { OrderBy = nameof(PersonDto.Address) }, p => p.Id, Mapper.ConfigurationProvider); - - // Assert - func.Should().Throw(); - } - - [Fact] - public void Should_ThrowOnNotExistedColumn() - { - // Act - var func = () => Subject - .OrderBy(new PageRequest { OrderBy = "TEST" }, p => p.Id, Mapper.ConfigurationProvider); - - // Assert - func.Should().Throw(); - } - } -}