< Summary

Information
Class: Elsa.Tenants.AspNetCore.Services.TenantPrefixHttpEndpointRoutesProvider
Assembly: Elsa.Tenants.AspNetCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Services/TenantPrefixHttpEndpointRoutesProvider.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 64
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetRoutesAsync()0%620%
AddTenantRouteAsync()0%4260%
AddDefaultRoute(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Services/TenantPrefixHttpEndpointRoutesProvider.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Extensions;
 3using Elsa.Http;
 4using Elsa.Http.Contexts;
 5using Elsa.Http.Options;
 6using JetBrains.Annotations;
 7using Microsoft.AspNetCore.Routing;
 8using Microsoft.Extensions.Options;
 9
 10namespace Elsa.Tenants.AspNetCore.Services;
 11
 12/// <summary>
 13/// Provides a route for an HTTP endpoint based on the specified context and tenant prefix.
 14/// </summary>
 15[UsedImplicitly]
 016public class TenantPrefixHttpEndpointRoutesProvider(ITenantsProvider tenantsProvider, IOptions<HttpActivityOptions> opti
 17{
 18    public async Task<IEnumerable<HttpRouteData>> GetRoutesAsync(HttpEndpointRouteProviderContext context)
 19    {
 020        var routes = new List<HttpRouteData>();
 021        var path = context.Payload.Path;
 22
 023        if (string.IsNullOrWhiteSpace(path))
 024            return routes;
 25
 026        await AddTenantRouteAsync(context, routes);
 027        AddDefaultRoute(context, routes);
 028        return routes;
 029    }
 30
 31    private async Task AddTenantRouteAsync(HttpEndpointRouteProviderContext context, List<HttpRouteData> routes)
 32    {
 033        var segments = new List<string?>();
 034        var routeDataValues = new RouteValueDictionary();
 35
 036        if (!string.IsNullOrEmpty(context.TenantId))
 37        {
 038            var tenant = await tenantsProvider.FindByIdAsync(context.TenantId);
 039            var tenantPrefix = tenant?.GetRoutePrefix();
 40
 041            if (!string.IsNullOrWhiteSpace(tenantPrefix))
 42            {
 043                segments.Add("{tenantPrefix}");
 044                routeDataValues.Add("tenantId", tenant!.Id);
 45            }
 46        }
 47
 048        var path = context.Payload.Path;
 049        segments.AddRange(options.Value.BasePath.ToString(), path);
 050        var routeData = new HttpRouteData(segments.JoinSegments(), routeDataValues);
 051        routes.Add(routeData);
 052    }
 53
 54    private void AddDefaultRoute(HttpEndpointRouteProviderContext context, List<HttpRouteData> routes)
 55    {
 056        var basePath = options.Value.BasePath.ToString();
 057        var path = context.Payload.Path;
 058        var routeData = new HttpRouteData(new[]
 059        {
 060            basePath, path
 061        }.JoinSegments());
 062        routes.Add(routeData);
 063    }
 64}