using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
namespace Jellyfin.Api.Helpers
{
///
/// Request Extensions.
///
public static class RequestHelpers
{
///
/// Get Order By.
///
/// Sort By. Comma delimited string.
/// Sort Order. Comma delimited string.
/// Order By.
public static ValueTuple[] GetOrderBy(string sortBy, string requestedSortOrder)
{
var val = sortBy;
if (string.IsNullOrEmpty(val))
{
return Array.Empty>();
}
var vals = val.Split(',');
if (string.IsNullOrWhiteSpace(requestedSortOrder))
{
requestedSortOrder = "Ascending";
}
var sortOrders = requestedSortOrder.Split(',');
var result = new ValueTuple[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(vals[i], sortOrder);
}
return result;
}
///
/// Gets the item fields.
///
/// The fields.
/// IEnumerable{ItemFields}.
public static ItemFields[] GetItemFields(string fields)
{
if (string.IsNullOrEmpty(fields))
{
return Array.Empty();
}
return fields.Split(',').Select(v =>
{
if (Enum.TryParse(v, true, out ItemFields value))
{
return (ItemFields?)value;
}
return null;
}).Where(i => i.HasValue).Select(i => i.Value).ToArray();
}
///
/// Get parsed filters.
///
/// The filters.
/// Item filters.
public static IEnumerable GetFilters(string filters)
{
return string.IsNullOrEmpty(filters)
? Array.Empty()
: filters.Split(',').Select(v => Enum.Parse(v, true));
}
}
}