Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EN-207 Add asset folder codenames #274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading