forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incremental work on FastEndpoints implementation
- Loading branch information
1 parent
078f21c
commit d8d0834
Showing
21 changed files
with
275 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/common/Elsa.Api.Common/Extensions/WebApplicationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Elsa.Workflows.Core.Serialization; | ||
using FastEndpoints; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Elsa.Extensions; | ||
|
||
/// <summary> | ||
/// Provides extension methods to add the FastEndpoints middleware configured for use with Elsa API endpoints. | ||
/// </summary> | ||
public static class WebApplicationExtensions | ||
{ | ||
/// <summary> | ||
/// Register the FastEndpoints middleware configured for use with with Elsa API endpoints. | ||
/// </summary> | ||
/// <param name="app"></param> | ||
/// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param> | ||
/// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example> | ||
public static WebApplication UseElsaFastEndpoints(this WebApplication app, string routePrefix = "elsa/api") | ||
{ | ||
return app.UseFastEndpoints(config => | ||
{ | ||
config.Endpoints.RoutePrefix = routePrefix; | ||
config.Serializer.RequestDeserializer = DeserializeRequestAsync; | ||
config.Serializer.ResponseSerializer = SerializeRequestAsync; | ||
}); | ||
} | ||
|
||
static ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken) | ||
{ | ||
var serializerOptionsProvider = httpRequest.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>(); | ||
var options = serializerOptionsProvider.CreateApiOptions(); | ||
|
||
return serializerContext == null | ||
? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken) | ||
: JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken); | ||
} | ||
|
||
static Task SerializeRequestAsync(HttpResponse httpResponse, object dto, string contentType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken) | ||
{ | ||
var serializerOptionsProvider = httpResponse.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>(); | ||
var options = serializerOptionsProvider.CreateApiOptions(); | ||
|
||
httpResponse.ContentType = contentType; | ||
return serializerContext == null | ||
? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), options, cancellationToken) | ||
: JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), serializerContext, cancellationToken); | ||
} | ||
|
||
} |
34 changes: 5 additions & 29 deletions
34
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/BulkDelete/Endpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,29 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Workflows.Core.Serialization; | ||
using Elsa.Workflows.Persistence.Services; | ||
using FastEndpoints; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkDelete; | ||
|
||
public class BulkDelete : Endpoint<Request, Response> | ||
{ | ||
private readonly IWorkflowInstanceStore _store; | ||
private readonly SerializerOptionsProvider _serializerOptionsProvider; | ||
|
||
public BulkDelete(IWorkflowInstanceStore store, SerializerOptionsProvider serializerOptionsProvider) | ||
public BulkDelete(IWorkflowInstanceStore store) | ||
{ | ||
_store = store; | ||
_serializerOptionsProvider = serializerOptionsProvider; | ||
} | ||
|
||
public override void Configure() | ||
{ | ||
Post(""); | ||
Post("/bulk-actions/delete/workflow-instances/by-id"); | ||
} | ||
|
||
[Microsoft.AspNetCore.Mvc.HttpPost] | ||
public async Task<IActionResult> HandleAsync(CancellationToken cancellationToken) | ||
public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken) | ||
{ | ||
var serializerOptions = _serializerOptionsProvider.CreateApiOptions(); | ||
var model = await Request.ReadFromJsonAsync<Request>(serializerOptions, cancellationToken); | ||
var count = await _store.DeleteManyAsync(model!.Ids, cancellationToken); | ||
var count = await _store.DeleteManyAsync(request.Ids, cancellationToken); | ||
|
||
return Json(new Response(count), serializerOptions); | ||
return new Response(count); | ||
} | ||
} | ||
|
||
public class Request | ||
{ | ||
public ICollection<string> Ids { get; set; } = default!; | ||
} | ||
|
||
public class Response | ||
{ | ||
public Response(int deletedCount) | ||
{ | ||
DeletedCount = deletedCount; | ||
} | ||
|
||
[JsonPropertyName("deleted")] public int DeletedCount { get; init; } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/BulkDelete/Models.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkDelete; | ||
|
||
public class Request | ||
{ | ||
public ICollection<string> Ids { get; set; } = default!; | ||
} | ||
|
||
public class Response | ||
{ | ||
public Response(int deletedCount) | ||
{ | ||
DeletedCount = deletedCount; | ||
} | ||
|
||
[JsonPropertyName("deleted")] public int DeletedCount { get; } | ||
} |
22 changes: 0 additions & 22 deletions
22
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Delete.cs
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Delete/Endpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Workflows.Persistence.Services; | ||
using FastEndpoints; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.Delete; | ||
|
||
public class Delete : Endpoint<Request> | ||
{ | ||
private readonly IWorkflowInstanceStore _store; | ||
public Delete(IWorkflowInstanceStore store) => _store = store; | ||
|
||
public override void Configure() | ||
{ | ||
Delete("/workflow-instances/{id}"); | ||
} | ||
|
||
public override async Task HandleAsync(Request request, CancellationToken cancellationToken) | ||
{ | ||
var deleted = await _store.DeleteAsync(request.Id, cancellationToken); | ||
|
||
if (deleted) | ||
await SendNoContentAsync(cancellationToken); | ||
else | ||
await SendNotFoundAsync(cancellationToken); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Delete/Models.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.Delete; | ||
|
||
public class Request | ||
{ | ||
public string Id { get; set; } = default!; | ||
} |
30 changes: 0 additions & 30 deletions
30
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Get.cs
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Get/Endpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Workflows.Persistence.Services; | ||
using FastEndpoints; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.Get; | ||
|
||
public class Get : Endpoint<Request, Response, WorkflowInstanceMapper> | ||
{ | ||
private readonly IWorkflowInstanceStore _store; | ||
|
||
public Get(IWorkflowInstanceStore store) | ||
{ | ||
_store = store; | ||
} | ||
|
||
public override void Configure() | ||
{ | ||
Get("/workflow-instances/{id}"); | ||
} | ||
|
||
public override async Task HandleAsync(Request request, CancellationToken cancellationToken) | ||
{ | ||
var workflowInstance = await _store.FindByIdAsync(request.Id, cancellationToken); | ||
|
||
if (workflowInstance == null) | ||
await SendNotFoundAsync(cancellationToken); | ||
else | ||
await SendOkAsync(await Map.FromEntityAsync(workflowInstance), cancellationToken); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/Get/Mappers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Elsa.Workflows.Persistence.Entities; | ||
using FastEndpoints; | ||
|
||
namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.Get; | ||
|
||
public class WorkflowInstanceMapper : ResponseMapper<Response, WorkflowInstance> | ||
{ | ||
public override Response FromEntity(WorkflowInstance e) => new() | ||
{ | ||
DefinitionId = e.DefinitionId, | ||
DefinitionVersionId = e.DefinitionVersionId, | ||
Version = e.Version, | ||
WorkflowState = e.WorkflowState, | ||
Status = e.Status, | ||
SubStatus = e.SubStatus, | ||
CorrelationId = e.CorrelationId, | ||
Name = e.Name, | ||
Fault = e.Fault, | ||
CancelledAt = e.CancelledAt, | ||
CreatedAt = e.CreatedAt, | ||
FaultedAt = e.FaultedAt, | ||
FinishedAt = e.FinishedAt, | ||
LastExecutedAt = e.LastExecutedAt | ||
}; | ||
} |
Oops, something went wrong.