| | | 1 | | using CShells.Lifecycle; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Shells.Api.Endpoints.Shells; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Shells.Api.Endpoints.Shells.Reload; |
| | | 9 | | |
| | | 10 | | [PublicAPI] |
| | 6 | 11 | | internal class Reload(IShellRegistry shellRegistry, IApiSerializer apiSerializer) : ElsaEndpointWithoutRequest |
| | | 12 | | { |
| | | 13 | | public override void Configure() |
| | | 14 | | { |
| | 4 | 15 | | Post("/shells/{shellId}/reload"); |
| | 4 | 16 | | ConfigurePermissions("actions:shells:reload"); |
| | 4 | 17 | | } |
| | | 18 | | |
| | | 19 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 20 | | { |
| | 2 | 21 | | var shellId = Route<string>("shellId")!; |
| | 2 | 22 | | var result = await shellRegistry.ReloadAsync(shellId, cancellationToken); |
| | | 23 | | |
| | | 24 | | // CShells 0.0.15 surfaces failures via ReloadResult.Error rather than throwing — translate to 404 |
| | | 25 | | // (which historically signalled "blueprint not found") for any composition / initialization error. |
| | 2 | 26 | | if (result.Error is not null) |
| | | 27 | | { |
| | 1 | 28 | | var failedResponse = new ShellReloadResponse |
| | 1 | 29 | | { |
| | 1 | 30 | | Status = ShellReloadStatus.NotFound, |
| | 1 | 31 | | RequestedShellId = shellId, |
| | 1 | 32 | | Timestamp = DateTimeOffset.UtcNow, |
| | 1 | 33 | | Message = result.Error.Message |
| | 1 | 34 | | }; |
| | 1 | 35 | | await SendResponseAsync(failedResponse, StatusCodes.Status404NotFound, cancellationToken); |
| | 1 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | var response = new ShellReloadResponse |
| | 1 | 40 | | { |
| | 1 | 41 | | Status = ShellReloadStatus.Completed, |
| | 1 | 42 | | RequestedShellId = shellId, |
| | 1 | 43 | | Timestamp = DateTimeOffset.UtcNow |
| | 1 | 44 | | }; |
| | 1 | 45 | | await SendResponseAsync(response, StatusCodes.Status200OK, cancellationToken); |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | private async Task SendResponseAsync(ShellReloadResponse response, int statusCode, CancellationToken cancellationTok |
| | | 49 | | { |
| | 2 | 50 | | var serializerOptions = apiSerializer.GetOptions(); |
| | 2 | 51 | | HttpContext.Response.StatusCode = statusCode; |
| | 2 | 52 | | await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken); |
| | 2 | 53 | | } |
| | | 54 | | } |