| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using LinqKit; |
| | | 5 | | |
| | | 6 | | // ReSharper disable once CheckNamespace |
| | | 7 | | namespace Elsa.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides extension methods for <see cref="VersionedEntity"/> objects. |
| | | 11 | | /// </summary> |
| | | 12 | | public 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 | | { |
| | 0 | 22 | | var isPublished = entity.IsPublished; |
| | 0 | 23 | | var isLatest = entity.IsLatest; |
| | 0 | 24 | | var version = entity.Version; |
| | | 25 | | |
| | 0 | 26 | | if (versionOptions.IsDraft) |
| | 0 | 27 | | return !isPublished; |
| | 0 | 28 | | if (versionOptions.IsLatest) |
| | 0 | 29 | | return isLatest; |
| | 0 | 30 | | if (versionOptions.IsPublished) |
| | 0 | 31 | | return isPublished; |
| | 0 | 32 | | if (versionOptions.IsLatestOrPublished) |
| | 0 | 33 | | return isPublished || isLatest; |
| | 0 | 34 | | if (versionOptions.IsLatestAndPublished) |
| | 0 | 35 | | return isPublished && isLatest; |
| | 0 | 36 | | if (versionOptions.AllVersions) |
| | 0 | 37 | | return true; |
| | 0 | 38 | | if (versionOptions.Version > 0) |
| | 0 | 39 | | return version == versionOptions.Version; |
| | 0 | 40 | | 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 => |
| | 0 | 53 | | 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 | | { |
| | 1784 | 64 | | if (versionOptions.IsDraft) |
| | 0 | 65 | | return query.Where(x => !x.IsPublished); |
| | 1784 | 66 | | if (versionOptions.IsLatest) |
| | 587 | 67 | | return query.Where(x => x.IsLatest); |
| | 1197 | 68 | | if (versionOptions.IsPublished) |
| | 605 | 69 | | return query.Where(x => x.IsPublished); |
| | 592 | 70 | | if (versionOptions.IsLatestOrPublished) |
| | 7 | 71 | | return query.Where(x => x.IsPublished || x.IsLatest); |
| | 585 | 72 | | if (versionOptions.IsLatestAndPublished) |
| | 0 | 73 | | return query.Where(x => x.IsPublished && x.IsLatest); |
| | 585 | 74 | | if (versionOptions.Version > 0) |
| | 550 | 75 | | return query.Where(x => x.Version == versionOptions.Version); |
| | | 76 | | |
| | 35 | 77 | | 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 | | { |
| | 0 | 89 | | if (versionOptions.IsDraft) |
| | 0 | 90 | | return expression.And(x => !x.IsPublished); |
| | 0 | 91 | | if (versionOptions.IsLatest) |
| | 0 | 92 | | return expression.And(x => x.IsLatest); |
| | 0 | 93 | | if (versionOptions.IsPublished) |
| | 0 | 94 | | return expression.And(x => x.IsPublished); |
| | 0 | 95 | | if (versionOptions.IsLatestOrPublished) |
| | 0 | 96 | | return expression.And(x => x.IsPublished || x.IsLatest); |
| | 0 | 97 | | if (versionOptions.IsLatestAndPublished) |
| | 0 | 98 | | return expression.And(x => x.IsPublished && x.IsLatest); |
| | 0 | 99 | | if (versionOptions.Version > 0) |
| | 0 | 100 | | return expression.And(x => x.Version == versionOptions.Version); |
| | | 101 | | |
| | 0 | 102 | | return expression; |
| | | 103 | | } |
| | | 104 | | } |