Restrict Genres and Tags to the users library Fixes #3859
This commit is contained in:
parent
5c8b124b70
commit
d261eaa98f
3 changed files with 117 additions and 34 deletions
|
|
@ -173,9 +173,41 @@ public class GenreRepository : IGenreRepository
|
|||
{
|
||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||
|
||||
var query = _context.Genre
|
||||
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
||||
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
||||
.Select(lib => lib.Id).ToList();
|
||||
|
||||
var query = _context.Genre.RestrictAgainstAgeRestriction(ageRating);
|
||||
|
||||
IQueryable<BrowseGenreDto> finalQuery;
|
||||
if (libs.Count != userLibs.Count)
|
||||
{
|
||||
var seriesIds = _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id);
|
||||
query = query.Where(s => s.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
||||
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)
|
||||
.Select(g => new BrowseGenreDto
|
||||
//.Select(sm => sm.Id)
|
||||
.Distinct()
|
||||
.Count(),
|
||||
ChapterCount = g.Chapters
|
||||
.Where(cp => seriesIds.Contains(cp.Volume.SeriesId))
|
||||
.RestrictAgainstAgeRestriction(ageRating)
|
||||
//.Select(ch => ch.Id)
|
||||
.Distinct()
|
||||
.Count()
|
||||
})
|
||||
.OrderBy(g => g.Title);
|
||||
}
|
||||
else
|
||||
{
|
||||
finalQuery = query.Select(g => new BrowseGenreDto
|
||||
{
|
||||
Id = g.Id,
|
||||
Title = g.Title,
|
||||
|
|
@ -189,7 +221,10 @@ public class GenreRepository : IGenreRepository
|
|||
.Count()
|
||||
})
|
||||
.OrderBy(g => g.Title);
|
||||
}
|
||||
|
||||
return await PagedList<BrowseGenreDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
||||
|
||||
|
||||
return await PagedList<BrowseGenreDto>.CreateAsync(finalQuery, userParams.PageNumber, userParams.PageSize);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,8 +111,42 @@ public class TagRepository : ITagRepository
|
|||
{
|
||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||
|
||||
var query = _context.Tag
|
||||
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
||||
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
||||
.Select(lib => lib.Id).ToList();
|
||||
|
||||
var query = _context.Tag.RestrictAgainstAgeRestriction(ageRating);
|
||||
|
||||
IQueryable<BrowseTagDto> finalQuery;
|
||||
if (userLibs.Count != libs.Count)
|
||||
{
|
||||
var seriesIds = _context.Series.Where(s => userLibs.Contains(s.LibraryId)).Select(s => s.Id);
|
||||
query = query.Where(tag => tag.Chapters.Any(cp => seriesIds.Contains(cp.Volume.SeriesId)) ||
|
||||
tag.SeriesMetadatas.Any(sm => seriesIds.Contains(sm.SeriesId)));
|
||||
|
||||
finalQuery = query
|
||||
.Select(g => new BrowseTagDto
|
||||
{
|
||||
Id = g.Id,
|
||||
Title = g.Title,
|
||||
SeriesCount = g.SeriesMetadatas
|
||||
.Where(sm => seriesIds.Contains(sm.SeriesId))
|
||||
.RestrictAgainstAgeRestriction(ageRating)
|
||||
//.Select(sm => sm.SeriesId)
|
||||
.Distinct()
|
||||
.Count(),
|
||||
ChapterCount = g.Chapters
|
||||
.Where(ch => seriesIds.Contains(ch.Volume.SeriesId))
|
||||
.RestrictAgainstAgeRestriction(ageRating)
|
||||
//.Select(ch => ch.Id)
|
||||
.Distinct()
|
||||
.Count()
|
||||
})
|
||||
.OrderBy(g => g.Title);
|
||||
}
|
||||
else
|
||||
{
|
||||
finalQuery = query
|
||||
.Select(g => new BrowseTagDto
|
||||
{
|
||||
Id = g.Id,
|
||||
|
|
@ -127,8 +161,9 @@ public class TagRepository : ITagRepository
|
|||
.Count()
|
||||
})
|
||||
.OrderBy(g => g.Title);
|
||||
}
|
||||
|
||||
return await PagedList<BrowseTagDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
||||
return await PagedList<BrowseTagDto>.CreateAsync(finalQuery, userParams.PageNumber, userParams.PageSize);
|
||||
}
|
||||
|
||||
public async Task<IList<Tag>> GetAllTagsAsync()
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using API.Data.Misc;
|
||||
using API.Entities;
|
||||
using API.Entities.Enums;
|
||||
using API.Entities.Metadata;
|
||||
|
||||
|
|
@ -55,4 +56,16 @@ public static class EnumerableExtensions
|
|||
|
||||
return q;
|
||||
}
|
||||
|
||||
public static IEnumerable<Chapter> RestrictAgainstAgeRestriction(this IEnumerable<Chapter> items, AgeRestriction restriction)
|
||||
{
|
||||
if (restriction.AgeRating == AgeRating.NotApplicable) return items;
|
||||
var q = items.Where(s => s.AgeRating <= restriction.AgeRating);
|
||||
if (!restriction.IncludeUnknowns)
|
||||
{
|
||||
return q.Where(s => s.AgeRating != AgeRating.Unknown);
|
||||
}
|
||||
|
||||
return q;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue