Skip to content

Commit

Permalink
EN-207 Add asset folder codenames
Browse files Browse the repository at this point in the history
  • Loading branch information
matus12 committed Jul 30, 2024
1 parent a272aac commit 337c067
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Kontent.Ai.Management/Extensions/AssetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ public static AssetFolderHierarchy GetFolderHierarchyByExternalId(this IEnumerab
}
return null;
}

/// <summary>
/// Gets folder hierarchy for a given folder codename
/// </summary>
/// <param name="folders">The <see cref="AssetFoldersModel.Folders"/> property retrieved from the <see cref="IManagementClient.GetAssetFoldersAsync"/> method.</param>
/// <param name="codename">Folder codename</param>
/// <returns>The <see cref="AssetFolderHierarchy"/> instance that represents the folder found for a given folder codename. Null if not found.</returns>
public static AssetFolderHierarchy GetFolderHierarchyByCodename(this IEnumerable<AssetFolderHierarchy> 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;
}

/// <summary>
/// Gets the full folder path string
Expand Down Expand Up @@ -147,6 +179,36 @@ public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByExtern
}
return null;
}

/// <summary>
/// Gets the folder hierarchy for a given folder identifier.
/// To use this method first convert your <see cref="AssetFoldersModel.Folders"/> property retrieved from <see cref="IManagementClient.GetAssetFoldersAsync"/> to a <see cref="IEnumerable{AssetFolderLinkingHierarchy}">IEnumerable&lt;AssetFolderLinkingHierarchy&gt;</see> by using the <see cref="GetParentLinkedFolderHierarchy"/> method.
/// </summary>
/// <param name="folders">The <see cref="IEnumerable{AssetFolderLinkingHierarchy}"/> instance.</param>
/// <param name="codename">Folder codename</param>
/// <returns>Returns the <see cref="AssetFolderLinkingHierarchy"/> instance found via a given folder identifier.</returns>
public static AssetFolderLinkingHierarchy GetParentLinkedFolderHierarchyByCodename(this IEnumerable<AssetFolderLinkingHierarchy> 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;
}

/// <summary>
/// Retrieves a list of folders with the <see cref="AssetFolderLinkingHierarchy.Parent"/> property filled in.
Expand All @@ -166,6 +228,7 @@ public static IEnumerable<AssetFolderLinkingHierarchy> GetParentLinkedFolderHier
ExternalId = itm.ExternalId,
Folders = itm.Folders != null ? new List<AssetFolderLinkingHierarchy>() : null,
Id = itm.Id,
Codename = itm.Codename,
Name = itm.Name
};
if (itm.Folders != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public sealed class AssetFolderHierarchy
/// </summary>
[JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ExternalId { get; set; }

/// <summary>
/// Gets or sets the codename of the folder.
/// </summary>
[JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Codename { get; set; }

/// <summary>
/// Gets or sets the name of the folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public sealed class AssetFolderLinkingHierarchy
/// </summary>
[JsonProperty("external_id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ExternalId { get; set; }

/// <summary>
/// Gets or sets the folder's codename.
/// </summary>
[JsonProperty("codename", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Codename { get; set; }

/// <summary>
/// Name of the folder
Expand Down

0 comments on commit 337c067

Please sign in to comment.