130 lines
4.4 KiB
C#
130 lines
4.4 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data.Misc;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace API.Extensions;
|
|
|
|
public static class QueryableExtensions
|
|
{
|
|
public static IQueryable<Series> RestrictAgainstAgeRestriction(this IQueryable<Series> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
var q = queryable.Where(s => s.Metadata.AgeRating <= restriction.AgeRating);
|
|
if (!restriction.IncludeUnknowns)
|
|
{
|
|
return q.Where(s => s.Metadata.AgeRating != AgeRating.Unknown);
|
|
}
|
|
|
|
return q;
|
|
}
|
|
|
|
public static IQueryable<CollectionTag> RestrictAgainstAgeRestriction(this IQueryable<CollectionTag> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
|
|
if (restriction.IncludeUnknowns)
|
|
{
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating));
|
|
}
|
|
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating && sm.AgeRating > AgeRating.Unknown));
|
|
}
|
|
|
|
public static IQueryable<Genre> RestrictAgainstAgeRestriction(this IQueryable<Genre> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
|
|
if (restriction.IncludeUnknowns)
|
|
{
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating));
|
|
}
|
|
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating && sm.AgeRating > AgeRating.Unknown));
|
|
}
|
|
|
|
public static IQueryable<Tag> RestrictAgainstAgeRestriction(this IQueryable<Tag> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
|
|
if (restriction.IncludeUnknowns)
|
|
{
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating));
|
|
}
|
|
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating && sm.AgeRating > AgeRating.Unknown));
|
|
}
|
|
|
|
public static IQueryable<Person> RestrictAgainstAgeRestriction(this IQueryable<Person> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
|
|
if (restriction.IncludeUnknowns)
|
|
{
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating));
|
|
}
|
|
|
|
return queryable.Where(c => c.SeriesMetadatas.All(sm =>
|
|
sm.AgeRating <= restriction.AgeRating && sm.AgeRating > AgeRating.Unknown));
|
|
}
|
|
|
|
public static IQueryable<ReadingList> RestrictAgainstAgeRestriction(this IQueryable<ReadingList> queryable, AgeRestriction restriction)
|
|
{
|
|
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
|
var q = queryable.Where(rl => rl.AgeRating <= restriction.AgeRating);
|
|
|
|
if (!restriction.IncludeUnknowns)
|
|
{
|
|
return q.Where(rl => rl.AgeRating != AgeRating.Unknown);
|
|
}
|
|
|
|
return q;
|
|
}
|
|
|
|
public static Task<AgeRestriction> GetUserAgeRestriction(this DbSet<AppUser> queryable, int userId)
|
|
{
|
|
if (userId < 1)
|
|
{
|
|
return Task.FromResult(new AgeRestriction()
|
|
{
|
|
AgeRating = AgeRating.NotApplicable,
|
|
IncludeUnknowns = true
|
|
});
|
|
}
|
|
return queryable
|
|
.AsNoTracking()
|
|
.Where(u => u.Id == userId)
|
|
.Select(u =>
|
|
new AgeRestriction(){
|
|
AgeRating = u.AgeRestriction,
|
|
IncludeUnknowns = u.AgeRestrictionIncludeUnknowns
|
|
})
|
|
.SingleAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Only applies the Take if it's greater than 0
|
|
/// </summary>
|
|
/// <param name="queryable"></param>
|
|
/// <param name="takeAmt"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IQueryable<T> TakeIfGreaterThan0<T>(this IQueryable<T> queryable, int takeAmt)
|
|
{
|
|
if (takeAmt > 0)
|
|
{
|
|
return queryable.Take(takeAmt);
|
|
}
|
|
|
|
return queryable;
|
|
}
|
|
}
|