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

View File

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