using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Core.Web; using Umbraco.Cms.Core.Models.PublishedContent; using Newtonsoft.Json; using System.Data; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System.Text.Json; using Umbraco.Cms.Core; using System.Reflection; using System.Dynamic; namespace UmbracoDocs.Samples; [ApiController] [Route("/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("slug", typeof(string)), new DataColumn("parentid", typeof(int)), new DataColumn("position", typeof(int)) { AutoIncrement = true, AutoIncrementSeed = 1, // Początkowa wartość AutoIncrementStep = 1 // Krok zwiększania } } }; 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("type") == "organization" && org.Field("name")!.ToLower() == Uri.UnescapeDataString(organization).ToLower() from dom in dataTable.AsEnumerable() where dom.Field("parentid") == org.Field("id") && dom.Field("type") == "domains" && dom.Field("name")!.ToLower() == Uri.UnescapeDataString(domain).ToLower() join child in dataTable.AsEnumerable() on dom.Field("id") equals child.Field("parentid") select child) .Union( from org in dataTable.AsEnumerable() where org.Field("type") == "organization" && org.Field("name")!.ToLower() == Uri.UnescapeDataString(organization).ToLower() from dom in dataTable.AsEnumerable() where dom.Field("parentid") == org.Field("id") && dom.Field("type") == "domains" && dom.Field("name")!.ToLower() == Uri.UnescapeDataString(domain).ToLower() join child in dataTable.AsEnumerable() on dom.Field("id") equals child.Field("parentid") join subChild in dataTable.AsEnumerable() on child.Field("id") equals subChild.Field("parentid") select subChild) .Select(child => new { id = child.Field("id"), name = child.Field("name"), slug = child.Field("slug"), type = child.Field("type"), parentid = child.Field("parentid"), position = child.Field("position") }); var allIds = result.Select(row => row.id).ToHashSet(); var modifiedResult = result.Select(row => new { id = row.id, name = row.name, type = row.type, slug = row.slug, parentid = row.parentid.HasValue && !allIds.Contains(row.parentid.Value) ? (int?)null : row.parentid, position = row.position }).ToList(); Console.WriteLine(JsonConvert.SerializeObject(modifiedResult, Formatting.Indented)); return Ok(modifiedResult); } private List? GetDocumentTreeRecursive(IEnumerable? 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? contents, DataTable dataTable) { if (contents != null) { contents.ToList().ForEach(x => { dataTable.LoadDataRow(new object[] { x.Id, x.Name, x.ContentType.Alias, x.Value("slug") ?? null!, x.Parent?.Id ?? null!, }, LoadOption.PreserveChanges); if (x.Children != null && x.Children.Any()) { GetMenuTree(x.Children, dataTable); } }); } return dataTable; } static List FilterTree(List 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 { 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 { filteredNode }; } } } return new List(); } 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? children { get; set; } } }