| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Management.Models; |
| | | 7 | | using Elsa.Workflows.Management.Options; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Api.Endpoints.VariableTypes.List; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Returns a list of available variable types. |
| | | 15 | | /// </summary> |
| | | 16 | | [PublicAPI] |
| | | 17 | | internal class List : ElsaEndpointWithoutRequest<Response> |
| | | 18 | | { |
| | | 19 | | private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry; |
| | | 20 | | private readonly ManagementOptions _managementOptions; |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 1 | 23 | | public List(IOptions<ManagementOptions> managementOptions, IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 24 | | { |
| | 1 | 25 | | _wellKnownTypeRegistry = wellKnownTypeRegistry; |
| | 1 | 26 | | _managementOptions = managementOptions.Value; |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override void Configure() |
| | | 31 | | { |
| | 1 | 32 | | Get("/descriptors/variables"); |
| | 1 | 33 | | ConfigurePermissions("read:*", "read:variable-descriptors"); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public override Task<Response> ExecuteAsync(CancellationToken cancellationToken) |
| | | 38 | | { |
| | 0 | 39 | | var types = _managementOptions.VariableDescriptors; |
| | 0 | 40 | | var descriptors = types.Select(Map).ToList(); |
| | 0 | 41 | | var response = new Response(descriptors); |
| | | 42 | | |
| | 0 | 43 | | return Task.FromResult(response); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private VariableTypeDescriptor Map(VariableDescriptor descriptor) |
| | | 47 | | { |
| | 0 | 48 | | var type = descriptor.Type; |
| | 0 | 49 | | var hasAlias = _wellKnownTypeRegistry.TryGetAlias(type, out var alias); |
| | 0 | 50 | | var typeName = hasAlias ? alias : type.GetSimpleAssemblyQualifiedName()!; |
| | 0 | 51 | | var displayName = hasAlias ? alias : type.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? type.Name; |
| | 0 | 52 | | var description = descriptor.Description ?? type.GetCustomAttribute<DescriptionAttribute>()?.Description; |
| | | 53 | | |
| | 0 | 54 | | return new(typeName, displayName, descriptor.Category, description); |
| | | 55 | | } |
| | | 56 | | } |