Reworking show's link creations

This commit is contained in:
Zoe Roux 2021-03-02 19:07:40 +01:00
parent fef6a93a1d
commit b8fcc5b7cf
2 changed files with 25 additions and 20 deletions

View File

@ -71,7 +71,7 @@ namespace Kyoo.Controllers
private IQueryable<LibraryItem> ItemsQuery
=> _database.Shows
.Where(x => !_database.CollectionLinks.Any(y => y.ChildID == x.ID))
.Where(x => !x.Collections.Any())
.Select(LibraryItem.FromShow)
.Concat(_database.Collections
.Select(LibraryItem.FromCollection));
@ -91,7 +91,7 @@ namespace Kyoo.Controllers
return query.CountAsync();
}
public async Task<ICollection<LibraryItem>> Search(string query)
public override async Task<ICollection<LibraryItem>> Search(string query)
{
return await ItemsQuery
.Where(x => EF.Functions.ILike(x.Title, $"%{query}%"))
@ -113,17 +113,15 @@ namespace Kyoo.Controllers
public override Task Delete(string slug) => throw new InvalidOperationException();
public override Task Delete(LibraryItem obj) => throw new InvalidOperationException();
private IQueryable<LibraryItem> LibraryRelatedQuery(Expression<Func<LibraryLink, bool>> selector)
=> _database.LibraryLinks
private IQueryable<LibraryItem> LibraryRelatedQuery(Expression<Func<Library, bool>> selector)
=> _database.Libraries
.Where(selector)
.Select(x => x.Show)
.Where(x => x != null)
.Where(x => !_database.CollectionLinks.Any(y => y.ChildID == x.ID))
.SelectMany(x => x.Shows)
.Where(x => !x.Collections.Any())
.Select(LibraryItem.FromShow)
.Concat(_database.LibraryLinks
.Concat(_database.Libraries
.Where(selector)
.Select(x => x.Collection)
.Where(x => x != null)
.SelectMany(x => x.Collections)
.Select(LibraryItem.FromCollection));
public async Task<ICollection<LibraryItem>> GetFromLibrary(int id,
@ -131,7 +129,7 @@ namespace Kyoo.Controllers
Sort<LibraryItem> sort = default,
Pagination limit = default)
{
ICollection<LibraryItem> items = await ApplyFilters(LibraryRelatedQuery(x => x.LibraryID == id),
ICollection<LibraryItem> items = await ApplyFilters(LibraryRelatedQuery(x => x.ID == id),
where,
sort,
limit);
@ -145,7 +143,7 @@ namespace Kyoo.Controllers
Sort<LibraryItem> sort = default,
Pagination limit = default)
{
ICollection<LibraryItem> items = await ApplyFilters(LibraryRelatedQuery(x => x.Library.Slug == slug),
ICollection<LibraryItem> items = await ApplyFilters(LibraryRelatedQuery(x => x.Slug == slug),
where,
sort,
limit);

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@ -78,7 +79,7 @@ namespace Kyoo.Controllers
|| EF.Functions.ILike(x.Slug, query)
/*|| x.Aliases.Any(y => EF.Functions.ILike(y, query))*/) // NOT TRANSLATABLE.
.Take(20)
.ToListAsync<Show>();
.ToListAsync();
}
public override async Task<Show> Create(Show obj)
@ -121,22 +122,28 @@ namespace Kyoo.Controllers
public async Task AddShowLink(int showID, int? libraryID, int? collectionID)
{
Show show = await Get(showID);
if (collectionID != null)
{
await _database.CollectionLinks.AddAsync(new CollectionLink {ParentID = collectionID.Value, ChildID = showID});
await _database.SaveIfNoDuplicates();
show.Collections ??= new List<Collection>();
show.Collections.Add(new Collection {ID = collectionID.Value});
await _database.SaveChangesAsync();
}
if (libraryID != null)
{
await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, ShowID = showID});
await _database.SaveIfNoDuplicates();
show.Libraries ??= new List<Library>();
show.Libraries.Add(new Library {ID = libraryID.Value});
await _database.SaveChangesAsync();
}
if (libraryID != null && collectionID != null)
{
await _database.LibraryLinks.AddAsync(new LibraryLink {LibraryID = libraryID.Value, CollectionID = collectionID.Value});
await _database.SaveIfNoDuplicates();
Library library = await _database.Libraries.FirstOrDefaultAsync(x => x.ID == libraryID.Value);
if (library == null)
throw new ItemNotFound($"No library found with the ID {libraryID.Value}");
library.Collections ??= new List<Collection>();
library.Collections.Add(new Collection {ID = collectionID.Value});
await _database.SaveChangesAsync();
}
}