| | 1 | |
|
| | 2 | |
|
| | 3 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | 5 | | using Microsoft.Extensions.Options; |
| | 6 | | using Plainquire.Filter.Abstractions; |
| | 7 | | using Plainquire.Page.Abstractions; |
| | 8 | | using System; |
| | 9 | | using System.Globalization; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Reflection; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | |
|
| | 14 | | namespace Plainquire.Page.Mvc.ModelBinders; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// ModelBinder for <see cref="EntityPage{TEntity}"/> |
| | 18 | | /// Implements <see cref="IModelBinder" /> |
| | 19 | | /// </summary> |
| | 20 | | /// <seealso cref="IModelBinder" /> |
| | 21 | | public class EntityPageModelBinder : IModelBinder |
| | 22 | | { |
| | 23 | | /// <inheritdoc /> |
| | 24 | | public Task BindModelAsync(ModelBindingContext bindingContext) |
| | 25 | | { |
| 19 | 26 | | if (bindingContext == null) |
| 0 | 27 | | throw new ArgumentNullException(nameof(bindingContext)); |
| | 28 | |
|
| 19 | 29 | | var serviceProvider = bindingContext.ActionContext.HttpContext.RequestServices; |
| | 30 | |
|
| 19 | 31 | | var isGenericPage = bindingContext.ModelType.IsGenericType; |
| 19 | 32 | | var pageType = isGenericPage ? bindingContext.ModelType.GenericTypeArguments[0] : null; |
| 19 | 33 | | var entityPage = CreateEntityPage(pageType, serviceProvider); |
| | 34 | |
|
| 19 | 35 | | var parameterOrPropertyName = bindingContext.ModelMetadata.ParameterName ?? bindingContext.ModelMetadata.Propert |
| 19 | 36 | | if (parameterOrPropertyName == null) |
| 0 | 37 | | throw new InvalidOperationException("Unable to get original parameter name."); |
| | 38 | |
|
| 19 | 39 | | var (pageParameterName, pageSizeParameterName) = ParameterExtensions |
| 19 | 40 | | .GetPageParameterNames(parameterOrPropertyName, bindingContext.OriginalModelName); |
| | 41 | |
|
| 19 | 42 | | var pageParameterValue = bindingContext.HttpContext.Request.Query.Keys |
| 19 | 43 | | .Where(queryParameter => queryParameter.Equals(pageParameterName, StringComparison.OrdinalIgnoreCase)) |
| 19 | 44 | | .Select(queryParameter => GetParameterValue(queryParameter, bindingContext)) |
| 19 | 45 | | .FirstOrDefault(); |
| | 46 | |
|
| 19 | 47 | | var pageSizeParameterValue = bindingContext.HttpContext.Request.Query.Keys |
| 19 | 48 | | .Where(queryParameter => queryParameter.Equals(pageSizeParameterName, StringComparison.OrdinalIgnoreCase)) |
| 19 | 49 | | .Select(queryParameter => GetParameterValue(queryParameter, bindingContext)) |
| 19 | 50 | | .FirstOrDefault(); |
| | 51 | |
|
| 19 | 52 | | var pageSizeFromFilterAttribute = pageType? |
| 19 | 53 | | .GetCustomAttribute<EntityFilterAttribute>()? |
| 19 | 54 | | .PageSize |
| 19 | 55 | | .ToString(CultureInfo.InvariantCulture); |
| | 56 | |
|
| 19 | 57 | | entityPage.PageNumberValue = pageParameterValue ?? string.Empty; |
| 19 | 58 | | entityPage.PageSizeValue = pageSizeParameterValue ?? pageSizeFromFilterAttribute ?? string.Empty; |
| | 59 | |
|
| 19 | 60 | | bindingContext.Result = ModelBindingResult.Success(entityPage); |
| | 61 | |
|
| 19 | 62 | | return Task.CompletedTask; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | private static string? GetParameterValue(string queryParameter, ModelBindingContext bindingContext) |
| 33 | 66 | | => bindingContext.ValueProvider.GetValue(queryParameter).FirstValue; |
| | 67 | |
|
| | 68 | | private static EntityPage CreateEntityPage(Type? pageType, IServiceProvider serviceProvider) |
| | 69 | | { |
| 19 | 70 | | if (pageType == null) |
| 10 | 71 | | return new EntityPage(); |
| | 72 | |
|
| 9 | 73 | | var entityPageType = typeof(EntityPage<>).MakeGenericType(pageType); |
| 9 | 74 | | var entityPageInstance = Activator.CreateInstance(entityPageType) |
| 9 | 75 | | ?? throw new InvalidOperationException($"Unable to create instance of type {entityPageType.Name}"); |
| | 76 | |
|
| 9 | 77 | | var entityPage = (EntityPage)entityPageInstance; |
| | 78 | |
|
| 9 | 79 | | var prototypeConfiguration = ((EntityPage?)serviceProvider.GetService(entityPageType))?.Configuration; |
| 9 | 80 | | var injectedConfiguration = serviceProvider.GetService<IOptions<PageConfiguration>>()?.Value; |
| 9 | 81 | | entityPage.Configuration = prototypeConfiguration ?? injectedConfiguration; |
| | 82 | |
|
| 9 | 83 | | return entityPage; |
| | 84 | | } |
| | 85 | | } |