|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Contentstack.Management.Core.Queryable; |
| 4 | +using Contentstack.Management.Core.Services.Models; |
| 5 | + |
| 6 | +namespace Contentstack.Management.Core.Models |
| 7 | +{ |
| 8 | + public class Variants |
| 9 | + { |
| 10 | + internal Stack stack; |
| 11 | + public string Uid { get; set; } |
| 12 | + |
| 13 | + internal string resourcePath; |
| 14 | + |
| 15 | + internal Variants(Stack stack, string uid = null) |
| 16 | + { |
| 17 | + stack.ThrowIfAPIKeyEmpty(); |
| 18 | + |
| 19 | + this.stack = stack; |
| 20 | + Uid = uid; |
| 21 | + resourcePath = uid == null ? "/variants" : $"/variants/{uid}"; |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The Delete variants call is used to delete a specific variants. |
| 26 | + /// </summary> |
| 27 | + /// <param name="collection">Query parameter</param> |
| 28 | + /// <example> |
| 29 | + /// <pre><code> |
| 30 | + /// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>"); |
| 31 | + /// ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").Variants("<Variants_UID>").Delete(); |
| 32 | + /// </code></pre> |
| 33 | + /// </example> |
| 34 | + /// <returns>The <see cref="ContentstackResponse"/>.</returns> |
| 35 | + public virtual ContentstackResponse Delete() |
| 36 | + { |
| 37 | + stack.ThrowIfNotLoggedIn(); |
| 38 | + ThrowIfUidEmpty(); |
| 39 | + |
| 40 | + var service = new FetchDeleteService(stack.client.serializer, stack, resourcePath, "DELETE"); |
| 41 | + return stack.client.InvokeSync(service); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The Delete variants call is used to delete a specific variants. |
| 46 | + /// </summary> |
| 47 | + /// <param name="collection">Query parameter</param> |
| 48 | + /// <example> |
| 49 | + /// <pre><code> |
| 50 | + /// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>"); |
| 51 | + /// ContentstackResponse contentstackResponse = await client.Stack("<API_KEY>").Variants("<Variants_UID>").DeleteAsync(); |
| 52 | + /// </code></pre> |
| 53 | + /// </example> |
| 54 | + /// <returns>The <see cref="ContentstackResponse"/>.</returns> |
| 55 | + public virtual Task<ContentstackResponse> DeleteAsync() |
| 56 | + { |
| 57 | + stack.ThrowIfNotLoggedIn(); |
| 58 | + ThrowIfUidEmpty(); |
| 59 | + |
| 60 | + var service = new FetchDeleteService(stack.client.serializer, stack, resourcePath, "DELETE"); |
| 61 | + return stack.client.InvokeAsync<FetchDeleteService, ContentstackResponse>(service, true); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Retrieves a specific variant by UID. |
| 66 | + /// </summary> |
| 67 | + /// <param name="collection">Optional query parameters.</param> |
| 68 | + /// <example> |
| 69 | + /// <pre><code> |
| 70 | + /// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>"); |
| 71 | + /// ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").Variants("<Variants_UID>").Fetch(); |
| 72 | + /// </code></pre> |
| 73 | + /// </example> |
| 74 | + /// <returns>Variant data in <see cref="ContentstackResponse"/>.</returns> |
| 75 | + public virtual ContentstackResponse Fetch(ParameterCollection collection = null) |
| 76 | + { |
| 77 | + stack.ThrowIfNotLoggedIn(); |
| 78 | + ThrowIfUidEmpty(); |
| 79 | + |
| 80 | + var service = new FetchDeleteService(stack.client.serializer, stack, resourcePath, collection: collection); |
| 81 | + return stack.client.InvokeSync(service); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Asynchronously retrieves a specific variant by UID. |
| 86 | + /// </summary> |
| 87 | + /// <param name="collection">Optional query parameters.</param> |
| 88 | + /// <example> |
| 89 | + /// <pre><code> |
| 90 | + /// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>"); |
| 91 | + /// ContentstackResponse contentstackResponse = await client.Stack("<API_KEY>").Variants("<Variants_UID>").FetchAsync(); |
| 92 | + /// </code></pre> |
| 93 | + /// </example> |
| 94 | + /// <returns>Task containing variant data in <see cref="ContentstackResponse"/>.</returns> |
| 95 | + public virtual Task<ContentstackResponse> FetchAsync(ParameterCollection collection = null) |
| 96 | + { |
| 97 | + stack.ThrowIfNotLoggedIn(); |
| 98 | + ThrowIfUidEmpty(); |
| 99 | + |
| 100 | + var service = new FetchDeleteService(stack.client.serializer, stack, resourcePath, collection: collection); |
| 101 | + return stack.client.InvokeAsync<FetchDeleteService, ContentstackResponse>(service); |
| 102 | + } |
| 103 | + |
| 104 | + #region Internal Validation |
| 105 | + |
| 106 | + /// <summary>Validates no UID is set for collection operations.</summary> |
| 107 | + internal void ThrowIfUidNotEmpty() |
| 108 | + { |
| 109 | + if (!string.IsNullOrEmpty(this.Uid)) |
| 110 | + { |
| 111 | + throw new InvalidOperationException("Operation not allowed."); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary>Validates UID is set for specific variant operations.</summary> |
| 116 | + internal void ThrowIfUidEmpty() |
| 117 | + { |
| 118 | + if (string.IsNullOrEmpty(this.Uid)) |
| 119 | + { |
| 120 | + throw new InvalidOperationException("Uid can not be empty."); |
| 121 | + } |
| 122 | + } |
| 123 | + #endregion |
| 124 | + } |
| 125 | +} |
0 commit comments