Move Dispatch and Execute API endpoints to Workflows resource

This commit is contained in:
Sipke Schoorstra 2021-06-16 16:57:57 +02:00
parent 97a9619fad
commit c2c9e9cd93
6 changed files with 90 additions and 125 deletions

View file

@ -1,45 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflow-definitions/{workflowDefinitionId}/dispatch")]
[Produces("application/json")]
public class Dispatch : Controller
{
private readonly IWorkflowLaunchpad _workflowLaunchpad;
public Dispatch(IWorkflowLaunchpad workflowLaunchpad)
{
_workflowLaunchpad = workflowLaunchpad;
}
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(DispatchWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow definition.",
Description = "Executes the specified workflow definition.",
OperationId = "WorkflowDefinitions.Execute",
Tags = new[] { "WorkflowDefinitions" })
]
public async Task<IActionResult> Handle(string workflowDefinitionId, DispatchWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.DispatchStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
return Ok(new DispatchWorkflowDefinitionResponse(result.WorkflowInstanceId, result.ActivityId));
}
}
}

View file

@ -1,53 +0,0 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflow-definitions/{workflowDefinitionId}/execute")]
[Produces("application/json")]
public class Execute : Controller
{
private readonly IWorkflowLaunchpad _workflowLaunchpad;
public Execute(IWorkflowLaunchpad workflowLaunchpad)
{
_workflowLaunchpad = workflowLaunchpad;
}
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExecuteWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Executes the specified workflow definition.",
Description = "Executes the specified workflow definition.",
OperationId = "WorkflowDefinitions.Execute",
Tags = new[] { "WorkflowDefinitions" })
]
public async Task<IActionResult> Handle(string workflowDefinitionId, ExecuteWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.ExecuteStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
if (Response.HasStarted)
return new EmptyResult();
return Ok(new ExecuteWorkflowDefinitionResponse(
result.Executed,
result.ActivityId,
result.WorkflowInstance
));
}
}
}

View file

@ -33,11 +33,12 @@ namespace Elsa.Server.Api.Endpoints.WorkflowDefinitions
public record DispatchWorkflowInstanceResponse();
public record ExecuteWorkflowsRequest(string ActivityType, IBookmark? Bookmark, IBookmark? Trigger, string? CorrelationId, string? WorkflowInstanceId, string? ContextId, object? Input);
public record TriggerWorkflowsRequest(string ActivityType, IBookmark? Bookmark, IBookmark? Trigger, string? CorrelationId, string? WorkflowInstanceId, string? ContextId, object? Input, bool Dispatch);
public record ExecuteWorkflowsResponse(ICollection<StartedWorkflow> StartedWorkflows);
public record TriggerWorkflowsResponse(ICollection<TriggeredWorkflow> TriggeredWorkflows);
public record TriggeredWorkflow(string WorkflowInstanceId, string? ActivityId);
public record DispatchWorkflowsRequest(string ActivityType, IBookmark? Bookmark, IBookmark? Trigger, string? CorrelationId, string? WorkflowInstanceId, string? ContextId, object? Input);
public record DispatchTriggerWorkflowsRequest(string ActivityType, IBookmark? Bookmark, IBookmark? Trigger, string? CorrelationId, string? WorkflowInstanceId, string? ContextId, object? Input);
public record DispatchWorkflowsResponse(ICollection<PendingWorkflow> PendingWorkflows);
public record DispatchTriggerWorkflowsResponse(ICollection<PendingWorkflow> PendingWorkflows);
}

View file

@ -5,14 +5,13 @@ using Elsa.Server.Api.Endpoints.WorkflowDefinitions;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Open.Linq.AsyncExtensions;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.Workflows
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflows/dispatch")]
[Route("v{apiVersion:apiVersion}/workflows/{workflowDefinitionId}/dispatch")]
[Produces("application/json")]
public class Dispatch : Controller
{
@ -25,23 +24,23 @@ namespace Elsa.Server.Api.Endpoints.Workflows
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(DispatchWorkflowsResponse))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(DispatchWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Triggers all workflows matching the specified criteria asynchronously.",
Description = "Triggers all workflows matching the specified criteria asynchronously.",
Summary = "Executes the specified workflow definition.",
Description = "Executes the specified workflow definition.",
OperationId = "Workflows.Dispatch",
Tags = new[] { "Workflows" })
]
public async Task<IActionResult> Handle(DispatchWorkflowsRequest request, CancellationToken cancellationToken = default)
public async Task<IActionResult> Handle(string workflowDefinitionId, DispatchWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var context = new CollectWorkflowsContext(request.ActivityType, request.Bookmark, request.Trigger, request.CorrelationId, request.WorkflowInstanceId, request.ContextId);
var result = await _workflowLaunchpad.CollectAndDispatchWorkflowsAsync(context, request.Input, cancellationToken).ToList();
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (Response.HasStarted)
return new EmptyResult();
return Ok(new DispatchWorkflowsResponse(result));
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.DispatchStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
return Ok(new DispatchWorkflowDefinitionResponse(result.WorkflowInstanceId, result.ActivityId));
}
}
}

View file

@ -5,14 +5,13 @@ using Elsa.Server.Api.Endpoints.WorkflowDefinitions;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Open.Linq.AsyncExtensions;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.Workflows
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflows/execute")]
[Route("v{apiVersion:apiVersion}/workflows/{workflowDefinitionId}/execute")]
[Produces("application/json")]
public class Execute : Controller
{
@ -25,23 +24,31 @@ namespace Elsa.Server.Api.Endpoints.Workflows
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExecuteWorkflowsResponse))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExecuteWorkflowDefinitionResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Triggers all workflows matching the specified criteria synchronously.",
Description = "Triggers all workflows matching the specified criteria synchronously.",
OperationId = "Workflows.Execute",
Tags = new[] { "Workflows" })
Summary = "Executes the specified workflow definition.",
Description = "Executes the specified workflow definition.",
OperationId = "WorkflowDefinitions.Execute",
Tags = new[] { "WorkflowDefinitions" })
]
public async Task<IActionResult> Handle(ExecuteWorkflowsRequest request, CancellationToken cancellationToken = default)
public async Task<IActionResult> Handle(string workflowDefinitionId, ExecuteWorkflowDefinitionRequest request, CancellationToken cancellationToken = default)
{
var context = new CollectWorkflowsContext(request.ActivityType, request.Bookmark, request.Trigger, request.CorrelationId, request.WorkflowInstanceId, request.ContextId);
var result = await _workflowLaunchpad.CollectAndExecuteWorkflowsAsync(context, request.Input, cancellationToken).ToList();
var startableWorkflow = await _workflowLaunchpad.CollectStartableWorkflowAsync(workflowDefinitionId, request.ActivityId, request.CorrelationId, request.ContextId, default, cancellationToken);
if (startableWorkflow == null)
return NotFound();
var result = await _workflowLaunchpad.ExecuteStartableWorkflowAsync(startableWorkflow, request.Input, cancellationToken);
if (Response.HasStarted)
return new EmptyResult();
return Ok(new ExecuteWorkflowsResponse(result));
return Ok(new ExecuteWorkflowDefinitionResponse(
result.Executed,
result.ActivityId,
result.WorkflowInstance
));
}
}
}

View file

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Server.Api.ActionFilters;
using Elsa.Server.Api.Endpoints.WorkflowDefinitions;
using Elsa.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Open.Linq.AsyncExtensions;
using Swashbuckle.AspNetCore.Annotations;
namespace Elsa.Server.Api.Endpoints.Workflows
{
[ApiController]
[ApiVersion("1")]
[Route("v{apiVersion:apiVersion}/workflows/trigger")]
[Produces("application/json")]
public class Trigger : Controller
{
private readonly IWorkflowLaunchpad _workflowLaunchpad;
public Trigger(IWorkflowLaunchpad workflowLaunchpad) => _workflowLaunchpad = workflowLaunchpad;
[HttpPost]
[ElsaJsonFormatter]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(TriggerWorkflowsResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
Summary = "Triggers all workflows matching the specified criteria synchronously.",
Description = "Triggers all workflows matching the specified criteria synchronously.",
OperationId = "Workflows.Execute",
Tags = new[] { "Workflows" })
]
public async Task<IActionResult> Handle(TriggerWorkflowsRequest request, CancellationToken cancellationToken = default)
{
var context = new CollectWorkflowsContext(request.ActivityType, request.Bookmark, request.Trigger, request.CorrelationId, request.WorkflowInstanceId, request.ContextId);
ICollection<TriggeredWorkflow> triggeredWorkflows;
if (request.Dispatch)
{
var result = await _workflowLaunchpad.CollectAndDispatchWorkflowsAsync(context, request.Input, cancellationToken).ToList();
triggeredWorkflows = result.Select(x => new TriggeredWorkflow(x.WorkflowInstanceId, x.ActivityId)).ToList();
}
else
{
var result = await _workflowLaunchpad.CollectAndExecuteWorkflowsAsync(context, request.Input, cancellationToken).ToList();
triggeredWorkflows = result.Select(x => new TriggeredWorkflow(x.WorkflowInstanceId, x.ActivityId)).ToList();
}
if (Response.HasStarted)
return new EmptyResult();
return Ok(new TriggerWorkflowsResponse(triggeredWorkflows));
}
}
}