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,23 +173,58 @@ public class GenreRepository : IGenreRepository
|
||||||
{
|
{
|
||||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||||
|
|
||||||
var query = _context.Genre
|
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
||||||
.Select(g => new BrowseGenreDto
|
.Select(lib => lib.Id).ToList();
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.Select(sm => sm.Id)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.Select(ch => ch.Id)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
|
|
||||||
return await PagedList<BrowseGenreDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
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(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,
|
||||||
|
SeriesCount = g.SeriesMetadatas
|
||||||
|
.Select(sm => sm.Id)
|
||||||
|
.Distinct()
|
||||||
|
.Count(),
|
||||||
|
ChapterCount = g.Chapters
|
||||||
|
.Select(ch => ch.Id)
|
||||||
|
.Distinct()
|
||||||
|
.Count()
|
||||||
|
})
|
||||||
|
.OrderBy(g => g.Title);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return await PagedList<BrowseGenreDto>.CreateAsync(finalQuery, userParams.PageNumber, userParams.PageSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -111,24 +111,59 @@ public class TagRepository : ITagRepository
|
||||||
{
|
{
|
||||||
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
var ageRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||||
|
|
||||||
var query = _context.Tag
|
var libs = await _context.Library.Includes(LibraryIncludes.AppUser).ToListAsync();
|
||||||
.RestrictAgainstAgeRestriction(ageRating)
|
var userLibs = libs.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
|
||||||
.Select(g => new BrowseTagDto
|
.Select(lib => lib.Id).ToList();
|
||||||
{
|
|
||||||
Id = g.Id,
|
|
||||||
Title = g.Title,
|
|
||||||
SeriesCount = g.SeriesMetadatas
|
|
||||||
.Select(sm => sm.Id)
|
|
||||||
.Distinct()
|
|
||||||
.Count(),
|
|
||||||
ChapterCount = g.Chapters
|
|
||||||
.Select(ch => ch.Id)
|
|
||||||
.Distinct()
|
|
||||||
.Count()
|
|
||||||
})
|
|
||||||
.OrderBy(g => g.Title);
|
|
||||||
|
|
||||||
return await PagedList<BrowseTagDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
|
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,
|
||||||
|
Title = g.Title,
|
||||||
|
SeriesCount = g.SeriesMetadatas
|
||||||
|
.Select(sm => sm.Id)
|
||||||
|
.Distinct()
|
||||||
|
.Count(),
|
||||||
|
ChapterCount = g.Chapters
|
||||||
|
.Select(ch => ch.Id)
|
||||||
|
.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()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using API.Data.Misc;
|
using API.Data.Misc;
|
||||||
|
using API.Entities;
|
||||||
using API.Entities.Enums;
|
using API.Entities.Enums;
|
||||||
using API.Entities.Metadata;
|
using API.Entities.Metadata;
|
||||||
|
|
||||||
|
|
@ -55,4 +56,16 @@ public static class EnumerableExtensions
|
||||||
|
|
||||||
return q;
|
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