-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32b22dd
commit f371b1d
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |