| | | 1 | | using Plainquire.Filter.Abstractions; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace Plainquire.Filter; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods used to handle MVC controller action parameters |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ParameterExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets all properties filterable by <see cref="EntityFilter"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="type">The type.</param> |
| | | 18 | | /// <autogeneratedoc /> |
| | | 19 | | public static IEnumerable<PropertyInfo> GetFilterableProperties(this Type type) |
| | 16 | 20 | | => type.GetProperties().Where(x => x.PropertyType.IsFilterableProperty() && x.IsParameterFilterable()); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the (MVC controller action) parameter name of the filter. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="member">The property to get the name for.</param> |
| | | 26 | | /// <param name="prefix">A prefix to use.</param> |
| | | 27 | | /// <autogeneratedoc /> |
| | | 28 | | public static string GetFilterParameterName(this MemberInfo member, string? prefix = null) |
| | | 29 | | { |
| | 48 | 30 | | prefix ??= member.ReflectedType.ExpandTypeName(); |
| | 48 | 31 | | var filterAttribute = member.GetCustomAttribute<FilterAttribute>(); |
| | 48 | 32 | | var name = filterAttribute?.Name ?? member.Name; |
| | 48 | 33 | | return $"{prefix}{name}".LowercaseFirstChar(); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Determines whether this parameter is visible as (MVC controller action) parameter. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="member">The member.</param> |
| | | 40 | | /// <autogeneratedoc /> |
| | | 41 | | private static bool IsParameterFilterable(this MemberInfo member) |
| | | 42 | | { |
| | 56 | 43 | | var filterAttribute = member.GetCustomAttribute<FilterAttribute>(); |
| | 56 | 44 | | return filterAttribute?.Filterable != false; |
| | | 45 | | } |
| | | 46 | | } |