diff --git a/Shoko.Server/API/v3/Controllers/FilterController.cs b/Shoko.Server/API/v3/Controllers/FilterController.cs index e41148c7a..795a9ccb2 100644 --- a/Shoko.Server/API/v3/Controllers/FilterController.cs +++ b/Shoko.Server/API/v3/Controllers/FilterController.cs @@ -102,68 +102,74 @@ public ActionResult AddNewFilter(Filter.Input.CreateOrUpdateFilterBody b /// Left, Right, Parameter, and SecondParameter show what type the expression supports as parameters. /// Left and Right are Expressions or Selectors. Parameters are constants. /// + /// Optional. The Expression types to return + /// Optional. The Expression groups to return [HttpGet("Expressions")] - public ActionResult GetExpressions() + public ActionResult GetExpressions([FromQuery]Filter.FilterExpressionHelp.FilterExpressionParameterType[] types = null, [FromQuery]FilterExpressionGroup[] groups = null) { + types ??= Array.Empty(); + groups ??= Array.Empty(); + // get all classes that derive from FilterExpression, but not SortingExpression _expressionTypes ??= AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()) - .Where(a => a != typeof(FilterExpression) && !a.IsGenericType && typeof(FilterExpression).IsAssignableFrom(a) && !typeof(SortingExpression).IsAssignableFrom(a)) + .Where(a => a != typeof(FilterExpression) && !a.IsGenericType && typeof(FilterExpression).IsAssignableFrom(a) && + !typeof(SortingExpression).IsAssignableFrom(a)) .OrderBy(a => a.FullName).Select(a => - { - var expression = (FilterExpression)Activator.CreateInstance(a); - if (expression == null) return null; - Filter.FilterExpressionHelp.FilterExpressionParameterType? left = expression switch - { - IWithExpressionParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, - IWithDateSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, - IWithNumberSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, - IWithStringSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, - _ => null - }; - Filter.FilterExpressionHelp.FilterExpressionParameterType? right = expression switch - { - IWithSecondExpressionParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, - IWithSecondDateSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, - IWithSecondNumberSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, - IWithSecondStringSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, - _ => null - }; - Filter.FilterExpressionHelp.FilterExpressionParameterType? parameter = expression switch - { - IWithDateParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Date, - IWithNumberParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Number, - IWithStringParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.String, - IWithTimeSpanParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.TimeSpan, - _ => null - }; - Filter.FilterExpressionHelp.FilterExpressionParameterType? secondParameter = expression switch { - IWithSecondStringParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.String, - _ => null - }; - var type = expression switch - { - FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, - FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, - FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, - FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, - _ => throw new Exception($"Expression {a.Name} is not a handled type for Filter Expression Help") - }; - return new Filter.FilterExpressionHelp - { - Expression = a.Name.TrimEnd("Expression").TrimEnd("Function").TrimEnd("Selector").Trim(), - Name = expression.Name, - Group = expression.Group, - Description = expression.HelpDescription, - PossibleParameters = expression.HelpPossibleParameters, - PossibleSecondParameters = expression.HelpPossibleSecondParameters, - Left = left, - Right = right, - Parameter = parameter, - SecondParameter = secondParameter, - Type = type - }; - }).Where(a => a != null).ToArray(); + var expression = (FilterExpression)Activator.CreateInstance(a); + if (expression == null) return null; + Filter.FilterExpressionHelp.FilterExpressionParameterType? left = expression switch + { + IWithExpressionParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, + IWithDateSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, + IWithNumberSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, + IWithStringSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, + _ => null + }; + Filter.FilterExpressionHelp.FilterExpressionParameterType? right = expression switch + { + IWithSecondExpressionParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, + IWithSecondDateSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, + IWithSecondNumberSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, + IWithSecondStringSelectorParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, + _ => null + }; + Filter.FilterExpressionHelp.FilterExpressionParameterType? parameter = expression switch + { + IWithDateParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Date, + IWithNumberParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.Number, + IWithStringParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.String, + IWithTimeSpanParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.TimeSpan, + _ => null + }; + Filter.FilterExpressionHelp.FilterExpressionParameterType? secondParameter = expression switch + { + IWithSecondStringParameter => Filter.FilterExpressionHelp.FilterExpressionParameterType.String, + _ => null + }; + var type = expression switch + { + FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.Expression, + FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.DateSelector, + FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.NumberSelector, + FilterExpression => Filter.FilterExpressionHelp.FilterExpressionParameterType.StringSelector, + _ => throw new Exception($"Expression {a.Name} is not a handled type for Filter Expression Help") + }; + return new Filter.FilterExpressionHelp + { + Expression = a.Name.TrimEnd("Expression").TrimEnd("Function").TrimEnd("Selector").Trim(), + Name = expression.Name, + Group = expression.Group, + Description = expression.HelpDescription, + PossibleParameters = expression.HelpPossibleParameters, + PossibleSecondParameters = expression.HelpPossibleSecondParameters, + Left = left, + Right = right, + Parameter = parameter, + SecondParameter = secondParameter, + Type = type + }; + }).Where(a => a != null && (types.Length == 0 || types.Contains(a.Type)) && (groups.Length == 0 || groups.Contains(a.Group))).ToArray(); return _expressionTypes; }