Skip to content

Commit

Permalink
Fix APIv3 Filter Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Oct 6, 2023
1 parent 0aa2ab9 commit 265cdda
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
46 changes: 29 additions & 17 deletions Shoko.Server/API/v3/Helpers/FilterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ namespace Shoko.Server.API.v3.Helpers;

public class FilterFactory
{
private readonly Dictionary<string, Type> _expressionTypes;
private readonly Dictionary<string, Type> _sortingTypes;
private readonly HttpContext _context;
private readonly FilterEvaluator _evaluator;

public FilterFactory(IHttpContextAccessor context, FilterEvaluator evaluator)
{
_context = context.HttpContext;
_evaluator = evaluator;

_expressionTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
.Where(a => a != typeof(FilterExpression) && !a.IsGenericType && typeof(FilterExpression).IsAssignableFrom(a) &&
!typeof(SortingExpression).IsAssignableFrom(a)).ToDictionary(a => a.Name.Replace("Expression", "").Replace("Function", ""));

_sortingTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
.Where(a => a != typeof(FilterExpression) && !a.IsAbstract && !a.IsGenericType && typeof(SortingExpression).IsAssignableFrom(a))
.ToDictionary(a => a.Name.Replace("SortingSelector", ""));
}

public Filter GetFilter(FilterPreset groupFilter, bool fullModel = false)
Expand Down Expand Up @@ -86,7 +96,7 @@ public IEnumerable<Filter> GetFilters(List<FilterPreset> groupFilters, bool full
return filters;
}

public static Filter.FilterCondition GetExpressionTree(FilterExpression expression)
public Filter.FilterCondition GetExpressionTree(FilterExpression expression)
{
if (expression is null) return null;
var result = new Filter.FilterCondition
Expand Down Expand Up @@ -118,13 +128,13 @@ public static Filter.FilterCondition GetExpressionTree(FilterExpression expressi
result.Parameter = parameter.Parameter;
break;
case IWithNumberParameter parameter:
result.Parameter = parameter.Parameter.ToString();
result.Parameter = parameter.Parameter == 0 ? null : parameter.Parameter.ToString(CultureInfo.CurrentCulture);
break;
case IWithDateParameter parameter:
result.Parameter = parameter.Parameter.ToString("yyyy-MM-dd");
result.Parameter = parameter.Parameter == default ? null : parameter.Parameter.ToString("yyyy-MM-dd");
break;
case IWithTimeSpanParameter parameter:
result.Parameter = parameter.Parameter.ToString("G");
result.Parameter = parameter.Parameter == default ? null : parameter.Parameter.ToString("G");
break;
}

Expand Down Expand Up @@ -153,10 +163,7 @@ public static Filter.FilterCondition GetExpressionTree(FilterExpression expressi

public FilterExpression<T> GetExpressionTree<T>(Filter.FilterCondition condition)
{
var type = Type.GetType(condition.Type + "Expression");
type ??= Type.GetType(condition.Type + "Function");
type ??= Type.GetType(condition.Type);
if (type == null) throw new ArgumentException($"FilterCondition type {condition.Type} was not found");
if (!_expressionTypes.TryGetValue(condition.Type, out var type)) throw new ArgumentException($"FilterCondition type {condition.Type} was not found");
var result = (FilterExpression<T>)Activator.CreateInstance(type);

// Left/First
Expand All @@ -180,16 +187,20 @@ public FilterExpression<T> GetExpressionTree<T>(Filter.FilterCondition condition
switch (result)
{
case IWithStringParameter parameter:
parameter.Parameter = parameter.Parameter;
parameter.Parameter = condition.Parameter;
break;
case IWithNumberParameter parameter:
parameter.Parameter = double.Parse(condition.Parameter!);
parameter.Parameter = string.IsNullOrEmpty(condition.Parameter) ? default : double.Parse(condition.Parameter!);
break;
case IWithDateParameter parameter:
parameter.Parameter = DateTime.ParseExact(condition.Parameter!, "yyyy-MM-dd", CultureInfo.InvariantCulture.DateTimeFormat);
parameter.Parameter = string.IsNullOrEmpty(condition.Parameter)
? default
: DateTime.ParseExact(condition.Parameter!, "yyyy-MM-dd", CultureInfo.InvariantCulture.DateTimeFormat);
break;
case IWithTimeSpanParameter parameter:
parameter.Parameter = TimeSpan.ParseExact(condition.Parameter!, "G", CultureInfo.InvariantCulture.DateTimeFormat);
parameter.Parameter = string.IsNullOrEmpty(condition.Parameter)
? default
: TimeSpan.ParseExact(condition.Parameter!, "G", CultureInfo.InvariantCulture.DateTimeFormat);
break;
}

Expand All @@ -216,8 +227,10 @@ public FilterExpression<T> GetExpressionTree<T>(Filter.FilterCondition condition
return result;
}

public static Filter.SortingCriteria GetSortingCriteria(SortingExpression expression)
public Filter.SortingCriteria GetSortingCriteria(SortingExpression expression)
{
if (expression == null) return new Filter.SortingCriteria { Type = "Name", IsInverted = false };

var result = new Filter.SortingCriteria
{
Type = expression.GetType().Name.Replace("SortingSelector", ""),
Expand All @@ -240,11 +253,10 @@ public static Filter.SortingCriteria GetSortingCriteria(SortingExpression expres
return result;
}

public static SortingExpression GetSortingCriteria(Filter.SortingCriteria criteria)
public SortingExpression GetSortingCriteria(Filter.SortingCriteria criteria)
{
var type = Type.GetType(criteria.Type + "SortingSelector");
type ??= Type.GetType(criteria.Type);
if (type == null) throw new ArgumentException($"SortingExpression type {criteria.Type}Selector was not found");
if (!_sortingTypes.TryGetValue(criteria.Type, out var type))
throw new ArgumentException($"SortingExpression type {criteria.Type}Selector was not found");
var result = (SortingExpression)Activator.CreateInstance(type)!;
result.Descending = criteria.IsInverted;

Expand Down
1 change: 0 additions & 1 deletion Shoko.Server/Filters/Legacy/LegacyConditionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Shoko.Server.Filters.Functions;
using Shoko.Server.Filters.Info;
using Shoko.Server.Filters.Interfaces;
using Shoko.Server.Filters.Logic;
using Shoko.Server.Filters.Logic.DateTimes;
using Shoko.Server.Filters.Logic.Expressions;
using Shoko.Server.Filters.Logic.Numbers;
Expand Down

0 comments on commit 265cdda

Please sign in to comment.