Kavita/API/Extensions/QueryExtensions/QueryableExtensions.cs
Joe Milazzo 979508047c
v0.7.8 - New Filtering System (#2260)
Co-authored-by: JeanPaulDOT <jp.houssier@gmail.com>
Co-authored-by: Francois Wilhelmy <ice_mouton@hotmail.com>
Co-authored-by: Gazy Mahomar <gmahomarf@gmail.com>
Co-authored-by: Stijn <stijn.biemans@gmail.com>
Co-authored-by: 無情天 <kofzhanganguo@126.com>
Co-authored-by: Havokdan <havokdan@yahoo.com.br>
Co-authored-by: Andre <andruecha32@gmail.com>
Co-authored-by: Mateusz <mateuszvx8.96@gmail.com>
Co-authored-by: Antonio Sanchez Castellón <angelfx19@gmail.com>
Co-authored-by: Duarte Silva <smallflake@protonmail.com>
Co-authored-by: LeeWan1210 <dldhks456@live.com>
Co-authored-by: aleixcox <18121624@qq.com>
Co-authored-by: Tomas Battistini <tomas.battistini@gmail.com>
Co-authored-by: mareczek82 <marek.posiadala@gmail.com>
Co-authored-by: Hans Kalisvaart <hans.kalisvaart@gmail.com>
Co-authored-by: majora2007 <kavitareader@gmail.com>
Co-authored-by: afermar <adrian.fm@protonmail.com>
Co-authored-by: oxygen44k <iiccpp@outlook.com>
Co-authored-by: Weblate (bot) <hosted@weblate.org>
Co-authored-by: Hadrien b <hadrien.1997@gmail.com>
Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
Co-authored-by: Andre Smith <Hobogrammer@users.noreply.github.com>
Co-authored-by: Safu Wan <safu@yahoo.com>
Co-authored-by: sibeck <sibeck.clown@gmail.com>
Co-authored-by: Florestano Pepe <florestano.pepe@gmail.com>
Co-authored-by: 书签 <shuqian.emu@gmail.com>
Co-authored-by: Stéphane Dupont <aleistor@gmail.com>
Co-authored-by: gallegonovato <fran-carro@hotmail.es>
Co-authored-by: AlienHack <the4got10@windowslive.com>
Co-authored-by: 周書丞 <tmrsm_chan@hotmail.com>
Co-authored-by: Andre Smith <andrepsmithjr@gmail.com>
Co-authored-by: xe1st <dnzkckali@gmail.com>
Co-authored-by: Jiří Heger <jiri.heger@gmail.com>
Co-authored-by: DR <weblate-kavita.snowflake668@slmail.me>
Co-authored-by: Mathieu Ares <matguitarist@gmail.com>
Co-authored-by: Stavros Kois <47820033+stavros-k@users.noreply.github.com>
Co-authored-by: Gazy Mahomar <gmahomarf@users.noreply.github.com>
Co-authored-by: Elias Jakob <elias.jakob100@gmail.com>
Co-authored-by: Christian Zanon <chri8431@libero.it>
Co-authored-by: Eryk Michalak <gnu.ewm@protonmail.com>
Co-authored-by: Hoshino0881118 <hoshino0881118@gmail.com>
2023-09-03 12:31:50 -07:00

189 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using API.Data.Misc;
using API.Data.Repositories;
using API.Entities;
using API.Entities.Enums;
using API.Entities.Scrobble;
using Microsoft.EntityFrameworkCore;
namespace API.Extensions.QueryExtensions;
public static class QueryableExtensions
{
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>
/// Applies restriction based on if the Library has restrictions (like include in search)
/// </summary>
/// <param name="query"></param>
/// <param name="context"></param>
/// <returns></returns>
public static IQueryable<Library> IsRestricted(this IQueryable<Library> query, QueryContext context)
{
if (context.HasFlag(QueryContext.None)) return query;
if (context.HasFlag(QueryContext.Dashboard))
{
query = query.Where(l => l.IncludeInDashboard);
}
if (context.HasFlag(QueryContext.Recommended))
{
query = query.Where(l => l.IncludeInRecommended);
}
if (context.HasFlag(QueryContext.Search))
{
query = query.Where(l => l.IncludeInSearch);
}
return query;
}
/// <summary>
/// Returns all libraries for a given user
/// </summary>
/// <param name="library"></param>
/// <param name="userId"></param>
/// <param name="queryContext"></param>
/// <returns></returns>
public static IQueryable<int> GetUserLibraries(this IQueryable<Library> library, int userId, QueryContext queryContext = QueryContext.None)
{
return library
.Include(l => l.AppUsers)
.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
.IsRestricted(queryContext)
.AsNoTracking()
.AsSplitQuery()
.Select(lib => lib.Id);
}
/// <summary>
/// Returns all libraries for a given user and library type
/// </summary>
/// <param name="library"></param>
/// <param name="userId"></param>
/// <param name="queryContext"></param>
/// <returns></returns>
public static IQueryable<int> GetUserLibrariesByType(this IQueryable<Library> library, int userId, LibraryType type, QueryContext queryContext = QueryContext.None)
{
return library
.Include(l => l.AppUsers)
.Where(lib => lib.AppUsers.Any(user => user.Id == userId))
.Where(lib => lib.Type == type)
.IsRestricted(queryContext)
.AsNoTracking()
.AsSplitQuery()
.Select(lib => lib.Id);
}
public static IEnumerable<DateTime> Range(this DateTime startDate, int numberOfDays) =>
Enumerable.Range(0, numberOfDays).Select(e => startDate.AddDays(e));
public static IQueryable<T> WhereIf<T>(this IQueryable<T> queryable, bool condition,
Expression<Func<T, bool>> predicate)
{
return condition ? queryable.Where(predicate) : queryable;
}
public static IQueryable<T> WhereLike<T>(this IQueryable<T> queryable, bool condition, Expression<Func<T, string>> propertySelector, string searchQuery)
where T : class
{
if (!condition || string.IsNullOrEmpty(searchQuery)) return queryable;
var method = typeof(DbFunctionsExtensions).GetMethod(nameof(DbFunctionsExtensions.Like), new[] { typeof(DbFunctions), typeof(string), typeof(string) });
var dbFunctions = typeof(EF).GetMethod(nameof(EF.Functions))?.Invoke(null, null);
var searchExpression = Expression.Constant($"%{searchQuery}%");
var likeExpression = Expression.Call(method, Expression.Constant(dbFunctions), propertySelector.Body, searchExpression);
var lambda = Expression.Lambda<Func<T, bool>>(likeExpression, propertySelector.Parameters[0]);
return queryable.Where(lambda);
}
/// <summary>
/// Performs a WhereLike that ORs multiple fields
/// </summary>
/// <param name="queryable"></param>
/// <param name="propertySelectors"></param>
/// <param name="searchQuery"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IQueryable<T> WhereLike<T>(this IQueryable<T> queryable, bool condition, List<Expression<Func<T, string>>> propertySelectors, string searchQuery)
where T : class
{
if (!condition || string.IsNullOrEmpty(searchQuery)) return queryable;
var method = typeof(DbFunctionsExtensions).GetMethod(nameof(DbFunctionsExtensions.Like), new[] { typeof(DbFunctions), typeof(string), typeof(string) });
var dbFunctions = typeof(EF).GetMethod(nameof(EF.Functions))?.Invoke(null, null);
var searchExpression = Expression.Constant($"%{searchQuery}%");
Expression orExpression = null;
foreach (var propertySelector in propertySelectors)
{
var likeExpression = Expression.Call(method, Expression.Constant(dbFunctions), propertySelector.Body, searchExpression);
var lambda = Expression.Lambda<Func<T, bool>>(likeExpression, propertySelector.Parameters[0]);
orExpression = orExpression == null ? lambda.Body : Expression.OrElse(orExpression, lambda.Body);
}
if (orExpression == null)
{
throw new ArgumentNullException(nameof(orExpression));
}
var combinedLambda = Expression.Lambda<Func<T, bool>>(orExpression, propertySelectors[0].Parameters[0]);
return queryable.Where(combinedLambda);
}
public static IQueryable<ScrobbleEvent> SortBy(this IQueryable<ScrobbleEvent> query, ScrobbleEventSortField sort, bool isDesc = false)
{
if (isDesc)
{
return sort switch
{
ScrobbleEventSortField.None => query,
ScrobbleEventSortField.Created => query.OrderByDescending(s => s.Created),
ScrobbleEventSortField.LastModified => query.OrderByDescending(s => s.LastModified),
ScrobbleEventSortField.Type => query.OrderByDescending(s => s.ScrobbleEventType),
ScrobbleEventSortField.Series => query.OrderByDescending(s => s.Series.NormalizedName),
ScrobbleEventSortField.IsProcessed => query.OrderByDescending(s => s.IsProcessed),
_ => query
};
}
return sort switch
{
ScrobbleEventSortField.None => query,
ScrobbleEventSortField.Created => query.OrderBy(s => s.Created),
ScrobbleEventSortField.LastModified => query.OrderBy(s => s.LastModified),
ScrobbleEventSortField.Type => query.OrderBy(s => s.ScrobbleEventType),
ScrobbleEventSortField.Series => query.OrderBy(s => s.Series.NormalizedName),
ScrobbleEventSortField.IsProcessed => query.OrderBy(s => s.IsProcessed),
_ => query
};
}
}