| | 1 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using Plainquire.Filter.Abstractions; |
| | 5 | | using System; |
| | 6 | | using System.Reflection; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Plainquire.Filter.Mvc.ModelBinders; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// ModelBinder for <see cref="EntityFilter{TEntity}"/> |
| | 13 | | /// Implements <see cref="IModelBinder" /> |
| | 14 | | /// </summary> |
| | 15 | | /// <seealso cref="IModelBinder" /> |
| | 16 | | public class EntityFilterModelBinder : IModelBinder |
| | 17 | | { |
| | 18 | | /// <inheritdoc /> |
| | 19 | | public Task BindModelAsync(ModelBindingContext bindingContext) |
| | 20 | | { |
| 10 | 21 | | if (bindingContext == null) |
| 0 | 22 | | throw new ArgumentNullException(nameof(bindingContext)); |
| | 23 | |
|
| 10 | 24 | | var serviceProvider = bindingContext.ActionContext.HttpContext.RequestServices; |
| 10 | 25 | | var filteredType = bindingContext.ModelType.GetGenericArguments()[0]; |
| | 26 | |
|
| 10 | 27 | | var filterableProperties = filteredType.GetFilterableProperties(); |
| 10 | 28 | | var entityFilterAttribute = filteredType.GetCustomAttribute<EntityFilterAttribute>(); |
| 10 | 29 | | var entityFilter = CreateEntityFilter(filteredType, serviceProvider); |
| | 30 | |
|
| 84 | 31 | | foreach (var property in filterableProperties) |
| | 32 | | { |
| 32 | 33 | | var parameterName = property.GetFilterParameterName(entityFilterAttribute?.Prefix); |
| 32 | 34 | | var parameterValues = bindingContext.ValueProvider.GetValue(parameterName); |
| 84 | 35 | | foreach (var filterSyntax in parameterValues) |
| 10 | 36 | | entityFilter.PropertyFilters.Add(new PropertyFilter(property.Name, ValueFiltersFactory.Create(filterSynt |
| | 37 | | } |
| | 38 | |
|
| 10 | 39 | | bindingContext.Result = ModelBindingResult.Success(entityFilter); |
| 10 | 40 | | return Task.CompletedTask; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | private static EntityFilter CreateEntityFilter(Type? type, IServiceProvider serviceProvider) |
| | 44 | | { |
| 10 | 45 | | if (type == null) |
| 0 | 46 | | return new EntityFilter(); |
| | 47 | |
|
| 10 | 48 | | var entityFilterType = typeof(EntityFilter<>).MakeGenericType(type); |
| 10 | 49 | | var entityFilterInstance = Activator.CreateInstance(entityFilterType) |
| 10 | 50 | | ?? throw new InvalidOperationException($"Unable to create instance of type {entityFilterType.Name}"); |
| | 51 | |
|
| 10 | 52 | | var entityFilter = (EntityFilter)entityFilterInstance; |
| | 53 | |
|
| 10 | 54 | | var prototypeConfiguration = ((EntityFilter?)serviceProvider.GetService(entityFilterType))?.Configuration; |
| 10 | 55 | | var injectedConfiguration = serviceProvider.GetService<IOptions<FilterConfiguration>>()?.Value; |
| 10 | 56 | | entityFilter.Configuration = prototypeConfiguration ?? injectedConfiguration; |
| | 57 | |
|
| 10 | 58 | | return entityFilter; |
| | 59 | | } |
| | 60 | | } |