Simply code, moving to my PC
This commit is contained in:
parent
ff1cc72c48
commit
f26b68351c
4 changed files with 74 additions and 160 deletions
|
|
@ -174,59 +174,32 @@ public class GenreRepository : IGenreRepository
|
||||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||||
|
|
||||||
var allLibrariesCount = await _context.Library.CountAsync();
|
var allLibrariesCount = await _context.Library.CountAsync();
|
||||||
var userLibs = await _context.Library
|
var userLibs = await _context.Library.GetUserLibraries(userId).ToListAsync();
|
||||||
.Includes(LibraryIncludes.AppUser)
|
|
||||||
.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
|
||||||
.Select(lib => lib.Id)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
var query = _context.Genre.RestrictAgainstAgeRestriction(ageRating);
|
var seriesIds = await _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id).ToListAsync();
|
||||||
|
|
||||||
IQueryable<BrowseGenreDto> finalQuery;
|
var query = _context.Genre
|
||||||
var seriesIds = _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id);
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
if (allLibrariesCount != userLibs.Count)
|
.WhereIf(allLibrariesCount != userLibs.Count,
|
||||||
{
|
genre => genre.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
||||||
|
genre.SeriesMetadatas.Any(sm => seriesIds.Contains(sm.SeriesId)))
|
||||||
|
.Select(g => new BrowseGenreDto
|
||||||
|
{
|
||||||
|
Id = g.Id,
|
||||||
|
Title = g.Title,
|
||||||
|
SeriesCount = g.SeriesMetadatas
|
||||||
|
.Where(sm => allLibrariesCount == userLibs.Count || seriesIds.Contains(sm.SeriesId))
|
||||||
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
|
.Distinct()
|
||||||
|
.Count(),
|
||||||
|
ChapterCount = g.Chapters
|
||||||
|
.Where(cp => allLibrariesCount == userLibs.Count || seriesIds.Contains(cp.Volume.SeriesId))
|
||||||
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
|
.Distinct()
|
||||||
|
.Count(),
|
||||||
|
})
|
||||||
|
.OrderBy(g => g.Title);
|
||||||
|
|
||||||
query = query.Where(s => s.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
return await PagedList<BrowseGenreDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
||||||
s.SeriesMetadatas.Any(sm => seriesIds.Contains(sm.SeriesId)));
|
|
||||||
|
|
||||||
finalQuery = query.Select(g => new BrowseGenreDto
|
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.Where(sm => seriesIds.Contains(sm.SeriesId))
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.Where(cp => seriesIds.Contains(cp.Volume.SeriesId))
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
finalQuery = query.Select(g => new BrowseGenreDto
|
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return await PagedList<BrowseGenreDto>.CreateAsync(finalQuery, userParams.PageNumber, userParams.PageSize);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -216,10 +216,8 @@ public class PersonRepository : IPersonRepository
|
||||||
|
|
||||||
private async Task<IQueryable<BrowsePersonDto>> CreateFilteredPersonQueryable(int userId, BrowsePersonFilterDto filter, AgeRestriction ageRating)
|
private async Task<IQueryable<BrowsePersonDto>> CreateFilteredPersonQueryable(int userId, BrowsePersonFilterDto filter, AgeRestriction ageRating)
|
||||||
{
|
{
|
||||||
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
var allLibrariesCount = await _context.Library.CountAsync();
|
||||||
var libIds = libs.Select(l => l.Id).ToList();
|
var userLibs = await _context.Library.GetUserLibraries(userId).ToListAsync();
|
||||||
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId)).Select(lib => lib.Id).ToList();
|
|
||||||
var shouldLibRestrict = libIds.Count != userLibs.Count;
|
|
||||||
|
|
||||||
var seriesIds = await _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id).ToListAsync();
|
var seriesIds = await _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id).ToListAsync();
|
||||||
|
|
||||||
|
|
@ -229,64 +227,35 @@ public class PersonRepository : IPersonRepository
|
||||||
query = BuildPersonFilterQuery(userId, filter, query);
|
query = BuildPersonFilterQuery(userId, filter, query);
|
||||||
|
|
||||||
// Apply restrictions
|
// Apply restrictions
|
||||||
query = query.RestrictAgainstAgeRestriction(ageRating);
|
query = query.RestrictAgainstAgeRestriction(ageRating)
|
||||||
|
.WhereIf(allLibrariesCount != userLibs.Count,
|
||||||
if (shouldLibRestrict)
|
person => person.ChapterPeople.Any(cp => seriesIds.Contains(cp.Chapter.Volume.SeriesId)) ||
|
||||||
{
|
person.SeriesMetadataPeople.Any(smp => seriesIds.Contains(smp.SeriesMetadata.SeriesId)));
|
||||||
query = query.Where(p => p.ChapterPeople.Any(cp => seriesIds.Contains(cp.Chapter.Volume.SeriesId)) ||
|
|
||||||
p.SeriesMetadataPeople.Any(smp => seriesIds.Contains(smp.SeriesMetadata.SeriesId)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply sorting and limiting
|
// Apply sorting and limiting
|
||||||
var sortedQuery = query.SortBy(filter.SortOptions);
|
var sortedQuery = query.SortBy(filter.SortOptions);
|
||||||
|
|
||||||
var limitedQuery = ApplyPersonLimit(sortedQuery, filter.LimitTo);
|
var limitedQuery = ApplyPersonLimit(sortedQuery, filter.LimitTo);
|
||||||
|
|
||||||
IQueryable<BrowsePersonDto> projectedQuery;
|
return limitedQuery.Select(p => new BrowsePersonDto
|
||||||
if (shouldLibRestrict)
|
|
||||||
{
|
{
|
||||||
projectedQuery = limitedQuery.Select(p => new BrowsePersonDto
|
Id = p.Id,
|
||||||
{
|
Name = p.Name,
|
||||||
Id = p.Id,
|
Description = p.Description,
|
||||||
Name = p.Name,
|
CoverImage = p.CoverImage,
|
||||||
Description = p.Description,
|
SeriesCount = p.SeriesMetadataPeople
|
||||||
CoverImage = p.CoverImage,
|
.Select(smp => smp.SeriesMetadata)
|
||||||
SeriesCount = p.SeriesMetadataPeople
|
.Where(sm => allLibrariesCount == userLibs.Count || seriesIds.Contains(sm.SeriesId))
|
||||||
.Select(smp => smp.SeriesMetadata)
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
.Where(sm => seriesIds.Contains(sm.SeriesId))
|
.Distinct()
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
.Count(),
|
||||||
.Distinct()
|
ChapterCount = p.ChapterPeople
|
||||||
.Count(),
|
.Select(chp => chp.Chapter)
|
||||||
ChapterCount = p.ChapterPeople
|
.Where(ch => allLibrariesCount == userLibs.Count || seriesIds.Contains(ch.Volume.SeriesId))
|
||||||
.Select(chp => chp.Chapter)
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
.Where(ch => seriesIds.Contains(ch.Volume.SeriesId))
|
.Distinct()
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
.Count(),
|
||||||
.Distinct()
|
});
|
||||||
.Count()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
projectedQuery = limitedQuery.Select(p => new BrowsePersonDto
|
|
||||||
{
|
|
||||||
Id = p.Id,
|
|
||||||
Name = p.Name,
|
|
||||||
Description = p.Description,
|
|
||||||
CoverImage = p.CoverImage,
|
|
||||||
SeriesCount = p.SeriesMetadataPeople
|
|
||||||
.Select(smp => smp.SeriesMetadata)
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = p.ChapterPeople
|
|
||||||
.Select(chp => chp.Chapter)
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return projectedQuery;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IQueryable<Person> BuildPersonFilterQuery(int userId, BrowsePersonFilterDto filterDto, IQueryable<Person> query)
|
private static IQueryable<Person> BuildPersonFilterQuery(int userId, BrowsePersonFilterDto filterDto, IQueryable<Person> query)
|
||||||
|
|
|
||||||
|
|
@ -111,57 +111,34 @@ public class TagRepository : ITagRepository
|
||||||
{
|
{
|
||||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||||
|
|
||||||
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
var allLibrariesCount = await _context.Library.CountAsync();
|
||||||
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
var userLibs = await _context.Library.GetUserLibraries(userId).ToListAsync();
|
||||||
.Select(lib => lib.Id).ToList();
|
|
||||||
|
|
||||||
var query = _context.Tag.RestrictAgainstAgeRestriction(ageRating);
|
var seriesIds = _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id);
|
||||||
|
|
||||||
IQueryable<BrowseTagDto> finalQuery;
|
var query = _context.Tag
|
||||||
if (userLibs.Count != libs.Count)
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
{
|
.WhereIf(userLibs.Count != allLibrariesCount,
|
||||||
var seriesIds = _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id);
|
tag => tag.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
||||||
query = query.Where(tag => tag.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
tag.SeriesMetadatas.Any(sm => seriesIds.Contains(sm.SeriesId)))
|
||||||
tag.SeriesMetadatas.Any(sm => seriesIds.Contains(sm.SeriesId)));
|
.Select(g => new BrowseTagDto
|
||||||
|
{
|
||||||
|
Id = g.Id,
|
||||||
|
Title = g.Title,
|
||||||
|
SeriesCount = g.SeriesMetadatas
|
||||||
|
.Where(sm => allLibrariesCount == userLibs.Count || seriesIds.Contains(sm.SeriesId))
|
||||||
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
|
.Distinct()
|
||||||
|
.Count(),
|
||||||
|
ChapterCount = g.Chapters
|
||||||
|
.Where(ch => allLibrariesCount == userLibs.Count || seriesIds.Contains(ch.Volume.SeriesId))
|
||||||
|
.RestrictAgainstAgeRestriction(ageRating)
|
||||||
|
.Distinct()
|
||||||
|
.Count()
|
||||||
|
})
|
||||||
|
.OrderBy(g => g.Title);
|
||||||
|
|
||||||
finalQuery = query
|
return await PagedList<BrowseTagDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
||||||
.Select(g => new BrowseTagDto
|
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.Where(sm => seriesIds.Contains(sm.SeriesId))
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.Where(ch => seriesIds.Contains(ch.Volume.SeriesId))
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
finalQuery = query
|
|
||||||
.Select(g => new BrowseTagDto
|
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await PagedList<BrowseTagDto>.CreateAsync(finalQuery, userParams.PageNumber, userParams.PageSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IList<Tag>> GetAllTagsAsync()
|
public async Task<IList<Tag>> GetAllTagsAsync()
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
using System.Collections.Generic;
|
using System.Linq;
|
||||||
using System.Linq;
|
|
||||||
using API.Entities;
|
using API.Entities;
|
||||||
using API.Entities.Person;
|
using API.Entities.Person;
|
||||||
|
|
||||||
namespace API.Extensions.QueryExtensions;
|
namespace API.Extensions.QueryExtensions;
|
||||||
|
|
||||||
// TODO: Refactor with IQueryable userLibs? But then I can't do the allLibs check?
|
|
||||||
/// <summary>
|
|
||||||
/// Optionally pass ids of all libraries, will then be smart and not restrict if the person has access to all
|
|
||||||
/// </summary>
|
|
||||||
public static class RestrictByLibraryExtensions
|
public static class RestrictByLibraryExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue