You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// public class QueryFilterAttribute<T>(string propertyName, Type propertyType) : Attribute, IActionFilter
18
+
// {
19
+
// /// <summary>
20
+
// /// Called before the action method executes. No operation is performed in this implementation.
21
+
// /// </summary>
22
+
// /// <param name="context">The context for the executed action.</param>
23
+
// public void OnActionExecuting(ActionExecutingContext context) { }
24
+
//
25
+
// /// <summary>
26
+
// /// Executes after the action method is invoked. Filters the result based on the specified property and enum value.
27
+
// /// </summary>
28
+
// /// <param name="context">The context for the action filter.</param>
29
+
// public void OnActionExecuted(ActionExecutedContext context)
30
+
// {
31
+
// if (!propertyType.IsEnum)
32
+
// throw new ApiException(HttpStatusCode.BadRequest, $"QueryFilter only accepts enum types for filters. The property '{propertyName}' is not an enum.");
33
+
//
34
+
// if (context.Result is not ObjectResult { Value: IEnumerable<T> enumerable } objectResult) return;
35
+
//
36
+
// var query = context.HttpContext.Request.Query;
37
+
// if (!query.TryGetValue(propertyName, out var parameterValue)) return;
38
+
//
39
+
// if (StringValues.IsNullOrEmpty(parameterValue))
40
+
// throw new ApiException(HttpStatusCode.BadRequest, "Query parameter is required but was not provided.");
41
+
//
42
+
// if (!Enum.TryParse(propertyType, parameterValue, ignoreCase: true, out var enumValue))
43
+
// throw new ApiException(HttpStatusCode.BadRequest, $"Invalid query parameter value: `{parameterValue}` for `{propertyName}`. Allowed values are: {string.Join(", ", Enum.GetNames(propertyType))}.");
44
+
//
45
+
// objectResult.Value = enumerable.Where(item =>
46
+
// {
47
+
// var property = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
48
+
// if (property == null) return false;
49
+
// var value = property.GetValue(item);
50
+
// return value?.Equals(enumValue) == true;
51
+
// });
52
+
// }
53
+
// }
54
+
10
55
/// <summary>
11
-
/// An attribute to filter query results based on a specified property and enum type.
56
+
/// A flexible query filter that applies filtering based on a property selector and comparison type.
12
57
/// </summary>
13
-
/// <typeparam name="T">The type of the objects being filtered.</typeparam>
0 commit comments