< 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: 85
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.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Labels.Contracts;
 4using Elsa.Labels.Entities;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Helpers;
 7using Elsa.Workflows.Management;
 8using Elsa.Workflows.Management.Filters;
 9using FastEndpoints;
 10using JetBrains.Annotations;
 11using Open.Linq.AsyncExtensions;
 12
 13namespace Elsa.Labels.Endpoints.WorkflowDefinitionLabels.Update;
 14
 15[PublicAPI]
 16internal class Update : ElsaEndpoint<Request, Response>
 17{
 18    private readonly ILabelStore _labelStore;
 19    private readonly IWorkflowDefinitionStore _workflowDefinitionStore;
 20    private readonly IWorkflowDefinitionLabelStore _workflowDefinitionLabelStore;
 21    private readonly IIdentityGenerator _identityGenerator;
 22
 23    // ReSharper disable once ClassNeverInstantiated.Local
 024    private record SelectedLabelsModel(ICollection<string> LabelIds);
 25
 26    private class WorkflowDefinitionLabelEqualityComparer : IEqualityComparer<WorkflowDefinitionLabel>
 27    {
 028        public bool Equals(WorkflowDefinitionLabel? x, WorkflowDefinitionLabel? y) => x?.LabelId.Equals(y?.LabelId) ?? f
 029        public int GetHashCode(WorkflowDefinitionLabel obj) => obj.LabelId.GetHashCode();
 30    }
 31
 032    public Update(
 033        ILabelStore labelStore,
 034        IWorkflowDefinitionStore workflowDefinitionStore,
 035        IWorkflowDefinitionLabelStore workflowDefinitionLabelStore,
 036        IIdentityGenerator identityGenerator)
 37    {
 038        _labelStore = labelStore;
 039        _workflowDefinitionStore = workflowDefinitionStore;
 040        _workflowDefinitionLabelStore = workflowDefinitionLabelStore;
 041        _identityGenerator = identityGenerator;
 042    }
 43
 44    public override void Configure()
 45    {
 046        Post("/workflow-definitions/{id}/labels");
 047        RequirePermission(Elsa.Labels.Permissions.LabelPermissions.WorkflowDefinitionLabels, CoreVerbs.Update);
 048    }
 49
 50    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 51    {
 052        var workflowDefinition = await _workflowDefinitionStore.FindAsync(new WorkflowDefinitionFilter{ Id = request.Id}
 53
 054        if (workflowDefinition == null)
 55        {
 056            await Send.NotFoundAsync(cancellationToken);
 057            return;
 58        }
 59
 60        // Load the actual labels to ensure only existing ones are assigned.
 061        var selectedLabelIds = await _labelStore.FindManyByIdAsync(request.LabelIds, cancellationToken).Select(x => x.Id
 62
 63        // Project selected labels into WorkflowDefinitionLabels.
 064        var newLabels = selectedLabelIds.Select(x => new WorkflowDefinitionLabel
 065        {
 066            Id = _identityGenerator.GenerateId(),
 067            LabelId = x,
 068            WorkflowDefinitionId = workflowDefinition.DefinitionId,
 069            WorkflowDefinitionVersionId = workflowDefinition.Id
 070        }).ToList();
 71
 72        // Get the current labels.
 073        var currentLabels = await _workflowDefinitionLabelStore.FindByWorkflowDefinitionVersionIdAsync(request.Id, cance
 74
 75        // Get a diff between new labels and existing labels.
 076        var diff = Diff.For(currentLabels, newLabels, new WorkflowDefinitionLabelEqualityComparer());
 77
 78        // Replace assigned labels.
 079        await _workflowDefinitionLabelStore.ReplaceAsync(diff.Removed, diff.Added, cancellationToken);
 80
 081        var response = new Response(selectedLabelIds);
 082        await Send.OkAsync(response, cancellationToken);
 083    }
 84
 85}