Fix reverse sorting

This commit is contained in:
Zoe Roux 2023-11-24 18:38:53 +01:00
parent 253e256458
commit 067eafbbe4
10 changed files with 11 additions and 69 deletions

View File

@ -106,23 +106,23 @@ namespace Kyoo.Core.Controllers
return $"coalesce({string.Join(", ", keys)})";
}
public static string ProcessSort<T>(Sort<T>? sort, Dictionary<string, Type> config, bool recurse = false)
public static string ProcessSort<T>(Sort<T>? sort, bool reverse, Dictionary<string, Type> config, bool recurse = false)
where T : IQuery
{
sort ??= new Sort<T>.Default();
string ret = sort switch
{
Sort<T>.Default(var value) => ProcessSort(value, config, true),
Sort<T>.By(string key, bool desc) => $"{_Property(key, config)} {(desc ? "desc" : "asc")}",
Sort<T>.Random(var seed) => $"md5('{seed}' || {_Property("id", config)})",
Sort<T>.Conglomerate(var list) => string.Join(", ", list.Select(x => ProcessSort(x, config, true))),
Sort<T>.Default(var value) => ProcessSort(value, reverse, config, true),
Sort<T>.By(string key, bool desc) => $"{_Property(key, config)} {(desc ^ reverse ? "desc" : "asc")}",
Sort<T>.Random(var seed) => $"md5('{seed}' || {_Property("id", config)}) {(reverse ? "desc" : "asc")}",
Sort<T>.Conglomerate(var list) => string.Join(", ", list.Select(x => ProcessSort(x, reverse, config, true))),
_ => throw new SwitchExpressionException(),
};
if (recurse)
return ret;
// always end query by an id sort.
return $"{ret}, {_Property("id", config)} asc";
return $"{ret}, {_Property("id", config)} {(reverse ? "desc" : "asc")}";
}
public static (
@ -278,7 +278,7 @@ namespace Kyoo.Core.Controllers
}
if (filter != null)
query += ProcessFilter(filter, config);
query += $"order by {ProcessSort(sort, config):raw}";
query += $"order by {ProcessSort(sort, limit.Reverse, config):raw}";
query += $"limit {limit.Limit}";
Type[] types = config.Select(x => x.Value)
@ -294,6 +294,8 @@ namespace Kyoo.Core.Controllers
return mapIncludes(collection, items.Skip(3));
throw new InvalidDataException();
});
if (limit.Reverse)
data = data.Reverse();
return data.ToList();
}

View File

@ -302,6 +302,8 @@ namespace Kyoo.Core.Controllers
query = query.Reverse();
if (limit.Limit > 0)
query = query.Take(limit.Limit);
if (limit.Reverse)
query = query.Reverse();
return await query.ToListAsync();
}

View File

@ -34,7 +34,6 @@ namespace Kyoo.Core.Api
/// </summary>
/// <typeparam name="T">The type of resource to make CRUD apis for.</typeparam>
[ApiController]
[ResourceView]
public class CrudApi<T> : BaseApi
where T : class, IResource, IQuery
{

View File

@ -34,7 +34,6 @@ namespace Kyoo.Core.Api
/// </summary>
/// <typeparam name="T">The type of resource to make CRUD and thumbnails apis for.</typeparam>
[ApiController]
[ResourceView]
public class CrudThumbsApi<T> : CrudApi<T>
where T : class, IResource, IThumbnails, IQuery
{

View File

@ -1,49 +0,0 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Kyoo.Core.Api
{
/// <summary>
/// An attribute to put on most controllers. It handle fields loading (only retuning fields requested and if they
/// are requested, load them) and help for the <c>where</c> query parameter.
/// </summary>
public class ResourceViewAttribute : ActionFilterAttribute
{
/// <inheritdoc />
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.ActionArguments.TryGetValue("where", out object? dic) && dic is Dictionary<string, string> where)
{
Dictionary<string, string> nWhere = new(where, StringComparer.InvariantCultureIgnoreCase);
nWhere.Remove("fields");
nWhere.Remove("afterID");
nWhere.Remove("limit");
nWhere.Remove("reverse");
foreach ((string key, _) in context.ActionArguments)
nWhere.Remove(key);
context.ActionArguments["where"] = nWhere;
}
base.OnActionExecuting(context);
}
}
}

View File

@ -16,16 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Kyoo.Abstractions.Models.Utils.Constants;
@ -37,7 +31,6 @@ namespace Kyoo.Core.Api
[Route("staff")]
[Route("people", Order = AlternativeRoute)]
[ApiController]
[ResourceView]
[PartialPermission(nameof(People))]
[ApiDefinition("Staff", Group = MetadataGroup)]
public class StaffApi : CrudThumbsApi<People>

View File

@ -34,7 +34,6 @@ namespace Kyoo.Core.Api
[Route("episodes")]
[Route("episode", Order = AlternativeRoute)]
[ApiController]
[ResourceView]
[PartialPermission(nameof(Episode))]
[ApiDefinition("Episodes", Group = ResourcesGroup)]
public class EpisodeApi : CrudThumbsApi<Episode>

View File

@ -32,7 +32,6 @@ namespace Kyoo.Core.Api
[Route("items")]
[Route("item", Order = AlternativeRoute)]
[ApiController]
[ResourceView]
[PartialPermission("LibraryItem")]
[ApiDefinition("Items", Group = ResourcesGroup)]
public class LibraryItemApi : CrudThumbsApi<ILibraryItem>

View File

@ -31,7 +31,6 @@ namespace Kyoo.Core.Api
[Route("news")]
[Route("new", Order = AlternativeRoute)]
[ApiController]
[ResourceView]
[PartialPermission("LibraryItem")]
[ApiDefinition("News", Group = ResourcesGroup)]
public class NewsApi : CrudThumbsApi<News>

View File

@ -34,7 +34,6 @@ namespace Kyoo.Core.Api
/// </summary>
[Route("search")]
[ApiController]
[ResourceView]
[ApiDefinition("Search", Group = ResourcesGroup)]
public class SearchApi : BaseApi
{