< Summary

Information
Class: Elsa.Extensions.VersionedEntityExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/VersionedEntityExtensions.cs
Line coverage
24%
Covered lines: 11
Uncovered lines: 34
Coverable lines: 45
Total lines: 104
Line coverage: 24.4%
Branch coverage
25%
Covered branches: 10
Total branches: 40
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WithVersion(...)0%210140%
WithVersion(...)0%620%
WithVersion(...)83.33%131284.61%
WithVersion(...)0%156120%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/VersionedEntityExtensions.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Elsa.Common.Entities;
 3using Elsa.Common.Models;
 4using LinqKit;
 5
 6// ReSharper disable once CheckNamespace
 7namespace Elsa.Extensions;
 8
 9/// <summary>
 10/// Provides extension methods for <see cref="VersionedEntity"/> objects.
 11/// </summary>
 12public static class VersionedEntityExtensions
 13{
 14    /// <summary>
 15    /// Returns true if the specified entity matches the version options.
 16    /// </summary>
 17    /// <param name="entity">The entity to check.</param>
 18    /// <param name="versionOptions">The version options.</param>
 19    /// <returns>True if the entity matches the version options.</returns>
 20    public static bool WithVersion(this VersionedEntity entity, VersionOptions versionOptions)
 21    {
 022        var isPublished = entity.IsPublished;
 023        var isLatest = entity.IsLatest;
 024        var version = entity.Version;
 25
 026        if (versionOptions.IsDraft)
 027            return !isPublished;
 028        if (versionOptions.IsLatest)
 029            return isLatest;
 030        if (versionOptions.IsPublished)
 031            return isPublished;
 032        if (versionOptions.IsLatestOrPublished)
 033            return isPublished || isLatest;
 034        if (versionOptions.IsLatestAndPublished)
 035            return isPublished && isLatest;
 036        if (versionOptions.AllVersions)
 037            return true;
 038        if (versionOptions.Version > 0)
 039            return version == versionOptions.Version;
 040        return true;
 41    }
 42
 43    /// <summary>
 44    /// Filters the specified enumerable by the version options.
 45    /// </summary>
 46    /// <param name="enumerable">The enumerable to filter.</param>
 47    /// <param name="versionOptions">The version options.</param>
 48    /// <typeparam name="T">The type of the enumerable.</typeparam>
 49    /// <returns>The filtered enumerable.</returns>
 50    public static IEnumerable<T> WithVersion<T>(
 51        this IEnumerable<T> enumerable,
 52        VersionOptions versionOptions) where T : VersionedEntity =>
 053        enumerable.Where(x => x.WithVersion(versionOptions)).OrderByDescending(x => x.Version);
 54
 55    /// <summary>
 56    /// Filters the specified queryable by the version options.
 57    /// </summary>
 58    /// <param name="query">The queryable to filter.</param>
 59    /// <param name="versionOptions">The version options.</param>
 60    /// <typeparam name="T">The type of the queryable.</typeparam>
 61    /// <returns>The filtered queryable.</returns>
 62    public static IQueryable<T> WithVersion<T>(this IQueryable<T> query, VersionOptions versionOptions) where T : Versio
 63    {
 178464        if (versionOptions.IsDraft)
 065            return query.Where(x => !x.IsPublished);
 178466        if (versionOptions.IsLatest)
 58767            return query.Where(x => x.IsLatest);
 119768        if (versionOptions.IsPublished)
 60569            return query.Where(x => x.IsPublished);
 59270        if (versionOptions.IsLatestOrPublished)
 771            return query.Where(x => x.IsPublished || x.IsLatest);
 58572        if (versionOptions.IsLatestAndPublished)
 073            return query.Where(x => x.IsPublished && x.IsLatest);
 58574        if (versionOptions.Version > 0)
 55075            return query.Where(x => x.Version == versionOptions.Version);
 76
 3577        return query;
 78    }
 79
 80    /// <summary>
 81    /// Returns an expression that filters the specified expression by the version options.
 82    /// </summary>
 83    /// <param name="expression">The expression to filter.</param>
 84    /// <param name="versionOptions">The version options.</param>
 85    /// <typeparam name="T">The type of the expression.</typeparam>
 86    /// <returns>The filtered expression.</returns>
 87    public static Expression<Func<T, bool>> WithVersion<T>(this Expression<Func<T, bool>> expression, VersionOptions ver
 88    {
 089        if (versionOptions.IsDraft)
 090            return expression.And(x => !x.IsPublished);
 091        if (versionOptions.IsLatest)
 092            return expression.And(x => x.IsLatest);
 093        if (versionOptions.IsPublished)
 094            return expression.And(x => x.IsPublished);
 095        if (versionOptions.IsLatestOrPublished)
 096            return expression.And(x => x.IsPublished || x.IsLatest);
 097        if (versionOptions.IsLatestAndPublished)
 098            return expression.And(x => x.IsPublished && x.IsLatest);
 099        if (versionOptions.Version > 0)
 0100            return expression.And(x => x.Version == versionOptions.Version);
 101
 0102        return expression;
 103    }
 104}