diff --git a/Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json b/Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json index e2cb6bbc..ede68583 100644 --- a/Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json +++ b/Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json @@ -3,11 +3,13 @@ { "id": "958001d8-2228-4373-b966-5262b5b96f71", "name": "Downloads", + "codename": "downloads", "external_id": "folder-with-downloadable-assets", "folders": [ { "id": "9ca927b6-6e4d-4d6b-81e3-ec5e8f7772a0", "name": "Archives", + "codename": "archives", "external_id": "folder-with-downloadable-archives", "folders": [] } @@ -16,6 +18,7 @@ { "id": "9ca927b6-6e4d-4d6b-81e3-ec5e8f7772a0", "name": "Legal documents", + "codename": "legal_documents", "external_id": "folder-documents", "folders": [] } diff --git a/Kontent.Ai.Management.Tests/ManagementClientTests/AssetFolderTests.cs b/Kontent.Ai.Management.Tests/ManagementClientTests/AssetFolderTests.cs index 37120653..643fc1d6 100644 --- a/Kontent.Ai.Management.Tests/ManagementClientTests/AssetFolderTests.cs +++ b/Kontent.Ai.Management.Tests/ManagementClientTests/AssetFolderTests.cs @@ -55,6 +55,7 @@ public async Task CreateAssetFoldersAsync_CreatesFolder() { ExternalId = "external-id", Name= "name", + Codename = "codename", Folders = Enumerable.Empty() } } @@ -114,6 +115,7 @@ public async Task ModifyAssetFoldersAsync_ChangesAreNull_Throws() { ExternalId = "external-id", Name= "name", + Codename = "codename", Folders = Enumerable.Empty() }, Before = Reference.ByCodename("codename"), diff --git a/Kontent.Ai.Management/Extensions/AssetExtensions.cs b/Kontent.Ai.Management/Extensions/AssetExtensions.cs index c2c372ca..03fa5010 100644 --- a/Kontent.Ai.Management/Extensions/AssetExtensions.cs +++ b/Kontent.Ai.Management/Extensions/AssetExtensions.cs @@ -71,6 +71,36 @@ public static AssetFolderHierarchy GetFolderHierarchyByExternalId(this IEnumerab } return null; } + + /// + /// Gets folder hierarchy for a given folder codename + /// + /// The property retrieved from the method. + /// Folder codename + /// The instance that represents the folder found for a given folder codename. Null if not found. + public static AssetFolderHierarchy GetFolderHierarchyByCodename(this IEnumerable folders, string codename) + { + if (folders == null) + { + return null; + } + + // Recursively search for the folder hierarchy that an asset is in. Returns null if file is not in a folder. + foreach (var folder in folders) + { + if (folder.Codename == codename) + { + return folder; + } + + var nestedFolder = folder.Folders?.GetFolderHierarchyByCodename(codename); + if (nestedFolder != null) //This is required so you don't stop processing if the root contains many folders (let the above foreach loop continue) + { + return nestedFolder; + } + } + return null; + } /// /// Gets the full folder path string @@ -147,6 +177,34 @@ public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByExtern } return null; } + + /// + /// Gets the folder hierarchy for a given folder identifier. + /// To use this method first convert your property retrieved from to a IEnumerable<AssetFolderLinkingHierarchy> by using the method. + /// + /// The instance. + /// Folder codename + /// Returns the instance found via a given folder identifier. + public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByCodename(this IEnumerable folders, string codename) + { + if (folders != null) + { + foreach (var folder in folders) + { + if (folder.Codename == codename) + { + return folder; + } + + var nestedFolder = folder.Folders?.GetParentLinkedFolderHierarchyByCodename(codename); + if (nestedFolder != null) // This is required so you don't stop processing if the root contains many folders (let the above for-each loop continue) + { + return nestedFolder; + } + } + } + return null; + } /// /// Retrieves a list of folders with the property filled in. @@ -166,6 +224,7 @@ public static IEnumerable GetParentLinkedFolderHier ExternalId = itm.ExternalId, Folders = itm.Folders != null ? new List() : null, Id = itm.Id, + Codename = itm.Codename, Name = itm.Name }; if (itm.Folders != null) diff --git a/Kontent.Ai.Management/Models/AssetFolders/AssetFolderHierarchy.cs b/Kontent.Ai.Management/Models/AssetFolders/AssetFolderHierarchy.cs index 71b3e4d4..16832043 100644 --- a/Kontent.Ai.Management/Models/AssetFolders/AssetFolderHierarchy.cs +++ b/Kontent.Ai.Management/Models/AssetFolders/AssetFolderHierarchy.cs @@ -19,6 +19,12 @@ public sealed class AssetFolderHierarchy /// [JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)] public string ExternalId { get; set; } + + /// + /// Gets or sets the codename of the folder. + /// + [JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)] + public string Codename { get; set; } /// /// Gets or sets the name of the folder diff --git a/Kontent.Ai.Management/Models/AssetFolders/AssetFolderLinkingHierarchy.cs b/Kontent.Ai.Management/Models/AssetFolders/AssetFolderLinkingHierarchy.cs index 25f109da..d9e16de4 100644 --- a/Kontent.Ai.Management/Models/AssetFolders/AssetFolderLinkingHierarchy.cs +++ b/Kontent.Ai.Management/Models/AssetFolders/AssetFolderLinkingHierarchy.cs @@ -21,6 +21,12 @@ public sealed class AssetFolderLinkingHierarchy /// [JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)] public string ExternalId { get; set; } + + /// + /// Gets or sets the folder's codename. + /// + [JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)] + public string Codename { get; set; } /// /// Name of the folder