|
| 1 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 2 | + |
| 3 | +using XtremeIdiots.Portal.Repository.Abstractions.Constants.V1.Analytics; |
| 4 | + |
| 5 | +namespace XtremeIdiots.Portal.Repository.Api.V1.Analytics; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Model binder for the analytics query enums (<see cref="AnalyticsCompareMode"/>, |
| 9 | +/// <see cref="AnalyticsAlignMode"/>, <see cref="AnalyticsBucket"/>). The typed API client serialises |
| 10 | +/// these as snake_case wire values (for example <c>previous_period</c>) rather than the enum member |
| 11 | +/// name, which the default enum binder rejects. This binder accepts snake_case, the enum member name |
| 12 | +/// (case-insensitive) and numeric values. |
| 13 | +/// </summary> |
| 14 | +public sealed class AnalyticsEnumModelBinder : IModelBinder |
| 15 | +{ |
| 16 | + public Task BindModelAsync(ModelBindingContext bindingContext) |
| 17 | + { |
| 18 | + ArgumentNullException.ThrowIfNull(bindingContext); |
| 19 | + |
| 20 | + var modelName = bindingContext.ModelName; |
| 21 | + var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName); |
| 22 | + |
| 23 | + if (valueProviderResult == ValueProviderResult.None) |
| 24 | + { |
| 25 | + return Task.CompletedTask; |
| 26 | + } |
| 27 | + |
| 28 | + var rawValue = valueProviderResult.FirstValue; |
| 29 | + if (string.IsNullOrWhiteSpace(rawValue)) |
| 30 | + { |
| 31 | + return Task.CompletedTask; |
| 32 | + } |
| 33 | + |
| 34 | + bindingContext.ModelState.SetModelValue(modelName, valueProviderResult); |
| 35 | + |
| 36 | + var enumType = bindingContext.ModelMetadata.UnderlyingOrModelType; |
| 37 | + var normalized = rawValue.Replace("_", string.Empty, StringComparison.Ordinal); |
| 38 | + |
| 39 | + if (Enum.TryParse(enumType, normalized, ignoreCase: true, out var parsed)) |
| 40 | + { |
| 41 | + bindingContext.Result = ModelBindingResult.Success(parsed); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + bindingContext.ModelState.TryAddModelError(modelName, $"The value '{rawValue}' is not valid."); |
| 46 | + } |
| 47 | + |
| 48 | + return Task.CompletedTask; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// <summary> |
| 53 | +/// Supplies <see cref="AnalyticsEnumModelBinder"/> for the analytics query enum types. |
| 54 | +/// </summary> |
| 55 | +public sealed class AnalyticsEnumModelBinderProvider : IModelBinderProvider |
| 56 | +{ |
| 57 | + private static readonly HashSet<Type> SupportedTypes = |
| 58 | + [ |
| 59 | + typeof(AnalyticsCompareMode), |
| 60 | + typeof(AnalyticsAlignMode), |
| 61 | + typeof(AnalyticsBucket) |
| 62 | + ]; |
| 63 | + |
| 64 | + public IModelBinder? GetBinder(ModelBinderProviderContext context) |
| 65 | + { |
| 66 | + ArgumentNullException.ThrowIfNull(context); |
| 67 | + |
| 68 | + return SupportedTypes.Contains(context.Metadata.UnderlyingOrModelType) |
| 69 | + ? new AnalyticsEnumModelBinder() |
| 70 | + : null; |
| 71 | + } |
| 72 | +} |
0 commit comments