From b77e93a4a923bbd74158c1bd97a291bc28e67657 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 31 Aug 2021 13:56:04 +0200 Subject: [PATCH] Merge List with GetMany Fixes #1391 --- .../aspnet/Elsa.Samples.SendHttp/Program.cs | 5 +- .../Endpoints/WorkflowDefinitions/GetMany.cs | 63 ------------------- .../Endpoints/WorkflowDefinitions/List.cs | 20 +++++- .../Extensions/ServiceCollectionExtensions.cs | 2 - 4 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/GetMany.cs diff --git a/src/samples/aspnet/Elsa.Samples.SendHttp/Program.cs b/src/samples/aspnet/Elsa.Samples.SendHttp/Program.cs index 41044ff1e..c5d5e2559 100644 --- a/src/samples/aspnet/Elsa.Samples.SendHttp/Program.cs +++ b/src/samples/aspnet/Elsa.Samples.SendHttp/Program.cs @@ -5,10 +5,7 @@ namespace Elsa.Samples.SendHttp { public class Program { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } + public static void Main(string[] args) => CreateHostBuilder(args).Build().Run(); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) diff --git a/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/GetMany.cs b/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/GetMany.cs deleted file mode 100644 index a67b1690b..000000000 --- a/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/GetMany.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using AutoMapper; -using Elsa.Models; -using Elsa.Persistence; -using Elsa.Persistence.Specifications; -using Elsa.Persistence.Specifications.WorkflowDefinitions; -using Elsa.Serialization; -using Elsa.Server.Api.Attributes; -using Elsa.Server.Api.Models; -using Elsa.Server.Api.Swagger.Examples; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Swashbuckle.AspNetCore.Annotations; -using Swashbuckle.AspNetCore.Filters; - -namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions -{ - [ApiController] - [ApiVersion("1")] - [Route("v{apiVersion:apiVersion}/workflow-definitions")] - [Produces("application/json")] - public class GetMany : Controller - { - private readonly IWorkflowDefinitionStore _workflowDefinitionStore; - private readonly IContentSerializer _serializer; - private readonly IMapper _mapper; - - public GetMany(IWorkflowDefinitionStore workflowDefinitionStore, IContentSerializer serializer, IMapper mapper) - { - _workflowDefinitionStore = workflowDefinitionStore; - _serializer = serializer; - _mapper = mapper; - } - - [HttpGet] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ListModel))] - [SwaggerResponseExample(StatusCodes.Status200OK, typeof(WorkflowDefinitionPagedListExample))] - [SwaggerOperation( - Summary = "Returns a list of workflow definitions for the specified ids.", - Description = "Returns a list of workflow definition summaries. When no version options are specified, the latest versions are returned.", - OperationId = "WorkflowDefinitions.GetMany", - Tags = new[] { "WorkflowDefinitions" }) - ] - public async Task> Handle([RequiredFromQuery] string? ids, VersionOptions? version = default, CancellationToken cancellationToken = default) - { - IList summaries = new List(); - - if (!string.IsNullOrWhiteSpace(ids)) - { - version ??= VersionOptions.Latest; - var splitIds = ids.Split(',', StringSplitOptions.RemoveEmptyEntries); - var specification = new ManyWorkflowDefinitionIdsSpecification(splitIds, version.Value); - var items = await _workflowDefinitionStore.FindManyAsync(specification, cancellationToken: cancellationToken); - summaries = _mapper.Map>(items); - } - - return Json(new ListModel(summaries), _serializer.GetSettings()); - } - } -} \ No newline at end of file diff --git a/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/List.cs b/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/List.cs index a3a62e30e..eb46e4dd2 100644 --- a/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/List.cs +++ b/src/server/Elsa.Server.Api/Endpoints/WorkflowDefinitions/List.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -7,6 +8,7 @@ using Elsa.Persistence; using Elsa.Persistence.Specifications; using Elsa.Persistence.Specifications.WorkflowDefinitions; using Elsa.Serialization; +using Elsa.Server.Api.Attributes; using Elsa.Server.Api.Models; using Elsa.Server.Api.Swagger.Examples; using Elsa.Services; @@ -45,11 +47,16 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions OperationId = "WorkflowDefinitions.List", Tags = new[] { "WorkflowDefinitions" }) ] - public async Task>> Handle(int? page = default, int? pageSize = default, VersionOptions? version = default, CancellationToken cancellationToken = default) + public async Task>> Handle( + [FromQuery] string? ids, + int? page = default, + int? pageSize = default, + VersionOptions? version = default, + CancellationToken cancellationToken = default) { var tenantId = await _tenantAccessor.GetTenantIdAsync(cancellationToken); version ??= VersionOptions.Latest; - var specification = new VersionOptionsSpecification(version.Value).And(new TenantSpecification(tenantId)); + var specification = GetSpecification(ids, version.Value).And(new TenantSpecification(tenantId)); var totalCount = await _workflowDefinitionStore.CountAsync(specification, cancellationToken); var paging = page == null || pageSize == null ? default : Paging.Page(page.Value, pageSize.Value); var items = await _workflowDefinitionStore.FindManyAsync(specification, paging: paging, cancellationToken: cancellationToken); @@ -58,5 +65,14 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions return Json(pagedList, _serializer.GetSettings()); } + + private Specification GetSpecification(string? ids, VersionOptions version) + { + if (string.IsNullOrWhiteSpace(ids)) + return new VersionOptionsSpecification(version); + + var splitIds = ids.Split(',', StringSplitOptions.RemoveEmptyEntries); + return new ManyWorkflowDefinitionIdsSpecification(splitIds, version); + } } } \ No newline at end of file diff --git a/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs b/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs index 0186ef1ca..2057d1ccc 100644 --- a/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs @@ -77,8 +77,6 @@ namespace Microsoft.Extensions.DependencyInjection Type = PrimitiveType.String.ToString().ToLower(), Example = new OpenApiString("System.String, mscorlib") }); - - c.ResolveConflictingActions(d => d.First()); configure?.Invoke(c); });