Skip to content

Commit 5765b06

Browse files
committed
Add Variants and VariantGroup models with methods
1 parent 6746151 commit 5765b06

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

Contentstack.Management.Core/Models/Stack.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Contentstack.Management.Core.Queryable;
55
using Contentstack.Management.Core.Services.Stack;
66
using Contentstack.Management.Core.Utils;
7-
using Contentstack.Management.Core.Models;
87
using Contentstack.Management.Core.Models.Token;
98

109
namespace Contentstack.Management.Core.Models
@@ -912,6 +911,66 @@ public BulkOperation BulkOperation()
912911

913912
return new BulkOperation(this);
914913
}
914+
915+
/// <summary>
916+
/// Gets the bulk operation instance for performing bulk operations on entries and assets.
917+
/// </summary>
918+
/// <example>
919+
/// <pre><code>
920+
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
921+
/// Stack stack = client.Stack("<API_KEY>");
922+
///
923+
/// var publishDetails = new BulkPublishDetails
924+
/// {
925+
/// Entries = new List<BulkPublishEntry>
926+
/// {
927+
/// new BulkPublishEntry { Uid = "entry_uid", ContentTypeUid = "content_type_uid", Locale = "en-us" }
928+
/// },
929+
/// Locales = new List<string> { "en-us" },
930+
/// Environments = new List<string> { "environment_uid" }
931+
/// };
932+
///
933+
/// ContentstackResponse response = stack.BulkOperation().Publish(publishDetails);
934+
/// </code></pre>
935+
/// </example>
936+
/// <returns>The <see cref="Models.Variants"/></returns>
937+
public Variants Variants(string uid = null)
938+
{
939+
ThrowIfNotLoggedIn();
940+
ThrowIfAPIKeyEmpty();
941+
942+
return new Variants(this, uid);
943+
}
944+
945+
/// <summary>
946+
/// Gets the bulk operation instance for performing bulk operations on entries and assets.
947+
/// </summary>
948+
/// <example>
949+
/// <pre><code>
950+
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
951+
/// Stack stack = client.Stack("<API_KEY>");
952+
///
953+
/// var publishDetails = new BulkPublishDetails
954+
/// {
955+
/// Entries = new List<BulkPublishEntry>
956+
/// {
957+
/// new BulkPublishEntry { Uid = "entry_uid", ContentTypeUid = "content_type_uid", Locale = "en-us" }
958+
/// },
959+
/// Locales = new List<string> { "en-us" },
960+
/// Environments = new List<string> { "environment_uid" }
961+
/// };
962+
///
963+
/// ContentstackResponse response = stack.BulkOperation().Publish(publishDetails);
964+
/// </code></pre>
965+
/// </example>
966+
/// <returns>The <see cref="Models.BulkOperation"/></returns>
967+
public VariantGroup VariantGroup(string uid = null)
968+
{
969+
ThrowIfNotLoggedIn();
970+
ThrowIfAPIKeyEmpty();
971+
972+
return new VariantGroup(this, uid);
973+
}
915974
#endregion
916975

917976
#region Throw Error
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 VariantGroup
9+
{
10+
11+
}
12+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)