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 authored and pokornyd committed Aug 8, 2024
1 parent a272aac commit 308705e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Kontent.Ai.Management.Tests/Data/AssetFolder/Folder.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
Expand All @@ -16,6 +18,7 @@
{
"id": "9ca927b6-6e4d-4d6b-81e3-ec5e8f7772a0",
"name": "Legal documents",
"codename": "legal_documents",
"external_id": "folder-documents",
"folders": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public async Task CreateAssetFoldersAsync_CreatesFolder()
{
ExternalId = "external-id",
Name= "name",
Codename = "codename",
Folders = Enumerable.Empty<AssetFolderHierarchy>()
}
}
Expand Down Expand Up @@ -114,6 +115,7 @@ public async Task ModifyAssetFoldersAsync_ChangesAreNull_Throws()
{
ExternalId = "external-id",
Name= "name",
Codename = "codename",
Folders = Enumerable.Empty<AssetFolderHierarchy>()
},
Before = Reference.ByCodename("codename"),
Expand Down
59 changes: 59 additions & 0 deletions Kontent.Ai.Management/Extensions/AssetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ 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 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;
}

/// <summary>
/// Gets the full folder path string
Expand Down Expand Up @@ -147,6 +177,34 @@ 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;
}

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 +224,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 308705e

Please sign in to comment.