| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Common.Converters; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Common.Models; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides parameters for querying a given version of versioned entities. |
| | | 11 | | /// </summary> |
| | | 12 | | [TypeConverter(typeof(VersionOptionsTypeConverter))] |
| | | 13 | | [JsonConverter(typeof(VersionOptionsJsonConverter))] |
| | | 14 | | [PublicAPI] |
| | | 15 | | public struct VersionOptions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the latest version. |
| | | 19 | | /// </summary> |
| | 1 | 20 | | public static readonly VersionOptions Latest = new() { IsLatest = true }; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the published version. |
| | | 24 | | /// </summary> |
| | 1 | 25 | | public static readonly VersionOptions Published = new() { IsPublished = true }; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the published version if available, and if not, the latest version. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public static readonly VersionOptions LatestOrPublished = new() { IsLatestOrPublished = true }; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the latest, published version. |
| | | 34 | | /// </summary> |
| | 1 | 35 | | public static readonly VersionOptions LatestAndPublished = new() { IsLatestAndPublished = true }; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the draft version. |
| | | 39 | | /// </summary> |
| | 1 | 40 | | public static readonly VersionOptions Draft = new() { IsDraft = true }; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets all versions. |
| | | 44 | | /// </summary> |
| | 1 | 45 | | public static readonly VersionOptions All = new() { AllVersions = true }; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets a specific version. |
| | | 49 | | /// </summary> |
| | 574 | 50 | | public static VersionOptions SpecificVersion(int version) => new() { Version = version }; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Parses a string into a <see cref="VersionOptions"/>. |
| | | 54 | | /// </summary> |
| | | 55 | | public static VersionOptions FromString(string value) => |
| | 0 | 56 | | value switch |
| | 0 | 57 | | { |
| | 0 | 58 | | "AllVersions" => All, |
| | 0 | 59 | | "Draft" => Draft, |
| | 0 | 60 | | "Latest" => Latest, |
| | 0 | 61 | | "Published" => Published, |
| | 0 | 62 | | "LatestOrPublished" => LatestOrPublished, |
| | 0 | 63 | | "LatestAndPublished" => LatestAndPublished, |
| | 0 | 64 | | _ => SpecificVersion(int.Parse(value, CultureInfo.InvariantCulture)) |
| | 0 | 65 | | }; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Tries to parse a string into a <see cref="VersionOptions"/>. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="value">The string to parse.</param> |
| | | 71 | | /// <param name="versionOptions">The parsed <see cref="VersionOptions"/>.</param> |
| | | 72 | | /// <returns>True if the string could be parsed, otherwise false.</returns> |
| | | 73 | | public static bool TryParse(string value, out VersionOptions versionOptions) |
| | | 74 | | { |
| | 0 | 75 | | versionOptions = FromString(value); |
| | 0 | 76 | | return true; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the latest version. |
| | | 81 | | /// </summary> |
| | 3630 | 82 | | public bool IsLatest { get; private set; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets the published version if available, and if not, the latest version. |
| | | 86 | | /// </summary> |
| | 1175 | 87 | | public bool IsLatestOrPublished { get; private set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the latest, published version. |
| | | 91 | | /// </summary> |
| | 1160 | 92 | | public bool IsLatestAndPublished { get; private set; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets the published version. |
| | | 96 | | /// </summary> |
| | 2452 | 97 | | public bool IsPublished { get; private set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets the draft version. |
| | | 101 | | /// </summary> |
| | 3630 | 102 | | public bool IsDraft { get; private set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets all versions. |
| | | 106 | | /// </summary> |
| | 1911 | 107 | | public bool AllVersions { get; private set; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets a specific version. |
| | | 111 | | /// </summary> |
| | 2283 | 112 | | public int Version { get; private set; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Returns a simple string representation of this <see cref="VersionOptions"/>. |
| | | 116 | | /// </summary> |
| | 1910 | 117 | | public override string ToString() => AllVersions ? "AllVersions" : IsDraft ? "Draft" : IsLatest ? "Latest" : IsPubli |
| | | 118 | | } |