| | | 1 | | using Elsa.Labels.Contracts; |
| | | 2 | | using Elsa.Labels.Entities; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Helpers; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | using FastEndpoints; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Open.Linq.AsyncExtensions; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Labels.Endpoints.WorkflowDefinitionLabels.Update; |
| | | 12 | | |
| | | 13 | | [PublicAPI] |
| | | 14 | | internal class Update : Endpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | private readonly ILabelStore _labelStore; |
| | | 17 | | private readonly IWorkflowDefinitionStore _workflowDefinitionStore; |
| | | 18 | | private readonly IWorkflowDefinitionLabelStore _workflowDefinitionLabelStore; |
| | | 19 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 20 | | |
| | | 21 | | // ReSharper disable once ClassNeverInstantiated.Local |
| | 0 | 22 | | private record SelectedLabelsModel(ICollection<string> LabelIds); |
| | | 23 | | |
| | | 24 | | private class WorkflowDefinitionLabelEqualityComparer : IEqualityComparer<WorkflowDefinitionLabel> |
| | | 25 | | { |
| | 0 | 26 | | public bool Equals(WorkflowDefinitionLabel? x, WorkflowDefinitionLabel? y) => x?.LabelId.Equals(y?.LabelId) ?? f |
| | 0 | 27 | | public int GetHashCode(WorkflowDefinitionLabel obj) => obj.LabelId.GetHashCode(); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | public Update( |
| | 0 | 31 | | ILabelStore labelStore, |
| | 0 | 32 | | IWorkflowDefinitionStore workflowDefinitionStore, |
| | 0 | 33 | | IWorkflowDefinitionLabelStore workflowDefinitionLabelStore, |
| | 0 | 34 | | IIdentityGenerator identityGenerator) |
| | | 35 | | { |
| | 0 | 36 | | _labelStore = labelStore; |
| | 0 | 37 | | _workflowDefinitionStore = workflowDefinitionStore; |
| | 0 | 38 | | _workflowDefinitionLabelStore = workflowDefinitionLabelStore; |
| | 0 | 39 | | _identityGenerator = identityGenerator; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public override void Configure() |
| | | 43 | | { |
| | 0 | 44 | | Post("/workflow-definitions/{id}/labels"); |
| | 0 | 45 | | Policies(Constants.PolicyName); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 49 | | { |
| | 0 | 50 | | var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionFilter{ Id = request.Id} |
| | | 51 | | |
| | 0 | 52 | | if (workflowDefinition == null) |
| | | 53 | | { |
| | 0 | 54 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Load the actual labels to ensure only existing ones are assigned. |
| | 0 | 59 | | var selectedLabelIds = await _labelStore.FindManyByIdAsync(request.LabelIds, cancellationToken).Select(x => x.Id |
| | | 60 | | |
| | | 61 | | // Project selected labels into WorkflowDefinitionLabels. |
| | 0 | 62 | | var newLabels = selectedLabelIds.Select(x => new WorkflowDefinitionLabel |
| | 0 | 63 | | { |
| | 0 | 64 | | Id = _identityGenerator.GenerateId(), |
| | 0 | 65 | | LabelId = x, |
| | 0 | 66 | | WorkflowDefinitionId = workflowDefinition.DefinitionId, |
| | 0 | 67 | | WorkflowDefinitionVersionId = workflowDefinition.Id |
| | 0 | 68 | | }).ToList(); |
| | | 69 | | |
| | | 70 | | // Get the current labels. |
| | 0 | 71 | | var currentLabels = await _workflowDefinitionLabelStore.FindByWorkflowDefinitionVersionIdAsync(request.Id, cance |
| | | 72 | | |
| | | 73 | | // Get a diff between new labels and existing labels. |
| | 0 | 74 | | var diff = Diff.For(currentLabels, newLabels, new WorkflowDefinitionLabelEqualityComparer()); |
| | | 75 | | |
| | | 76 | | // Replace assigned labels. |
| | 0 | 77 | | await _workflowDefinitionLabelStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken); |
| | | 78 | | |
| | 0 | 79 | | var response = new Response(selectedLabelIds); |
| | 0 | 80 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | } |