From 337c067bafeba966cc2b4b668a14e319020e9c99 Mon Sep 17 00:00:00 2001 From: Matus Backor Date: Tue, 30 Jul 2024 15:38:32 +0200 Subject: [PATCH] EN-207 Add asset folder codenames --- .../Extensions/AssetExtensions.cs | 63 +++++++++++++++++++ .../AssetFolders/AssetFolderHierarchy.cs | 6 ++ .../AssetFolderLinkingHierarchy.cs | 6 ++ 3 files changed, 75 insertions(+) diff --git a/Kontent.Ai.Management/Extensions/AssetExtensions.cs b/Kontent.Ai.Management/Extensions/AssetExtensions.cs index c2c372ca..a1df10cc 100644 --- a/Kontent.Ai.Management/Extensions/AssetExtensions.cs +++ b/Kontent.Ai.Management/Extensions/AssetExtensions.cs @@ -71,6 +71,38 @@ 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 itm in folders) + { + if (itm.Codename == codename) + { + return itm; + } + else if (itm.Folders != null) + { + var nestedFolder = itm.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 +179,36 @@ 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; + } + else if (folder.Folders != null) + { + 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 +228,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