< Summary

Information
Class: Elsa.Labels.Endpoints.WorkflowDefinitionLabels.Update.Update
Assembly: Elsa.Labels
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Labels/Endpoints/WorkflowDefinitionLabels/Update/Endpoint.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 84
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_LabelIds()100%210%
Equals(...)0%2040%
GetHashCode(...)100%210%
.ctor(...)100%210%
Configure()100%210%
HandleAsync()0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Labels/Endpoints/WorkflowDefinitionLabels/Update/Endpoint.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Labels.Contracts;
 3using Elsa.Labels.Entities;
 4using Elsa.Workflows;
 5using Elsa.Workflows.Helpers;
 6using Elsa.Workflows.Management;
 7using Elsa.Workflows.Management.Filters;
 8using FastEndpoints;
 9using JetBrains.Annotations;
 10using Open.Linq.AsyncExtensions;
 11
 12namespace Elsa.Labels.Endpoints.WorkflowDefinitionLabels.Update;
 13
 14[PublicAPI]
 15internal class Update : ElsaEndpoint<Request, Response>
 16{
 17    private readonly ILabelStore _labelStore;
 18    private readonly IWorkflowDefinitionStore _workflowDefinitionStore;
 19    private readonly IWorkflowDefinitionLabelStore _workflowDefinitionLabelStore;
 20    private readonly IIdentityGenerator _identityGenerator;
 21
 22    // ReSharper disable once ClassNeverInstantiated.Local
 023    private record SelectedLabelsModel(ICollection<string> LabelIds);
 24
 25    private class WorkflowDefinitionLabelEqualityComparer : IEqualityComparer<WorkflowDefinitionLabel>
 26    {
 027        public bool Equals(WorkflowDefinitionLabel? x, WorkflowDefinitionLabel? y) => x?.LabelId.Equals(y?.LabelId) ?? f
 028        public int GetHashCode(WorkflowDefinitionLabel obj) => obj.LabelId.GetHashCode();
 29    }
 30
 031    public Update(
 032        ILabelStore labelStore,
 033        IWorkflowDefinitionStore workflowDefinitionStore,
 034        IWorkflowDefinitionLabelStore workflowDefinitionLabelStore,
 035        IIdentityGenerator identityGenerator)
 36    {
 037        _labelStore = labelStore;
 038        _workflowDefinitionStore = workflowDefinitionStore;
 039        _workflowDefinitionLabelStore = workflowDefinitionLabelStore;
 040        _identityGenerator = identityGenerator;
 041    }
 42
 43    public override void Configure()
 44    {
 045        Post("/workflow-definitions/{id}/labels");
 046        ConfigurePermissions("update:workflow-definition-labels");
 047    }
 48
 49    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 50    {
 051        var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionFilter{ Id = request.Id}
 52
 053        if (workflowDefinition == null)
 54        {
 055            await Send.NotFoundAsync(cancellationToken);
 056            return;
 57        }
 58
 59        // Load the actual labels to ensure only existing ones are assigned.
 060        var selectedLabelIds = await _labelStore.FindManyByIdAsync(request.LabelIds, cancellationToken).Select(x => x.Id
 61
 62        // Project selected labels into WorkflowDefinitionLabels.
 063        var newLabels = selectedLabelIds.Select(x => new WorkflowDefinitionLabel
 064        {
 065            Id = _identityGenerator.GenerateId(),
 066            LabelId = x,
 067            WorkflowDefinitionId = workflowDefinition.DefinitionId,
 068            WorkflowDefinitionVersionId = workflowDefinition.Id
 069        }).ToList();
 70
 71        // Get the current labels.
 072        var currentLabels = await _workflowDefinitionLabelStore.FindByWorkflowDefinitionVersionIdAsync(request.Id, cance
 73
 74        // Get a diff between new labels and existing labels.
 075        var diff = Diff.For(currentLabels, newLabels, new WorkflowDefinitionLabelEqualityComparer());
 76
 77        // Replace assigned labels.
 078        await _workflowDefinitionLabelStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken);
 79
 080        var response = new Response(selectedLabelIds);
 081        await Send.OkAsync(response, cancellationToken);
 082    }
 83
 84}