From 067eafbbe4f799116ae5ce352866d8960b6c5c2c Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 24 Nov 2023 18:38:53 +0100 Subject: [PATCH] Fix reverse sorting --- .../Repositories/LibraryItemRepository.cs | 16 +++--- .../Repositories/LocalRepository.cs | 2 + back/src/Kyoo.Core/Views/Helper/CrudApi.cs | 1 - .../Kyoo.Core/Views/Helper/CrudThumbsApi.cs | 1 - .../Views/Helper/ResourceViewAttribute.cs | 49 ------------------- back/src/Kyoo.Core/Views/Metadata/StaffApi.cs | 7 --- .../Kyoo.Core/Views/Resources/EpisodeApi.cs | 1 - .../Views/Resources/LibraryItemApi.cs | 1 - back/src/Kyoo.Core/Views/Resources/NewsApi.cs | 1 - .../Kyoo.Core/Views/Resources/SearchApi.cs | 1 - 10 files changed, 11 insertions(+), 69 deletions(-) delete mode 100644 back/src/Kyoo.Core/Views/Helper/ResourceViewAttribute.cs diff --git a/back/src/Kyoo.Core/Controllers/Repositories/LibraryItemRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/LibraryItemRepository.cs index 6b22f928..55c18a02 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/LibraryItemRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/LibraryItemRepository.cs @@ -106,23 +106,23 @@ namespace Kyoo.Core.Controllers return $"coalesce({string.Join(", ", keys)})"; } - public static string ProcessSort(Sort? sort, Dictionary config, bool recurse = false) + public static string ProcessSort(Sort? sort, bool reverse, Dictionary config, bool recurse = false) where T : IQuery { sort ??= new Sort.Default(); string ret = sort switch { - Sort.Default(var value) => ProcessSort(value, config, true), - Sort.By(string key, bool desc) => $"{_Property(key, config)} {(desc ? "desc" : "asc")}", - Sort.Random(var seed) => $"md5('{seed}' || {_Property("id", config)})", - Sort.Conglomerate(var list) => string.Join(", ", list.Select(x => ProcessSort(x, config, true))), + Sort.Default(var value) => ProcessSort(value, reverse, config, true), + Sort.By(string key, bool desc) => $"{_Property(key, config)} {(desc ^ reverse ? "desc" : "asc")}", + Sort.Random(var seed) => $"md5('{seed}' || {_Property("id", config)}) {(reverse ? "desc" : "asc")}", + Sort.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(); } diff --git a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs index 0d7a6759..e77d0a78 100644 --- a/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs +++ b/back/src/Kyoo.Core/Controllers/Repositories/LocalRepository.cs @@ -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(); } diff --git a/back/src/Kyoo.Core/Views/Helper/CrudApi.cs b/back/src/Kyoo.Core/Views/Helper/CrudApi.cs index a0d67230..a5a5a169 100644 --- a/back/src/Kyoo.Core/Views/Helper/CrudApi.cs +++ b/back/src/Kyoo.Core/Views/Helper/CrudApi.cs @@ -34,7 +34,6 @@ namespace Kyoo.Core.Api /// /// The type of resource to make CRUD apis for. [ApiController] - [ResourceView] public class CrudApi : BaseApi where T : class, IResource, IQuery { diff --git a/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs b/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs index 8168a09d..96a76561 100644 --- a/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs +++ b/back/src/Kyoo.Core/Views/Helper/CrudThumbsApi.cs @@ -34,7 +34,6 @@ namespace Kyoo.Core.Api /// /// The type of resource to make CRUD and thumbnails apis for. [ApiController] - [ResourceView] public class CrudThumbsApi : CrudApi where T : class, IResource, IThumbnails, IQuery { diff --git a/back/src/Kyoo.Core/Views/Helper/ResourceViewAttribute.cs b/back/src/Kyoo.Core/Views/Helper/ResourceViewAttribute.cs deleted file mode 100644 index aa896e31..00000000 --- a/back/src/Kyoo.Core/Views/Helper/ResourceViewAttribute.cs +++ /dev/null @@ -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 . - -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc.Filters; - -namespace Kyoo.Core.Api -{ - /// - /// 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 where query parameter. - /// - public class ResourceViewAttribute : ActionFilterAttribute - { - /// - public override void OnActionExecuting(ActionExecutingContext context) - { - if (context.ActionArguments.TryGetValue("where", out object? dic) && dic is Dictionary where) - { - Dictionary 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); - } - } -} diff --git a/back/src/Kyoo.Core/Views/Metadata/StaffApi.cs b/back/src/Kyoo.Core/Views/Metadata/StaffApi.cs index 01aa3983..bfbce70d 100644 --- a/back/src/Kyoo.Core/Views/Metadata/StaffApi.cs +++ b/back/src/Kyoo.Core/Views/Metadata/StaffApi.cs @@ -16,16 +16,10 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . -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 diff --git a/back/src/Kyoo.Core/Views/Resources/EpisodeApi.cs b/back/src/Kyoo.Core/Views/Resources/EpisodeApi.cs index 7be18dc7..47c84b70 100644 --- a/back/src/Kyoo.Core/Views/Resources/EpisodeApi.cs +++ b/back/src/Kyoo.Core/Views/Resources/EpisodeApi.cs @@ -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 diff --git a/back/src/Kyoo.Core/Views/Resources/LibraryItemApi.cs b/back/src/Kyoo.Core/Views/Resources/LibraryItemApi.cs index cc1d9c0c..f4bbe1c4 100644 --- a/back/src/Kyoo.Core/Views/Resources/LibraryItemApi.cs +++ b/back/src/Kyoo.Core/Views/Resources/LibraryItemApi.cs @@ -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 diff --git a/back/src/Kyoo.Core/Views/Resources/NewsApi.cs b/back/src/Kyoo.Core/Views/Resources/NewsApi.cs index 9127b7b5..a16b9c23 100644 --- a/back/src/Kyoo.Core/Views/Resources/NewsApi.cs +++ b/back/src/Kyoo.Core/Views/Resources/NewsApi.cs @@ -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 diff --git a/back/src/Kyoo.Core/Views/Resources/SearchApi.cs b/back/src/Kyoo.Core/Views/Resources/SearchApi.cs index 93c21fff..a40e9a3e 100644 --- a/back/src/Kyoo.Core/Views/Resources/SearchApi.cs +++ b/back/src/Kyoo.Core/Views/Resources/SearchApi.cs @@ -34,7 +34,6 @@ namespace Kyoo.Core.Api /// [Route("search")] [ApiController] - [ResourceView] [ApiDefinition("Search", Group = ResourcesGroup)] public class SearchApi : BaseApi {