diff --git a/NHSUKViewComponents.Web/Enums/Enumeration.cs b/NHSUKViewComponents.Web/Enums/Enumeration.cs deleted file mode 100644 index 5c551f4..0000000 --- a/NHSUKViewComponents.Web/Enums/Enumeration.cs +++ /dev/null @@ -1,131 +0,0 @@ -namespace NHSUKViewComponents.Web.Enums -{ - using System; - using System.Collections.Generic; - using System.ComponentModel; - using System.Globalization; - using System.Linq; - using System.Reflection; - - /// - /// Enumeration base class as recommended at https://ankitvijay.net/2020/05/21/introduction-enumeration-class/ - /// and https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types - /// - public abstract class Enumeration - { - public string Name { get; private set; } - - public int Id { get; private set; } - - protected Enumeration(int id, string name) => (Id, Name) = (id, name); - - public override string ToString() => Name; - - public static IEnumerable GetAll() where T : Enumeration => - typeof(T).GetFields( - BindingFlags.Public | - BindingFlags.Static | - BindingFlags.DeclaredOnly) - .Select(f => f.GetValue(null)) - .Cast(); - - public override bool Equals(object obj) - { - if (obj == null) - { - return false; - } - - if (!(obj is Enumeration objAsEnumeration)) - { - return false; - } - - var typeMatches = GetType() == obj.GetType(); - var valueMatches = Id.Equals(objAsEnumeration.Id); - - return typeMatches && valueMatches; - } - - public static bool TryGetFromIdOrName( - string idOrName, - out T enumeration, - bool ignoreCase = false - ) - where T : Enumeration - { - var comparison = - ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; - return TryParse(item => string.Equals(item.Name, idOrName, comparison), out enumeration) || - int.TryParse(idOrName, out var id) && - TryParse(item => item.Id == id, out enumeration); - } - - protected static bool TryParse( - Func predicate, - out TEnumeration enumeration) - where TEnumeration : Enumeration - { - enumeration = GetAll().FirstOrDefault(predicate); - return enumeration != null; - } - - public static T FromId(int id) where T : Enumeration - { - var matchingItem = Parse(id, "nameOrValue", item => item.Id == id); - return matchingItem; - } - - public static T FromName(string name) where T : Enumeration - { - var matchingItem = Parse(name, "name", item => item.Name == name); - return matchingItem; - } - - private static TEnumeration Parse( - TIntOrString nameOrValue, - string description, - Func predicate) - where TEnumeration : Enumeration - { - var matchingItem = GetAll().FirstOrDefault(predicate); - - if (matchingItem == null) - { - throw new InvalidOperationException( - $"'{nameOrValue}' is not a valid {description} in {typeof(TEnumeration)}"); - } - - return matchingItem; - } - - public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id); - } - - public class EnumerationTypeConverter : TypeConverter where T : Enumeration - { - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); - } - - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - return value is string casted - ? Enumeration.FromName(casted) - : base.ConvertFrom(context, culture, value); - } - - public override object ConvertTo( - ITypeDescriptorContext context, - CultureInfo culture, - object value, - Type destinationType - ) - { - return destinationType == typeof(string) && value is Enumeration casted - ? casted.Name - : base.ConvertTo(context, culture, value, destinationType); - } - } -} diff --git a/NHSUKViewComponents.Web/ViewComponents/RadiosViewComponent.cs b/NHSUKViewComponents.Web/ViewComponents/RadiosViewComponent.cs deleted file mode 100644 index 8e0b6fb..0000000 --- a/NHSUKViewComponents.Web/ViewComponents/RadiosViewComponent.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace NHSUKViewComponents.Web.ViewComponents -{ - using System.Collections.Generic; - using System.Linq; - using Microsoft.AspNetCore.Mvc; - using NHSUKViewComponents.Web.Enums; - using NHSUKViewComponents.Web.ViewModels; - - public class RadiosViewComponent : ViewComponent - { - public IViewComponentResult Invoke( - string aspFor, - string label, - IEnumerable radios, - bool populateWithCurrentValues, - string? hintText, - bool required - ) - { - var radiosList = radios.Select( - r => new RadiosItemViewModel( - r.Enumeration.Name, - r.Label, - IsSelectedRadio(aspFor, r.Enumeration, populateWithCurrentValues), - r.HintText - ) - ); - - var viewModel = new RadiosViewModel( - aspFor, - label, - string.IsNullOrEmpty(hintText) ? null : hintText, - radiosList, - required - ); - - return View(viewModel); - } - - private bool IsSelectedRadio(string aspFor, Enumeration radioItem, bool populateWithCurrentValue) - { - var model = ViewData.Model; - - var property = model.GetType().GetProperty(aspFor); - var value = (Enumeration)property?.GetValue(model)!; - - return populateWithCurrentValue && value.Equals(radioItem); - } - } -} diff --git a/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs b/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs deleted file mode 100644 index 25af69b..0000000 --- a/NHSUKViewComponents.Web/ViewModels/RadiosItemViewModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace NHSUKViewComponents.Web.ViewModels -{ - public class RadiosItemViewModel - { - public RadiosItemViewModel(string value, string label, bool selected, string? hintText) - { - Value = value; - Label = label; - Selected = selected; - HintText = hintText; - } - - public string Value { get; set; } - - public string Label { get; set; } - - public bool Selected { get; set; } - - public string? HintText { get; set; } - } -} diff --git a/NHSUKViewComponents.Web/ViewModels/RadiosListItemViewModel.cs b/NHSUKViewComponents.Web/ViewModels/RadiosListItemViewModel.cs deleted file mode 100644 index 01a2baa..0000000 --- a/NHSUKViewComponents.Web/ViewModels/RadiosListItemViewModel.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace NHSUKViewComponents.Web.ViewModels -{ - using NHSUKViewComponents.Web.Enums; - - public class RadiosListItemViewModel - { - public RadiosListItemViewModel(Enumeration enumeration, string label, string? hintText = null) - { - Enumeration = enumeration; - Label = label; - HintText = hintText; - } - - public Enumeration Enumeration { get; set; } - - public string Label { get; set; } - - public string? HintText { get; set; } - } -} diff --git a/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs b/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs deleted file mode 100644 index 56b968a..0000000 --- a/NHSUKViewComponents.Web/ViewModels/RadiosViewModel.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace NHSUKViewComponents.Web.ViewModels -{ - using System.Collections.Generic; - - public class RadiosViewModel - { - public RadiosViewModel( - string aspFor, - string label, - string? hintText, - IEnumerable radios, - bool required - ) - { - AspFor = aspFor; - Label = (!required && !label.EndsWith("(optional)") ? label + " (optional)" : label); - HintText = hintText; - Radios = radios; - Required = required; - } - - public string AspFor { get; set; } - - public string Label { get; set; } - - public string? HintText { get; set; } - - public IEnumerable Radios { get; set; } - public bool Required { get; set; } - } -} diff --git a/NHSUKViewComponents.Web/Views/Shared/Components/Radios/Default.cshtml b/NHSUKViewComponents.Web/Views/Shared/Components/Radios/Default.cshtml deleted file mode 100644 index 4d7581e..0000000 --- a/NHSUKViewComponents.Web/Views/Shared/Components/Radios/Default.cshtml +++ /dev/null @@ -1,48 +0,0 @@ -@using NHSUKViewComponents.Web.Extensions -@using NHSUKViewComponents.Web.ViewModels -@model RadiosViewModel - - - - - - - @Model.Label - - - - @if (Model.HintText != null) - { - - @Html.Raw(Model.HintText) - - } - - - @foreach (var (radio, index) in Model.Radios.Select((r, i) => (r, i))) - { - var radioId = $"{radio.Value}-{index}"; - - - - @radio.Label - - @if (radio.HintText != null) - { - - @radio.HintText - - } - - } - - - - -