| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.ComponentModel.DataAnnotations; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using Elsa.Abstractions; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Models; |
| | | 7 | | using Humanizer; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Api.Endpoints.IncidentStrategies.List; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Returns list of available <see cref="IIncidentStrategy" /> implementations. |
| | | 13 | | /// </summary> |
| | 1 | 14 | | internal class List(IEnumerable<IIncidentStrategy> strategies) : ElsaEndpointWithoutRequest<ListResponse<IncidentStrateg |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 1 | 18 | | Get("/descriptors/incident-strategies"); |
| | 1 | 19 | | ConfigurePermissions("read:incident-strategies"); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | public override Task<ListResponse<IncidentStrategyDescriptor>> ExecuteAsync(CancellationToken cancellationToken) |
| | | 23 | | { |
| | 0 | 24 | | var descriptors = strategies.Select(IncidentStrategyDescriptor.FromStrategy).OrderBy(x => x.DisplayName).ToList( |
| | 0 | 25 | | var response =new ListResponse<IncidentStrategyDescriptor>(descriptors); |
| | 0 | 26 | | return Task.FromResult(response); |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal record IncidentStrategyDescriptor(string DisplayName, string Description, string TypeName) |
| | | 31 | | { |
| | | 32 | | public static IncidentStrategyDescriptor FromStrategy(IIncidentStrategy strategy) |
| | | 33 | | { |
| | | 34 | | var type = strategy.GetType(); |
| | | 35 | | var displayNameAttribute = type.GetCustomAttribute<DisplayNameAttribute>(); |
| | | 36 | | var descriptionAttribute = type.GetCustomAttribute<DescriptionAttribute>(); |
| | | 37 | | var displayAttribute = type.GetCustomAttribute<DisplayAttribute>(); |
| | | 38 | | var displayName = displayNameAttribute?.DisplayName ?? displayAttribute?.Name ?? type.Name.Replace("Strategy", " |
| | | 39 | | var description = descriptionAttribute?.Description ?? displayAttribute?.Description ?? ""; |
| | | 40 | | |
| | | 41 | | return new IncidentStrategyDescriptor(displayName, description, type.GetSimpleAssemblyQualifiedName()); |
| | | 42 | | } |
| | | 43 | | } |