Skip to content

Commit

Permalink
Extensão para metodo ApllySort
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezrafael committed Aug 15, 2021
1 parent 32b22dd commit f371b1d
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions CourseLibrary.API/Helpers/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using CourseLibrary.API.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
namespace CourseLibrary.API.Helpers
{
public static class IQueryableExtensions
{
public static IQueryable<T> ApplySort<T>(this IQueryable<T> source, string orderBy,
Dictionary<string, PropertyMappingValue> mappingDictionary)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

if (mappingDictionary == null)
{
throw new ArgumentNullException(nameof(mappingDictionary));
}

if (string.IsNullOrWhiteSpace(orderBy))
{
return source;
}

var orderByString = string.Empty;

// the orderBy string is separated by ",", so we split it.
var orderByAfterSplit = orderBy.Split(',');

// apply each orderby clause
foreach (var orderByClause in orderByAfterSplit)
{
// trim the orderBy clause, as it might contain leading
// or trailing spaces. Can't trim the var in foreach,
// so use another var.
var trimmedOrderByClause = orderByClause.Trim();

// if the sort option ends with with " desc", we order
// descending, ortherwise ascending
var orderDescending = trimmedOrderByClause.EndsWith(" desc");

// remove " asc" or " desc" from the orderBy clause, so we
// get the property name to look for in the mapping dictionary
var indexOfFirstSpace = trimmedOrderByClause.IndexOf(" ");
var propertyName = indexOfFirstSpace == -1 ?
trimmedOrderByClause : trimmedOrderByClause.Remove(indexOfFirstSpace);

// find the matching property
if (!mappingDictionary.ContainsKey(propertyName))
{
throw new ArgumentException($"Key mapping for {propertyName} is missing");
}

// get the PropertyMappingValue
var propertyMappingValue = mappingDictionary[propertyName];

if (propertyMappingValue == null)
{
throw new ArgumentNullException("propertyMappingValue");
}

// Run through the property names
// so the orderby clauses are applied in the correct order
foreach (var destinationProperty in
propertyMappingValue.DestinationProperties)
{
// revert sort order if necessary
if (propertyMappingValue.Revert)
{
orderDescending = !orderDescending;
}

orderByString = orderByString +
(string.IsNullOrWhiteSpace(orderByString) ? string.Empty : ", ")
+ destinationProperty
+ (orderDescending ? " descending" : " ascending");
}
}

return source.OrderBy(orderByString);
}
}
}

0 comments on commit f371b1d

Please sign in to comment.