using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using YABA.Common.Attributes; using YABA.Common.Lookups; namespace YABA.Common.Extensions { public static class EnumExtensions { private static readonly IEnumerable SuccessfulCrudStatuses = new List() { CrudResultLookup.CreateSucceeded, CrudResultLookup.UpdateSucceeded, CrudResultLookup.DeleteSucceeded, CrudResultLookup.RetrieveSuccessful }; public static TAttribute GetAttribute(this Enum value) where TAttribute : Attribute { var enumType = value.GetType(); var name = Enum.GetName(enumType, value); return enumType.GetField(name).GetCustomAttributes(false).OfType().SingleOrDefault(); } public static string GetDisplayName(this Enum enumValue) { return enumValue.GetAttribute().Name; } public static string GetClaimName(this ClaimsLookup claimLookup) { return claimLookup.GetAttribute().Name; } public static bool IsCrudResultSuccessful(this CrudResultLookup importStatusLookup) => SuccessfulCrudStatuses.Contains(importStatusLookup); public static bool IsCrudResultFailure(this CrudResultLookup importStatusLookup) => !SuccessfulCrudStatuses.Contains(importStatusLookup); } }