mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add random sort
This commit is contained in:
parent
2fa5587fbf
commit
0a38fdd8d3
@ -56,6 +56,9 @@ namespace Kyoo.Abstractions.Controllers
|
||||
/// <param name="List">The list of keys to sort by.</param>
|
||||
public record Conglomerate(params Sort<T>[] List) : Sort<T>;
|
||||
|
||||
/// <summary>Sort randomly items</summary>
|
||||
public record Random() : Sort<T>;
|
||||
|
||||
/// <summary>The default sort method for the given type.</summary>
|
||||
public record Default : Sort<T>;
|
||||
|
||||
@ -69,6 +72,8 @@ namespace Kyoo.Abstractions.Controllers
|
||||
{
|
||||
if (string.IsNullOrEmpty(sortBy) || sortBy == "default")
|
||||
return new Default();
|
||||
if (sortBy == "random")
|
||||
return new Random();
|
||||
if (sortBy.Contains(','))
|
||||
return new Conglomerate(sortBy.Split(',').Select(From).ToArray());
|
||||
|
||||
|
@ -19,12 +19,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Postgresql;
|
||||
using Kyoo.Utils;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Core.Controllers
|
||||
@ -53,42 +51,6 @@ namespace Kyoo.Core.Controllers
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<LibraryItem> GetOrDefault(int id)
|
||||
{
|
||||
return await _database.LibraryItems.SingleOrDefaultAsync(x => x.Id == id).Then(SetBackingImage);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<LibraryItem> GetOrDefault(string slug)
|
||||
{
|
||||
return await _database.LibraryItems.SingleOrDefaultAsync(x => x.Slug == slug).Then(SetBackingImage);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<LibraryItem> GetOrDefault(Expression<Func<LibraryItem, bool>> where, Sort<LibraryItem> sortBy = default)
|
||||
{
|
||||
return await Sort(_database.LibraryItems, sortBy).FirstOrDefaultAsync(where).Then(SetBackingImage);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<ICollection<LibraryItem>> GetAll(Expression<Func<LibraryItem, bool>> where = null,
|
||||
Sort<LibraryItem> sort = default,
|
||||
Pagination limit = default)
|
||||
{
|
||||
return (await ApplyFilters(_database.LibraryItems, where, sort, limit))
|
||||
.Select(SetBackingImageSelf).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<int> GetCount(Expression<Func<LibraryItem, bool>> where = null)
|
||||
{
|
||||
IQueryable<LibraryItem> query = _database.LibraryItems;
|
||||
if (where != null)
|
||||
query = query.Where(where);
|
||||
return query.CountAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<ICollection<LibraryItem>> Search(string query)
|
||||
{
|
||||
|
@ -109,6 +109,8 @@ namespace Kyoo.Core.Controllers
|
||||
return _Sort(query, DefaultSort, then);
|
||||
case Sort<T>.By(var key, var desc):
|
||||
return _SortBy(query, x => EF.Property<T>(x, key), desc, then);
|
||||
case Sort<T>.Random:
|
||||
return _SortBy(query, x => EF.Functions.Random(), false, then);
|
||||
case Sort<T>.Conglomerate(var sorts):
|
||||
IOrderedQueryable<T> nQuery = _Sort(query, sorts.First(), false);
|
||||
foreach (Sort<T> sort in sorts.Skip(1))
|
||||
@ -169,6 +171,7 @@ namespace Kyoo.Core.Controllers
|
||||
Sort<T>.Default => GetSortsBy(DefaultSort),
|
||||
Sort<T>.By @sortBy => new[] { sortBy },
|
||||
Sort<T>.Conglomerate(var list) => list.SelectMany(GetSortsBy),
|
||||
Sort<T>.Random => throw new ArgumentException("Impossible to paginate randomly sorted items."),
|
||||
_ => Array.Empty<Sort<T>.By>(),
|
||||
};
|
||||
}
|
||||
@ -306,6 +309,8 @@ namespace Kyoo.Core.Controllers
|
||||
/// <inheritdoc />
|
||||
public virtual Task<T> GetOrDefault(string slug)
|
||||
{
|
||||
if (slug == "random")
|
||||
return Database.Set<T>().OrderBy(x => EF.Functions.Random()).FirstOrDefaultAsync().Then(SetBackingImage);
|
||||
return Database.Set<T>().FirstOrDefaultAsync(x => x.Slug == slug).Then(SetBackingImage);
|
||||
}
|
||||
|
||||
|
@ -113,6 +113,7 @@ namespace Kyoo.Core.Controllers
|
||||
{
|
||||
string directory = item switch
|
||||
{
|
||||
LibraryItem litem => Path.Combine("./metadata", litem.Kind.ToString().ToLowerInvariant(), litem.Slug),
|
||||
IResource res => Path.Combine("./metadata", item.GetType().Name.ToLowerInvariant(), res.Slug),
|
||||
_ => Path.Combine("./metadata", typeof(T).Name.ToLowerInvariant())
|
||||
};
|
||||
|
@ -39,7 +39,7 @@ namespace Kyoo.Core.Api
|
||||
[ResourceView]
|
||||
[PartialPermission("LibraryItem")]
|
||||
[ApiDefinition("Items", Group = ResourcesGroup)]
|
||||
public class LibraryItemApi : BaseApi
|
||||
public class LibraryItemApi : CrudThumbsApi<LibraryItem>
|
||||
{
|
||||
/// <summary>
|
||||
/// The library item repository used to modify or retrieve information in the data store.
|
||||
@ -52,7 +52,8 @@ namespace Kyoo.Core.Api
|
||||
/// <param name="libraryItems">
|
||||
/// The library item repository used to modify or retrieve information in the data store.
|
||||
/// </param>
|
||||
public LibraryItemApi(ILibraryItemRepository libraryItems)
|
||||
public LibraryItemApi(ILibraryItemRepository libraryItems, IThumbnailsManager thumbs)
|
||||
: base(libraryItems, thumbs)
|
||||
{
|
||||
_libraryItems = libraryItems;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user