187 lines
6.8 KiB
C#
187 lines
6.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Umbraco.Cms.Core.Web;
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
using Newtonsoft.Json;
|
|
using System.Data;
|
|
|
|
|
|
namespace UmbracoDocs.Samples;
|
|
|
|
[ApiController]
|
|
[Route("/umbraco/api/[controller]")]
|
|
public class GetMenuController : Controller
|
|
{
|
|
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
|
|
|
public GetMenuController(IUmbracoContextAccessor umbracoContextAccessor)
|
|
{
|
|
_umbracoContextAccessor = umbracoContextAccessor;
|
|
}
|
|
|
|
[HttpGet("{organization}/{domain}")]
|
|
public IActionResult GetDocumentTree(string organization, string domain)
|
|
{
|
|
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
|
|
|
if (umbracoContext == null)
|
|
{
|
|
return StatusCode(500, "Umbraco context is not available.");
|
|
}
|
|
|
|
DataTable dataTable = new DataTable
|
|
{
|
|
Columns =
|
|
{
|
|
new DataColumn("id", typeof(int)),
|
|
new DataColumn("name", typeof(string)),
|
|
new DataColumn("type", typeof(string)),
|
|
new DataColumn("parentid", typeof(int))
|
|
}
|
|
};
|
|
|
|
var rootContent = umbracoContext.Content?.GetAtRoot();
|
|
|
|
/*var nodes = GetDocumentTreeRecursive(rootContent);
|
|
// Find and filter the tree
|
|
if (nodes != null)
|
|
{
|
|
var filteredNodes = FilterTree(nodes, "szkoleniowcy.online");
|
|
string updatedJson = JsonConvert.SerializeObject(filteredNodes, Formatting.Indented);
|
|
//Console.WriteLine(updatedJson);
|
|
}*/
|
|
|
|
var t = GetMenuTree(rootContent, dataTable);
|
|
|
|
var result = (
|
|
from org in dataTable.AsEnumerable()
|
|
where
|
|
org.Field<string>("type") == "organization" &&
|
|
org.Field<string>("name")!.ToLower() == Uri.UnescapeDataString(organization).ToLower()
|
|
from
|
|
dom in dataTable.AsEnumerable()
|
|
where
|
|
dom.Field<int?>("parentid") == org.Field<int>("id") &&
|
|
dom.Field<string>("type") == "domains" &&
|
|
dom.Field<string>("name")!.ToLower() == Uri.UnescapeDataString(domain).ToLower()
|
|
join child in dataTable.AsEnumerable() on dom.Field<int>("id") equals child.Field<int?>("parentid")
|
|
select child)
|
|
.Union(
|
|
from
|
|
org in dataTable.AsEnumerable()
|
|
where
|
|
org.Field<string>("type") == "organization" &&
|
|
org.Field<string>("name")!.ToLower() == Uri.UnescapeDataString(organization).ToLower()
|
|
from
|
|
dom in dataTable.AsEnumerable()
|
|
where
|
|
dom.Field<int?>("parentid") == org.Field<int>("id") &&
|
|
dom.Field<string>("type") == "domains" &&
|
|
dom.Field<string>("name")!.ToLower() == Uri.UnescapeDataString(domain).ToLower()
|
|
join child in dataTable.AsEnumerable() on dom.Field<int>("id") equals child.Field<int?>("parentid")
|
|
join subChild in dataTable.AsEnumerable() on child.Field<int>("id") equals subChild.Field<int?>("parentid")
|
|
select subChild)
|
|
.Select(child => new
|
|
{
|
|
id = child.Field<int>("id"),
|
|
name = child.Field<string>("name"),
|
|
type = child.Field<string>("type"),
|
|
parentid = child.Field<int?>("parentid")
|
|
});
|
|
|
|
var allIds = result.Select(row => row.id).ToHashSet();
|
|
var modifiedResult = result.Select(row => new
|
|
{
|
|
id = row.id,
|
|
name = row.name,
|
|
type = row.type,
|
|
parentid = row.parentid.HasValue && !allIds.Contains(row.parentid.Value) ? (int?)null : row.parentid
|
|
}).ToList();
|
|
|
|
Console.WriteLine(JsonConvert.SerializeObject(modifiedResult, Formatting.Indented));
|
|
|
|
return Ok(modifiedResult);
|
|
}
|
|
|
|
private List<DocumentTreeNode>? GetDocumentTreeRecursive(IEnumerable<IPublishedContent>? contents)
|
|
{
|
|
if (contents != null)
|
|
{
|
|
return
|
|
contents?.Select(x =>
|
|
new DocumentTreeNode
|
|
{
|
|
id = x.Id,
|
|
name = x.Name,
|
|
type = x.ContentType.Alias,
|
|
parentId = x.Parent?.Id,
|
|
children = GetDocumentTreeRecursive(x.Children)
|
|
}).ToList();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private DataTable? GetMenuTree(IEnumerable<IPublishedContent>? contents, DataTable dataTable)
|
|
{
|
|
if (contents != null)
|
|
{
|
|
contents.ToList().ForEach(x =>
|
|
{
|
|
dataTable.LoadDataRow(new object[]
|
|
{
|
|
x.Id,
|
|
x.Name,
|
|
x.ContentType.Alias,
|
|
x.Parent?.Id ?? null!
|
|
}, LoadOption.PreserveChanges);
|
|
if (x.Children != null && x.Children.Any())
|
|
{
|
|
GetMenuTree(x.Children, dataTable);
|
|
}
|
|
});
|
|
}
|
|
return dataTable;
|
|
}
|
|
|
|
static List<DocumentTreeNode> FilterTree(List<DocumentTreeNode> nodes, string targetName)
|
|
{
|
|
// Find the node with the target name and return its subtree
|
|
foreach (var node in nodes)
|
|
{
|
|
if (node.name == targetName)
|
|
{
|
|
return new List<DocumentTreeNode> { node };
|
|
}
|
|
|
|
if (node.children != null)
|
|
{
|
|
var filteredChildren = FilterTree(node.children, targetName);
|
|
if (filteredChildren.Count > 0)
|
|
{
|
|
// Copy the current node and set its children to the filtered children
|
|
var filteredNode = new DocumentTreeNode
|
|
{
|
|
id = node.id,
|
|
name = node.name,
|
|
type = node.type,
|
|
parentId = node.parentId,
|
|
children = filteredChildren
|
|
};
|
|
|
|
return new List<DocumentTreeNode> { filteredNode };
|
|
}
|
|
}
|
|
}
|
|
|
|
return new List<DocumentTreeNode>();
|
|
}
|
|
|
|
public class DocumentTreeNode
|
|
{
|
|
public int id { get; set; }
|
|
public string? name { get; set; }
|
|
public string? type { get; set; }
|
|
public int? parentId { get; set; }
|
|
public List<DocumentTreeNode>? children { get; set; }
|
|
}
|
|
} |