| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides a set of extension methods for <see cref="Workflow"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | [PublicAPI] |
| | | 12 | | public static class WorkflowExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Returns true if the specified workflow matches the specified version options. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="workflow">The workflow to check.</param> |
| | | 18 | | /// <param name="version">The version options.</param> |
| | | 19 | | /// <returns>True if the workflow matches the version options; otherwise, false.</returns> |
| | | 20 | | public static bool WithVersion(this Workflow workflow, VersionOptions version) |
| | | 21 | | { |
| | 0 | 22 | | var identity = workflow.Identity; |
| | 0 | 23 | | var (isLatest, isPublished) = workflow.Publication; |
| | | 24 | | |
| | 0 | 25 | | if (version.IsDraft) |
| | 0 | 26 | | return !isPublished; |
| | 0 | 27 | | if (version.IsLatest) |
| | 0 | 28 | | return isLatest; |
| | 0 | 29 | | if (version.IsPublished) |
| | 0 | 30 | | return isPublished; |
| | 0 | 31 | | if (version.IsLatestOrPublished) |
| | 0 | 32 | | return isPublished || isLatest; |
| | 0 | 33 | | if (version.AllVersions) |
| | 0 | 34 | | return true; |
| | 0 | 35 | | if (version.Version > 0) |
| | 0 | 36 | | return identity.Version == version.Version; |
| | 0 | 37 | | return true; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Applies the specified version options to the query. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="query">The query to apply the version options to.</param> |
| | | 44 | | /// <param name="version">The version options.</param> |
| | | 45 | | /// <returns>The query.</returns> |
| | | 46 | | public static IEnumerable<Workflow> WithVersion( |
| | | 47 | | this IEnumerable<Workflow> query, |
| | | 48 | | VersionOptions version) => |
| | 0 | 49 | | query.Where(x => x.WithVersion(version)).OrderByDescending(x => x.Identity.Version); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Applies the specified version options to the query. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="query">The query to apply the version options to.</param> |
| | | 55 | | /// <param name="version">The version options.</param> |
| | | 56 | | /// <returns>The query.</returns> |
| | | 57 | | public static IQueryable<Workflow> WithVersion( |
| | | 58 | | this IQueryable<Workflow> query, |
| | | 59 | | VersionOptions version) => |
| | 0 | 60 | | query.Where(x => x.WithVersion(version)).OrderByDescending(x => x.Identity.Version); |
| | | 61 | | } |