diff options
| author | crobibero <cody@robibe.ro> | 2020-07-06 10:00:23 -0600 |
|---|---|---|
| committer | crobibero <cody@robibe.ro> | 2020-07-06 10:00:23 -0600 |
| commit | b2e7a4a1cb335e7281f7857cc88fadd6b671dcb4 (patch) | |
| tree | f5f8769b907cf183b1309acec891641e0ec954cb /Jellyfin.Api/Helpers/RequestHelpers.cs | |
| parent | 068725cdedecc88bcde9f18c68b9b1e5c0f6e569 (diff) | |
| parent | 613444a152ecede061d229cbd0d528d88915db7b (diff) | |
Merge remote-tracking branch 'upstream/api-migration' into api-channel
Diffstat (limited to 'Jellyfin.Api/Helpers/RequestHelpers.cs')
| -rw-r--r-- | Jellyfin.Api/Helpers/RequestHelpers.cs | 81 |
1 files changed, 79 insertions, 2 deletions
diff --git a/Jellyfin.Api/Helpers/RequestHelpers.cs b/Jellyfin.Api/Helpers/RequestHelpers.cs index 242f88467..070711681 100644 --- a/Jellyfin.Api/Helpers/RequestHelpers.cs +++ b/Jellyfin.Api/Helpers/RequestHelpers.cs @@ -74,7 +74,7 @@ namespace Jellyfin.Api.Helpers /// <param name="separator">The char that separates the substrings.</param> /// <param name="removeEmpty">Option to remove empty substrings from the array.</param> /// <returns>An array of the substrings.</returns> - internal static string[] Split(string value, char separator, bool removeEmpty) + internal static string[] Split(string? value, char separator, bool removeEmpty) { if (string.IsNullOrWhiteSpace(value)) { @@ -142,9 +142,86 @@ namespace Jellyfin.Api.Helpers return Array.Empty<Guid>(); } - return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + return Split(value, ',', true) .Select(i => new Guid(i)) .ToArray(); } + + /// <summary> + /// Get orderby. + /// </summary> + /// <param name="sortBy">Sort by.</param> + /// <param name="requestedSortOrder">Sort order.</param> + /// <returns>Resulting order by.</returns> + internal static ValueTuple<string, SortOrder>[] GetOrderBy(string? sortBy, string? requestedSortOrder) + { + if (string.IsNullOrEmpty(sortBy)) + { + return Array.Empty<ValueTuple<string, SortOrder>>(); + } + + var vals = sortBy.Split(','); + if (string.IsNullOrWhiteSpace(requestedSortOrder)) + { + requestedSortOrder = "Ascending"; + } + + var sortOrders = requestedSortOrder.Split(','); + + var result = new ValueTuple<string, SortOrder>[vals.Length]; + + for (var i = 0; i < vals.Length; i++) + { + var sortOrderIndex = sortOrders.Length > i ? i : 0; + + var sortOrderValue = sortOrders.Length > sortOrderIndex ? sortOrders[sortOrderIndex] : null; + var sortOrder = string.Equals(sortOrderValue, "Descending", StringComparison.OrdinalIgnoreCase) + ? SortOrder.Descending + : SortOrder.Ascending; + + result[i] = new ValueTuple<string, SortOrder>(vals[i], sortOrder); + } + + return result; + } + + /// <summary> + /// Gets the filters. + /// </summary> + /// <param name="filters">The filter string.</param> + /// <returns>IEnumerable{ItemFilter}.</returns> + internal static ItemFilter[] GetFilters(string filters) + { + return string.IsNullOrEmpty(filters) + ? Array.Empty<ItemFilter>() + : Split(filters, ',', true) + .Select(v => Enum.Parse<ItemFilter>(v, true)).ToArray(); + } + + /// <summary> + /// Gets the item fields. + /// </summary> + /// <param name="fields">The fields string.</param> + /// <returns>IEnumerable{ItemFields}.</returns> + internal static ItemFields[] GetItemFields(string? fields) + { + if (string.IsNullOrEmpty(fields)) + { + return Array.Empty<ItemFields>(); + } + + return Split(fields, ',', true) + .Select(v => + { + if (Enum.TryParse(v, true, out ItemFields value)) + { + return (ItemFields?)value; + } + + return null; + }).Where(i => i.HasValue) + .Select(i => i!.Value) + .ToArray(); + } } } |
