| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using Plainquire.Sort.Abstractions; |
| | 3 | | using System; |
| | 4 | | using System.Text.Json.Serialization; |
| | 5 | | using System.Text.RegularExpressions; |
| | 6 | |
|
| | 7 | | namespace Plainquire.Sort; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// A sort order for a property. |
| | 11 | | /// </summary> |
| | 12 | | public class PropertySort |
| | 13 | | { |
| | 14 | | private readonly SortConfiguration? _configuration; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Path used when no member is specified in a member access expression (<c>person => person</c> instead of <c>perso |
| | 18 | | /// </summary> |
| | 19 | | public const string PATH_TO_SELF = "$SELF$"; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The path to the property. |
| | 23 | | /// </summary> |
| | 24 | | public string PropertyPath { get; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The sort direction. |
| | 28 | | /// </summary> |
| | 29 | | public SortDirection Direction { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The position of the sort order. |
| | 33 | | /// </summary> |
| | 34 | | public int Position { get; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Creates a new instance of <see cref="PropertySort"/>. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="propertyPath">Path to the property to be sorted by.</param> |
| | 40 | | /// <param name="direction">Sort direction to use.</param> |
| | 41 | | /// <param name="position">Position in the final sorting sequence.</param> |
| | 42 | | /// <param name="configuration">The configuration to use.</param> |
| | 43 | | /// <exception cref="ArgumentException"></exception> |
| | 44 | | [JsonConstructor] |
| | 45 | | internal PropertySort(string propertyPath, SortDirection direction, int position, SortConfiguration? configuration) |
| | 46 | | { |
| 711 | 47 | | PropertyPath = propertyPath ?? throw new ArgumentNullException(nameof(propertyPath)); |
| | 48 | | Direction = direction; |
| | 49 | | Position = position; |
| 711 | 50 | | _configuration = configuration; |
| 711 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Creates a new instance of <see cref="PropertySort"/>. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="propertyPath">Path to the property to be sorted by.</param> |
| | 57 | | /// <param name="direction">Sort direction to use.</param> |
| | 58 | | /// <param name="position">Position in the final sorting sequence.</param> |
| | 59 | | /// <param name="configuration">The configuration to use.</param> |
| | 60 | | /// <exception cref="ArgumentNullException"></exception> |
| | 61 | | public static PropertySort Create(string propertyPath, SortDirection direction, int? position = null, SortConfigurat |
| 468 | 62 | | => new(propertyPath, direction, position ?? 0, configuration); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Creates a new instance of <see cref="PropertySort"/>. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="sortSyntax">The sort order syntax.</param> |
| | 68 | | /// <param name="position">Position in the final sorting sequence.</param> |
| | 69 | | /// <param name="configuration">The configuration to use.</param> |
| | 70 | | /// <returns></returns> |
| | 71 | | public static PropertySort Create(string sortSyntax, int? position = null, SortConfiguration? configuration = null) |
| | 72 | | { |
| 309 | 73 | | if (string.IsNullOrEmpty(sortSyntax)) |
| 2 | 74 | | throw new ArgumentException("Value cannot be null or empty.", nameof(sortSyntax)); |
| | 75 | |
|
| 307 | 76 | | var (propertyPath, sortDirection) = ParseSortSyntax(sortSyntax, configuration); |
| 307 | 77 | | return Create(propertyPath, sortDirection, position, configuration); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | public override string ToString() |
| | 82 | | { |
| 4 | 83 | | var configuration = _configuration ?? SortConfiguration.Default ?? new SortConfiguration(); |
| | 84 | |
|
| 4 | 85 | | var directionPostfix = Direction == SortDirection.Ascending |
| 4 | 86 | | ? configuration.PrimaryAscendingPostfix |
| 4 | 87 | | : configuration.PrimaryDescendingPostfix; |
| | 88 | |
|
| 4 | 89 | | return $"{PropertyPath}{directionPostfix}"; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private static (string PropertyPath, SortDirection Direction) ParseSortSyntax(string sortSyntax, SortConfiguration? |
| | 93 | | { |
| 307 | 94 | | configuration ??= SortConfiguration.Default ?? new SortConfiguration(); |
| | 95 | |
|
| 307 | 96 | | var sortSyntaxPattern = $"^(?<prefix>{configuration.SortDirectionPrefixPattern})(?<propertyPath>.*?)(?<postfix>{ |
| 307 | 97 | | var match = Regex.Match(sortSyntax, sortSyntaxPattern, RegexOptions.IgnoreCase, RegexDefaults.Timeout); |
| | 98 | |
|
| 307 | 99 | | var hasDescendingPrefix = configuration.DescendingPrefixes.Contains(match.Groups["prefix"].Value); |
| 307 | 100 | | var hasDescendingPostfix = configuration.DescendingPostfixes.Contains(match.Groups["postfix"].Value); |
| | 101 | |
|
| 307 | 102 | | var sortDirection = hasDescendingPrefix || hasDescendingPostfix |
| 307 | 103 | | ? SortDirection.Descending |
| 307 | 104 | | : SortDirection.Ascending; |
| | 105 | |
|
| 307 | 106 | | var propertyPath = match.Groups["propertyPath"].Value; |
| | 107 | |
|
| 307 | 108 | | return (propertyPath, sortDirection); |
| | 109 | | } |
| | 110 | | } |