mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-04-24 14:08:44 -04:00
Merge pull request #5095 from Bond-009/sortorder
(cherry picked from commit 98a4e1b840
)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
This commit is contained in:
parent
46a6cd8d1f
commit
8138fc3003
2 changed files with 72 additions and 8 deletions
|
@ -25,22 +25,27 @@ namespace Jellyfin.Api.Helpers
|
||||||
/// <param name="sortBy">Sort By. Comma delimited string.</param>
|
/// <param name="sortBy">Sort By. Comma delimited string.</param>
|
||||||
/// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
|
/// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
|
||||||
/// <returns>Order By.</returns>
|
/// <returns>Order By.</returns>
|
||||||
public static ValueTuple<string, SortOrder>[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
|
public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
|
||||||
{
|
{
|
||||||
if (sortBy.Count == 0)
|
if (sortBy.Count == 0)
|
||||||
{
|
{
|
||||||
return Array.Empty<ValueTuple<string, SortOrder>>();
|
return Array.Empty<ValueTuple<string, SortOrder>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new ValueTuple<string, SortOrder>[sortBy.Count];
|
var result = new (string, SortOrder)[sortBy.Count];
|
||||||
for (var i = 0; i < sortBy.Count; i++)
|
var i = 0;
|
||||||
|
// Add elements which have a SortOrder specified
|
||||||
|
for (; i < requestedSortOrder.Count; i++)
|
||||||
{
|
{
|
||||||
var sortOrderIndex = requestedSortOrder.Count > i ? i : 0;
|
result[i] = (sortBy[i], requestedSortOrder[i]);
|
||||||
|
}
|
||||||
|
|
||||||
var sortOrder = requestedSortOrder.Count > sortOrderIndex
|
// Add remaining elements with the first specified SortOrder
|
||||||
? requestedSortOrder[sortOrderIndex]
|
// or the default one if no SortOrders are specified
|
||||||
: SortOrder.Ascending;
|
var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
|
||||||
result[i] = new ValueTuple<string, SortOrder>(sortBy[i], sortOrder);
|
for (; i < sortBy.Count; i++)
|
||||||
|
{
|
||||||
|
result[i] = (sortBy[i], order);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
59
tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
Normal file
59
tests/Jellyfin.Api.Tests/Helpers/RequestHelpersTests.cs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Jellyfin.Api.Helpers;
|
||||||
|
using Jellyfin.Data.Enums;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.Api.Tests.Helpers
|
||||||
|
{
|
||||||
|
public class RequestHelpersTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetOrderBy_Success_TestData))]
|
||||||
|
public void GetOrderBy_Success(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder, (string, SortOrder)[] expected)
|
||||||
|
{
|
||||||
|
Assert.Equal(expected, RequestHelpers.GetOrderBy(sortBy, requestedSortOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> GetOrderBy_Success_TestData()
|
||||||
|
{
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
Array.Empty<string>(),
|
||||||
|
Array.Empty<SortOrder>(),
|
||||||
|
Array.Empty<(string, SortOrder)>()
|
||||||
|
};
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new string[]
|
||||||
|
{
|
||||||
|
"IsFavoriteOrLiked",
|
||||||
|
"Random"
|
||||||
|
},
|
||||||
|
Array.Empty<SortOrder>(),
|
||||||
|
new (string, SortOrder)[]
|
||||||
|
{
|
||||||
|
("IsFavoriteOrLiked", SortOrder.Ascending),
|
||||||
|
("Random", SortOrder.Ascending),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new string[]
|
||||||
|
{
|
||||||
|
"SortName",
|
||||||
|
"ProductionYear"
|
||||||
|
},
|
||||||
|
new SortOrder[]
|
||||||
|
{
|
||||||
|
SortOrder.Descending
|
||||||
|
},
|
||||||
|
new (string, SortOrder)[]
|
||||||
|
{
|
||||||
|
("SortName", SortOrder.Descending),
|
||||||
|
("ProductionYear", SortOrder.Descending),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue