< 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: 83
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.Labels.Contracts;
 2using Elsa.Labels.Entities;
 3using Elsa.Workflows;
 4using Elsa.Workflows.Helpers;
 5using Elsa.Workflows.Management;
 6using Elsa.Workflows.Management.Filters;
 7using FastEndpoints;
 8using JetBrains.Annotations;
 9using Open.Linq.AsyncExtensions;
 10
 11namespace Elsa.Labels.Endpoints.WorkflowDefinitionLabels.Update;
 12
 13[PublicAPI]
 14internal 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
 022    private record SelectedLabelsModel(ICollection<string> LabelIds);
 23
 24    private class WorkflowDefinitionLabelEqualityComparer : IEqualityComparer<WorkflowDefinitionLabel>
 25    {
 026        public bool Equals(WorkflowDefinitionLabel? x, WorkflowDefinitionLabel? y) => x?.LabelId.Equals(y?.LabelId) ?? f
 027        public int GetHashCode(WorkflowDefinitionLabel obj) => obj.LabelId.GetHashCode();
 28    }
 29
 030    public Update(
 031        ILabelStore labelStore,
 032        IWorkflowDefinitionStore workflowDefinitionStore,
 033        IWorkflowDefinitionLabelStore workflowDefinitionLabelStore,
 034        IIdentityGenerator identityGenerator)
 35    {
 036        _labelStore = labelStore;
 037        _workflowDefinitionStore = workflowDefinitionStore;
 038        _workflowDefinitionLabelStore = workflowDefinitionLabelStore;
 039        _identityGenerator = identityGenerator;
 040    }
 41
 42    public override void Configure()
 43    {
 044        Post("/workflow-definitions/{id}/labels");
 045        Policies(Constants.PolicyName);
 046    }
 47
 48    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 49    {
 050        var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionFilter{ Id = request.Id}
 51
 052        if (workflowDefinition == null)
 53        {
 054            await Send.NotFoundAsync(cancellationToken);
 055            return;
 56        }
 57
 58        // Load the actual labels to ensure only existing ones are assigned.
 059        var selectedLabelIds = await _labelStore.FindManyByIdAsync(request.LabelIds, cancellationToken).Select(x => x.Id
 60
 61        // Project selected labels into WorkflowDefinitionLabels.
 062        var newLabels = selectedLabelIds.Select(x => new WorkflowDefinitionLabel
 063        {
 064            Id = _identityGenerator.GenerateId(),
 065            LabelId = x,
 066            WorkflowDefinitionId = workflowDefinition.DefinitionId,
 067            WorkflowDefinitionVersionId = workflowDefinition.Id
 068        }).ToList();
 69
 70        // Get the current labels.
 071        var currentLabels = await _workflowDefinitionLabelStore.FindByWorkflowDefinitionVersionIdAsync(request.Id, cance
 72
 73        // Get a diff between new labels and existing labels.
 074        var diff = Diff.For(currentLabels, newLabels, new WorkflowDefinitionLabelEqualityComparer());
 75
 76        // Replace assigned labels.
 077        await _workflowDefinitionLabelStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken);
 78
 079        var response = new Response(selectedLabelIds);
 080        await Send.OkAsync(response, cancellationToken);
 081    }
 82
 83}