Skip to content

Commit

Permalink
Improove getAllChilds gettings from db (page tree functional) (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
AMEST authored Sep 22, 2024
1 parent 9b66120 commit 517e8df
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions src/Mimisbrunnr.Wiki/Services/PageManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Mimisbrunnr.Wiki.Contracts;
using Mimisbrunnr.Wiki.Contracts;
using Skidbladnir.Repository.Abstractions;

namespace Mimisbrunnr.Wiki.Services;
Expand Down Expand Up @@ -37,41 +36,9 @@ public Task<Page[]> FindByName(string name)
.Where(x => x.Name.Contains(name)).ToArrayAsync();
}

public async Task<Page[]> GetAllChilds(Page page, bool lightContract = true)
public Task<Page[]> GetAllChilds(Page page, bool lightContract = true)
{
var flatChildsList = new List<Page>();
var childsQuery = _pageRepository
.GetAll()
.Where(x => x.ParentId == page.Id);
if (lightContract)
childsQuery = childsQuery.Select(x => new Page
{
Id = x.Id,
ParentId = x.ParentId,
SpaceId = x.SpaceId,
Name = x.Name
});

var childs = await childsQuery.ToArrayAsync();
if (childs.Length == 0)
return Array.Empty<Page>();

flatChildsList.AddRange(childs);

var getChildTasks = new List<Task<Page[]>>();
foreach (var child in childs)
getChildTasks.Add(GetAllChilds(child, lightContract));

await Task.WhenAll(getChildTasks);

foreach (var innerChildsTask in getChildTasks)
{
var innerChilds = innerChildsTask.Result;
if (innerChilds != null && innerChilds.Length > 0)
flatChildsList.AddRange(innerChilds);
}

return flatChildsList.ToArray();
return GetAllChilds([page.Id], lightContract);
}

public Task<Page> GetById(string id)
Expand Down Expand Up @@ -238,5 +205,30 @@ private async Task RemoveAllPageVersions(Page page)
foreach (var version in allVersions)
await _historicalPageRepository.Delete(version);
}

private async Task<Page[]> GetAllChilds(string[] pageIds, bool lightContract = true)
{
var childsQuery = _pageRepository
.GetAll()
.Where(x => pageIds.Contains(x.ParentId));
if (lightContract)
childsQuery = childsQuery.Select(x => new Page
{
Id = x.Id,
ParentId = x.ParentId,
SpaceId = x.SpaceId,
Name = x.Name
});

var childs = await childsQuery.ToListAsync();
if (childs.Count == 0)
return Array.Empty<Page>();

var innerChilds = await GetAllChilds(childs.Select(x => x.Id).ToArray(), lightContract);
if (innerChilds.Length > 0)
childs.AddRange(innerChilds);

return childs.ToArray();
}

}

0 comments on commit 517e8df

Please sign in to comment.