| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Api.Contracts; |
| | | 3 | | using Elsa.Workflows.Api.Endpoints.Shells; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Api.Endpoints.Shells.Reload; |
| | | 9 | | |
| | | 10 | | [PublicAPI] |
| | 6 | 11 | | internal class Reload(IShellReloadOrchestrator shellReloadOrchestrator, IApiSerializer apiSerializer) : ElsaEndpointWith |
| | | 12 | | { |
| | | 13 | | public override void Configure() |
| | | 14 | | { |
| | 3 | 15 | | Post("/shells/{shellId}/reload"); |
| | 3 | 16 | | ConfigurePermissions("actions:shells:reload"); |
| | 3 | 17 | | } |
| | | 18 | | |
| | | 19 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 20 | | { |
| | 3 | 21 | | var shellId = Route<string>("shellId")!; |
| | 3 | 22 | | var result = await shellReloadOrchestrator.ReloadAsync(shellId, cancellationToken); |
| | | 23 | | |
| | 3 | 24 | | if (result.Status == ShellReloadStatus.NotFound) |
| | | 25 | | { |
| | 1 | 26 | | await Send.NotFoundAsync(cancellationToken); |
| | 1 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | |
| | 2 | 30 | | var serializerOptions = apiSerializer.GetOptions(); |
| | 2 | 31 | | var response = ShellReloadResponse.FromResult(result); |
| | 2 | 32 | | var statusCode = result.Status switch |
| | 2 | 33 | | { |
| | 0 | 34 | | ShellReloadStatus.Busy => StatusCodes.Status409Conflict, |
| | 1 | 35 | | ShellReloadStatus.RequestedShellFailed => StatusCodes.Status422UnprocessableEntity, |
| | 0 | 36 | | ShellReloadStatus.Failed => StatusCodes.Status503ServiceUnavailable, |
| | 1 | 37 | | _ => StatusCodes.Status200OK |
| | 2 | 38 | | }; |
| | | 39 | | |
| | 2 | 40 | | await SendJsonAsync(response, statusCode, serializerOptions, cancellationToken); |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task SendJsonAsync(ShellReloadResponse response, int statusCode, System.Text.Json.JsonSerializerOption |
| | | 44 | | { |
| | 2 | 45 | | HttpContext.Response.StatusCode = statusCode; |
| | 2 | 46 | | await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken); |
| | 2 | 47 | | } |
| | | 48 | | } |