| | | 1 | | using Chronicis.Client.Models; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.DTOs.Maps; |
| | | 4 | | using Chronicis.Shared.DTOs.Sessions; |
| | | 5 | | using Chronicis.Shared.Enums; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Client.Services.Tree; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Responsible for building the navigation tree structure from API data. |
| | | 11 | | /// Fetches worlds, campaigns, arcs, and articles and constructs the hierarchical tree. |
| | | 12 | | /// </summary> |
| | | 13 | | internal sealed class TreeDataBuilder |
| | | 14 | | { |
| | | 15 | | private readonly IArticleApiService _articleApi; |
| | | 16 | | private readonly IWorldApiService _worldApi; |
| | | 17 | | private readonly ICampaignApiService _campaignApi; |
| | | 18 | | private readonly IArcApiService _arcApi; |
| | | 19 | | private readonly ISessionApiService _sessionApi; |
| | | 20 | | private readonly IMapApiService _mapApi; |
| | | 21 | | private readonly ILogger _logger; |
| | | 22 | | |
| | | 23 | | public TreeDataBuilder( |
| | | 24 | | IArticleApiService articleApi, |
| | | 25 | | IWorldApiService worldApi, |
| | | 26 | | ICampaignApiService campaignApi, |
| | | 27 | | IArcApiService arcApi, |
| | | 28 | | ISessionApiService sessionApi, |
| | | 29 | | IMapApiService mapApi, |
| | | 30 | | ILogger logger) |
| | | 31 | | { |
| | 24 | 32 | | _articleApi = articleApi; |
| | 24 | 33 | | _worldApi = worldApi; |
| | 24 | 34 | | _campaignApi = campaignApi; |
| | 24 | 35 | | _arcApi = arcApi; |
| | 24 | 36 | | _sessionApi = sessionApi; |
| | 24 | 37 | | _mapApi = mapApi; |
| | 24 | 38 | | _logger = logger; |
| | 24 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Result of building the tree, containing the node index and cached articles. |
| | | 43 | | /// </summary> |
| | | 44 | | public sealed class BuildResult |
| | | 45 | | { |
| | | 46 | | public required TreeNodeIndex NodeIndex { get; init; } |
| | | 47 | | public required List<ArticleTreeDto> CachedArticles { get; init; } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Builds the complete tree structure by fetching all data from APIs. |
| | | 52 | | /// </summary> |
| | | 53 | | public async Task<BuildResult> BuildTreeAsync() |
| | | 54 | | { |
| | | 55 | | var nodeIndex = new TreeNodeIndex(); |
| | | 56 | | var stopwatch = System.Diagnostics.Stopwatch.StartNew(); |
| | | 57 | | |
| | | 58 | | // Phase 1: Fetch worlds and articles in parallel |
| | | 59 | | var worldsTask = _worldApi.GetWorldsAsync(); |
| | | 60 | | var articlesTask = _articleApi.GetAllArticlesAsync(); |
| | | 61 | | |
| | | 62 | | await Task.WhenAll(worldsTask, articlesTask); |
| | | 63 | | |
| | | 64 | | var worlds = worldsTask.Result; |
| | | 65 | | var allArticles = articlesTask.Result; |
| | | 66 | | |
| | | 67 | | if (!worlds.Any()) |
| | | 68 | | { |
| | | 69 | | return new BuildResult |
| | | 70 | | { |
| | | 71 | | NodeIndex = nodeIndex, |
| | | 72 | | CachedArticles = allArticles |
| | | 73 | | }; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // Phase 2: Fetch all world details in parallel |
| | | 77 | | _logger.LogDebug("Phase 2: Fetching world details in parallel..."); |
| | | 78 | | var worldDetailTasks = worlds.Select(w => _worldApi.GetWorldAsync(w.Id)).ToList(); |
| | | 79 | | var worldDetails = await Task.WhenAll(worldDetailTasks); |
| | | 80 | | |
| | | 81 | | var worldDetailLookup = worldDetails |
| | | 82 | | .Where(wd => wd != null) |
| | | 83 | | .ToDictionary(wd => wd!.Id, wd => wd!); |
| | | 84 | | |
| | | 85 | | _logger.LogDebug("Phase 2 complete: {Count} world details in {Ms}ms", |
| | | 86 | | worldDetailLookup.Count, stopwatch.ElapsedMilliseconds); |
| | | 87 | | |
| | | 88 | | // Phase 3: Collect all campaigns and fetch arcs, links, documents in parallel |
| | | 89 | | var allCampaigns = worldDetails |
| | | 90 | | .Where(wd => wd != null) |
| | | 91 | | .SelectMany(wd => wd!.Campaigns ?? new List<CampaignDto>()) |
| | | 92 | | .ToList(); |
| | | 93 | | |
| | | 94 | | _logger.LogDebug("Phase 3: Fetching arcs for {Count} campaigns, world links, and documents in parallel...", allC |
| | | 95 | | |
| | | 96 | | var arcTasks = allCampaigns.Select(c => _arcApi.GetArcsByCampaignAsync(c.Id)).ToList(); |
| | | 97 | | var linkTasks = worlds.Select(w => _worldApi.GetWorldLinksAsync(w.Id)).ToList(); |
| | | 98 | | var documentTasks = worlds.Select(w => _worldApi.GetWorldDocumentsAsync(w.Id)).ToList(); |
| | | 99 | | var mapTasks = worlds.Select(w => _mapApi.ListMapsForWorldAsync(w.Id)).ToList(); |
| | | 100 | | |
| | | 101 | | await Task.WhenAll( |
| | | 102 | | Task.WhenAll(arcTasks), |
| | | 103 | | Task.WhenAll(linkTasks), |
| | | 104 | | Task.WhenAll(documentTasks), |
| | | 105 | | Task.WhenAll(mapTasks) |
| | | 106 | | ); |
| | | 107 | | |
| | | 108 | | var arcResults = arcTasks.Select(t => t.Result).ToArray(); |
| | | 109 | | |
| | | 110 | | var linksByWorld = new Dictionary<Guid, List<WorldLinkDto>>(); |
| | | 111 | | for (int i = 0; i < worlds.Count; i++) |
| | | 112 | | { |
| | | 113 | | linksByWorld[worlds[i].Id] = linkTasks[i].Result; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | var documentsByWorld = new Dictionary<Guid, List<WorldDocumentDto>>(); |
| | | 117 | | for (int i = 0; i < worlds.Count; i++) |
| | | 118 | | { |
| | | 119 | | documentsByWorld[worlds[i].Id] = documentTasks[i].Result; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | var mapsByWorld = new Dictionary<Guid, List<MapSummaryDto>>(); |
| | | 123 | | for (int i = 0; i < worlds.Count; i++) |
| | | 124 | | { |
| | | 125 | | mapsByWorld[worlds[i].Id] = mapTasks[i].Result; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | var arcsByCampaign = new Dictionary<Guid, List<ArcDto>>(); |
| | | 129 | | for (int i = 0; i < allCampaigns.Count; i++) |
| | | 130 | | { |
| | | 131 | | arcsByCampaign[allCampaigns[i].Id] = arcResults[i]; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // Phase 3b: Fetch sessions for all arcs in parallel |
| | | 135 | | var allArcs = arcResults.SelectMany(r => r).ToList(); |
| | | 136 | | var sessionTasks = allArcs.Select(a => _sessionApi.GetSessionsByArcAsync(a.Id)).ToList(); |
| | | 137 | | await Task.WhenAll(sessionTasks); |
| | | 138 | | |
| | | 139 | | var sessionsByArc = new Dictionary<Guid, List<SessionTreeDto>>(); |
| | | 140 | | for (int i = 0; i < allArcs.Count; i++) |
| | | 141 | | { |
| | | 142 | | sessionsByArc[allArcs[i].Id] = sessionTasks[i].Result; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | _logger.LogDebug("Phase 3 complete: {Count} total arcs in {Ms}ms", |
| | | 146 | | arcResults.Sum(r => r.Count), stopwatch.ElapsedMilliseconds); |
| | | 147 | | |
| | | 148 | | // Phase 4: Build the tree structure |
| | | 149 | | _logger.LogDebug("Phase 4: Building tree structure..."); |
| | | 150 | | |
| | | 151 | | var articleIndex = allArticles.ToDictionary(a => a.Id); |
| | | 152 | | |
| | | 153 | | foreach (var world in worlds.OrderBy(w => w.Name)) |
| | | 154 | | { |
| | | 155 | | if (!worldDetailLookup.TryGetValue(world.Id, out var worldDetail)) |
| | | 156 | | { |
| | | 157 | | _logger.LogWarning("World detail not found for {WorldId}", world.Id); |
| | | 158 | | continue; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | var worldLinks = linksByWorld[world.Id]; |
| | | 162 | | var worldDocuments = documentsByWorld[world.Id]; |
| | | 163 | | |
| | | 164 | | var worldMaps = mapsByWorld[world.Id]; |
| | | 165 | | |
| | | 166 | | var worldNode = BuildWorldNode( |
| | | 167 | | world, |
| | | 168 | | worldDetail, |
| | | 169 | | allArticles, |
| | | 170 | | articleIndex, |
| | | 171 | | arcsByCampaign, |
| | | 172 | | sessionsByArc, |
| | | 173 | | worldLinks, |
| | | 174 | | worldDocuments, |
| | | 175 | | worldMaps, |
| | | 176 | | nodeIndex); |
| | | 177 | | |
| | | 178 | | nodeIndex.AddRootNode(worldNode); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | // Handle orphan articles (no world assigned) |
| | | 182 | | var orphanArticles = allArticles.Where(a => !a.WorldId.HasValue && a.ParentId == null).ToList(); |
| | | 183 | | if (orphanArticles.Any()) |
| | | 184 | | { |
| | | 185 | | _logger.LogWarning("{Count} articles have no world assigned", orphanArticles.Count); |
| | | 186 | | |
| | | 187 | | var orphanNode = CreateVirtualGroupNode( |
| | | 188 | | VirtualGroupType.Uncategorized, |
| | | 189 | | "Unassigned Articles", |
| | | 190 | | null); |
| | | 191 | | |
| | | 192 | | foreach (var article in orphanArticles) |
| | | 193 | | { |
| | | 194 | | var articleNode = CreateArticleNode(article); |
| | | 195 | | orphanNode.Children.Add(articleNode); |
| | | 196 | | nodeIndex.AddNode(articleNode); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | if (orphanNode.Children.Any()) |
| | | 200 | | { |
| | | 201 | | orphanNode.ChildCount = orphanNode.Children.Count; |
| | | 202 | | nodeIndex.AddRootNode(orphanNode); |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | stopwatch.Stop(); |
| | | 207 | | _logger.LogDebug("Tree build complete in {Ms}ms total", stopwatch.ElapsedMilliseconds); |
| | | 208 | | |
| | | 209 | | return new BuildResult |
| | | 210 | | { |
| | | 211 | | NodeIndex = nodeIndex, |
| | | 212 | | CachedArticles = allArticles |
| | | 213 | | }; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | private TreeNode BuildWorldNode( |
| | | 217 | | WorldDto world, |
| | | 218 | | WorldDetailDto worldDetail, |
| | | 219 | | List<ArticleTreeDto> allArticles, |
| | | 220 | | Dictionary<Guid, ArticleTreeDto> articleIndex, |
| | | 221 | | Dictionary<Guid, List<ArcDto>> arcsByCampaign, |
| | | 222 | | Dictionary<Guid, List<SessionTreeDto>> sessionsByArc, |
| | | 223 | | List<WorldLinkDto> worldLinks, |
| | | 224 | | List<WorldDocumentDto> worldDocuments, |
| | | 225 | | List<MapSummaryDto> worldMaps, |
| | | 226 | | TreeNodeIndex nodeIndex) |
| | | 227 | | { |
| | 26 | 228 | | var worldNode = new TreeNode |
| | 26 | 229 | | { |
| | 26 | 230 | | Id = world.Id, |
| | 26 | 231 | | NodeType = TreeNodeType.World, |
| | 26 | 232 | | Title = world.Name, |
| | 26 | 233 | | WorldId = world.Id, |
| | 26 | 234 | | IconEmoji = "fa-solid fa-globe" |
| | 26 | 235 | | }; |
| | | 236 | | |
| | 26 | 237 | | var campaigns = worldDetail.Campaigns ?? new List<CampaignDto>(); |
| | 26 | 238 | | var worldArticles = allArticles.Where(a => a.WorldId == world.Id).ToList(); |
| | | 239 | | |
| | | 240 | | // Create virtual groups |
| | 26 | 241 | | var campaignsGroup = CreateVirtualGroupNode(VirtualGroupType.Campaigns, "Campaigns", world.Id); |
| | 26 | 242 | | var charactersGroup = CreateVirtualGroupNode(VirtualGroupType.PlayerCharacters, "Player Characters", world.Id); |
| | 26 | 243 | | var wikiGroup = CreateVirtualGroupNode(VirtualGroupType.Wiki, "Wiki", world.Id); |
| | 26 | 244 | | var uncategorizedGroup = CreateVirtualGroupNode(VirtualGroupType.Uncategorized, "Uncategorized", world.Id); |
| | | 245 | | |
| | | 246 | | // Build Campaigns group |
| | 100 | 247 | | foreach (var campaign in campaigns.OrderBy(c => c.Name)) |
| | | 248 | | { |
| | 24 | 249 | | var arcs = arcsByCampaign[campaign.Id]; |
| | | 250 | | |
| | 24 | 251 | | var campaignNode = BuildCampaignNode(campaign, arcs, worldArticles, articleIndex, sessionsByArc, nodeIndex); |
| | 24 | 252 | | campaignsGroup.Children.Add(campaignNode); |
| | 24 | 253 | | nodeIndex.AddNode(campaignNode); |
| | | 254 | | } |
| | 26 | 255 | | campaignsGroup.ChildCount = campaignsGroup.Children.Count; |
| | | 256 | | |
| | | 257 | | // Build Characters group |
| | 26 | 258 | | var characterArticles = worldArticles |
| | 26 | 259 | | .Where(a => a.Type == ArticleType.Character && a.ParentId == null).OrderBy(a => a.Title).ToList(); |
| | | 260 | | |
| | 56 | 261 | | foreach (var article in characterArticles) |
| | | 262 | | { |
| | 2 | 263 | | var articleNode = BuildArticleNodeWithChildren(article, worldArticles, articleIndex, nodeIndex); |
| | 2 | 264 | | charactersGroup.Children.Add(articleNode); |
| | | 265 | | } |
| | 26 | 266 | | charactersGroup.ChildCount = charactersGroup.Children.Count; |
| | | 267 | | |
| | | 268 | | // Build Wiki group |
| | 26 | 269 | | var wikiArticles = worldArticles |
| | 26 | 270 | | .Where(a => a.Type == ArticleType.WikiArticle && a.ParentId == null) |
| | 26 | 271 | | .OrderBy(a => a.Title) |
| | 26 | 272 | | .ToList(); |
| | | 273 | | |
| | 100 | 274 | | foreach (var article in wikiArticles) |
| | | 275 | | { |
| | 24 | 276 | | var articleNode = BuildArticleNodeWithChildren(article, worldArticles, articleIndex, nodeIndex); |
| | 24 | 277 | | wikiGroup.Children.Add(articleNode); |
| | | 278 | | } |
| | 26 | 279 | | wikiGroup.ChildCount = wikiGroup.Children.Count; |
| | | 280 | | |
| | | 281 | | // Build Links group |
| | 26 | 282 | | var linksGroup = CreateVirtualGroupNode(VirtualGroupType.Links, "External Resources", world.Id); |
| | 54 | 283 | | foreach (var link in worldLinks.OrderBy(l => l.Title)) |
| | | 284 | | { |
| | 1 | 285 | | var linkNode = new TreeNode |
| | 1 | 286 | | { |
| | 1 | 287 | | Id = link.Id, |
| | 1 | 288 | | NodeType = TreeNodeType.ExternalLink, |
| | 1 | 289 | | Title = link.Title, |
| | 1 | 290 | | Url = link.Url, |
| | 1 | 291 | | WorldId = world.Id, |
| | 1 | 292 | | IconEmoji = "fa-solid fa-external-link-alt" |
| | 1 | 293 | | }; |
| | 1 | 294 | | linksGroup.Children.Add(linkNode); |
| | 1 | 295 | | nodeIndex.AddNode(linkNode); |
| | | 296 | | } |
| | | 297 | | |
| | 54 | 298 | | foreach (var document in worldDocuments.Where(d => d.ArticleId == null).OrderBy(d => d.Title)) |
| | | 299 | | { |
| | 1 | 300 | | var documentNode = new TreeNode |
| | 1 | 301 | | { |
| | 1 | 302 | | Id = document.Id, |
| | 1 | 303 | | NodeType = TreeNodeType.ExternalLink, |
| | 1 | 304 | | Title = document.Title, |
| | 1 | 305 | | Url = null, |
| | 1 | 306 | | WorldId = world.Id, |
| | 1 | 307 | | IconEmoji = GetDocumentIcon(document.ContentType), |
| | 1 | 308 | | AdditionalData = new Dictionary<string, object> |
| | 1 | 309 | | { |
| | 1 | 310 | | { "IsDocument", true }, |
| | 1 | 311 | | { "ContentType", document.ContentType }, |
| | 1 | 312 | | { "FileSizeBytes", document.FileSizeBytes }, |
| | 1 | 313 | | { "FileName", document.FileName } |
| | 1 | 314 | | } |
| | 1 | 315 | | }; |
| | 1 | 316 | | linksGroup.Children.Add(documentNode); |
| | 1 | 317 | | nodeIndex.AddNode(documentNode); |
| | | 318 | | } |
| | 26 | 319 | | linksGroup.ChildCount = linksGroup.Children.Count; |
| | | 320 | | |
| | | 321 | | // Build Maps group |
| | 26 | 322 | | var mapsGroup = CreateVirtualGroupNode(VirtualGroupType.Maps, "Maps", world.Id); |
| | 56 | 323 | | foreach (var map in worldMaps.OrderBy(m => m.Name)) |
| | | 324 | | { |
| | 2 | 325 | | var mapNode = new TreeNode |
| | 2 | 326 | | { |
| | 2 | 327 | | Id = map.WorldMapId, |
| | 2 | 328 | | NodeType = TreeNodeType.Map, |
| | 2 | 329 | | Title = map.Name, |
| | 2 | 330 | | WorldId = world.Id |
| | 2 | 331 | | }; |
| | 2 | 332 | | mapsGroup.Children.Add(mapNode); |
| | 2 | 333 | | nodeIndex.AddNode(mapNode); |
| | | 334 | | } |
| | 26 | 335 | | mapsGroup.ChildCount = mapsGroup.Children.Count; |
| | | 336 | | |
| | | 337 | | // Build Uncategorized group |
| | 26 | 338 | | var uncategorizedArticles = worldArticles |
| | 26 | 339 | | .Where(a => a.ParentId == null && |
| | 26 | 340 | | a.Type != ArticleType.WikiArticle && |
| | 26 | 341 | | a.Type != ArticleType.Character && |
| | 26 | 342 | | a.Type != ArticleType.Session && |
| | 26 | 343 | | a.Type != ArticleType.SessionNote && |
| | 26 | 344 | | a.Type != ArticleType.CharacterNote).OrderBy(a => a.Title).ToList(); |
| | | 345 | | |
| | 56 | 346 | | foreach (var article in uncategorizedArticles) |
| | | 347 | | { |
| | 2 | 348 | | var articleNode = BuildArticleNodeWithChildren(article, worldArticles, articleIndex, nodeIndex); |
| | 2 | 349 | | uncategorizedGroup.Children.Add(articleNode); |
| | | 350 | | } |
| | 26 | 351 | | uncategorizedGroup.ChildCount = uncategorizedGroup.Children.Count; |
| | | 352 | | |
| | | 353 | | // Add groups to world node |
| | 26 | 354 | | worldNode.Children.Add(campaignsGroup); |
| | 26 | 355 | | nodeIndex.AddNode(campaignsGroup); |
| | | 356 | | |
| | 26 | 357 | | worldNode.Children.Add(charactersGroup); |
| | 26 | 358 | | nodeIndex.AddNode(charactersGroup); |
| | | 359 | | |
| | 26 | 360 | | worldNode.Children.Add(wikiGroup); |
| | 26 | 361 | | nodeIndex.AddNode(wikiGroup); |
| | | 362 | | |
| | 26 | 363 | | worldNode.Children.Add(mapsGroup); |
| | 26 | 364 | | nodeIndex.AddNode(mapsGroup); |
| | | 365 | | |
| | 26 | 366 | | if (linksGroup.Children.Any()) |
| | | 367 | | { |
| | 1 | 368 | | worldNode.Children.Add(linksGroup); |
| | 1 | 369 | | nodeIndex.AddNode(linksGroup); |
| | | 370 | | } |
| | | 371 | | |
| | 26 | 372 | | if (uncategorizedGroup.Children.Any()) |
| | | 373 | | { |
| | 2 | 374 | | worldNode.Children.Add(uncategorizedGroup); |
| | 2 | 375 | | nodeIndex.AddNode(uncategorizedGroup); |
| | | 376 | | } |
| | | 377 | | |
| | 26 | 378 | | worldNode.ChildCount = worldNode.Children.Count; |
| | | 379 | | |
| | 26 | 380 | | return worldNode; |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | private TreeNode BuildCampaignNode( |
| | | 384 | | CampaignDto campaign, |
| | | 385 | | List<ArcDto> arcs, |
| | | 386 | | List<ArticleTreeDto> worldArticles, |
| | | 387 | | Dictionary<Guid, ArticleTreeDto> articleIndex, |
| | | 388 | | Dictionary<Guid, List<SessionTreeDto>> sessionsByArc, |
| | | 389 | | TreeNodeIndex nodeIndex) |
| | | 390 | | { |
| | 24 | 391 | | var campaignNode = new TreeNode |
| | 24 | 392 | | { |
| | 24 | 393 | | Id = campaign.Id, |
| | 24 | 394 | | NodeType = TreeNodeType.Campaign, |
| | 24 | 395 | | Title = campaign.Name, |
| | 24 | 396 | | WorldId = campaign.WorldId, |
| | 24 | 397 | | CampaignId = campaign.Id, |
| | 24 | 398 | | IconEmoji = "fa-solid fa-dungeon" |
| | 24 | 399 | | }; |
| | | 400 | | |
| | 96 | 401 | | foreach (var arc in arcs.OrderBy(a => a.SortOrder).ThenBy(a => a.Name)) |
| | | 402 | | { |
| | 24 | 403 | | var arcNode = BuildArcNode(arc, worldArticles, articleIndex, sessionsByArc, nodeIndex); |
| | 24 | 404 | | campaignNode.Children.Add(arcNode); |
| | 24 | 405 | | nodeIndex.AddNode(arcNode); |
| | | 406 | | } |
| | | 407 | | |
| | 24 | 408 | | campaignNode.ChildCount = campaignNode.Children.Count; |
| | | 409 | | |
| | 24 | 410 | | return campaignNode; |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | private TreeNode BuildArcNode( |
| | | 414 | | ArcDto arc, |
| | | 415 | | List<ArticleTreeDto> worldArticles, |
| | | 416 | | Dictionary<Guid, ArticleTreeDto> articleIndex, |
| | | 417 | | Dictionary<Guid, List<SessionTreeDto>> sessionsByArc, |
| | | 418 | | TreeNodeIndex nodeIndex) |
| | | 419 | | { |
| | 26 | 420 | | var arcNode = new TreeNode |
| | 26 | 421 | | { |
| | 26 | 422 | | Id = arc.Id, |
| | 26 | 423 | | NodeType = TreeNodeType.Arc, |
| | 26 | 424 | | Title = arc.Name, |
| | 26 | 425 | | CampaignId = arc.CampaignId, |
| | 26 | 426 | | ArcId = arc.Id, |
| | 26 | 427 | | IconEmoji = "fa-solid fa-book-open" |
| | 26 | 428 | | }; |
| | | 429 | | |
| | 26 | 430 | | var sessions = sessionsByArc.TryGetValue(arc.Id, out var sessionList) |
| | 26 | 431 | | ? sessionList |
| | 26 | 432 | | : new List<SessionTreeDto>(); |
| | | 433 | | |
| | 26 | 434 | | if (sessions.Count > 0) |
| | | 435 | | { |
| | 20 | 436 | | foreach (var session in sessions |
| | 4 | 437 | | .OrderBy(s => s.Name, StringComparer.OrdinalIgnoreCase) |
| | 4 | 438 | | .ThenBy(s => s.Id)) |
| | | 439 | | { |
| | 6 | 440 | | var sessionNode = BuildSessionNode(session, arc, worldArticles, articleIndex, nodeIndex); |
| | 6 | 441 | | arcNode.Children.Add(sessionNode); |
| | | 442 | | } |
| | | 443 | | } |
| | | 444 | | |
| | 26 | 445 | | arcNode.ChildCount = arcNode.Children.Count; |
| | | 446 | | |
| | 26 | 447 | | return arcNode; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private TreeNode BuildSessionNode( |
| | | 451 | | SessionTreeDto session, |
| | | 452 | | ArcDto arc, |
| | | 453 | | List<ArticleTreeDto> worldArticles, |
| | | 454 | | Dictionary<Guid, ArticleTreeDto> articleIndex, |
| | | 455 | | TreeNodeIndex nodeIndex) |
| | | 456 | | { |
| | 6 | 457 | | var sessionNode = new TreeNode |
| | 6 | 458 | | { |
| | 6 | 459 | | Id = session.Id, |
| | 6 | 460 | | NodeType = TreeNodeType.Session, |
| | 6 | 461 | | Title = session.Name, |
| | 6 | 462 | | CampaignId = arc.CampaignId, |
| | 6 | 463 | | ArcId = arc.Id, |
| | 6 | 464 | | IconEmoji = "fa-solid fa-calendar-day", |
| | 6 | 465 | | HasAISummary = session.HasAiSummary |
| | 6 | 466 | | }; |
| | | 467 | | |
| | 6 | 468 | | nodeIndex.AddNode(sessionNode); |
| | | 469 | | |
| | 6 | 470 | | var sessionNotes = worldArticles |
| | 6 | 471 | | .Where(a => a.Type == ArticleType.SessionNote && a.SessionId == session.Id) |
| | 6 | 472 | | .OrderBy(a => a.Title) |
| | 6 | 473 | | .ToList(); |
| | | 474 | | |
| | 6 | 475 | | if (sessionNotes.Count == 0) |
| | | 476 | | { |
| | 4 | 477 | | sessionNode.ChildCount = 0; |
| | 4 | 478 | | return sessionNode; |
| | | 479 | | } |
| | | 480 | | |
| | 2 | 481 | | var sessionNoteIds = sessionNotes.Select(n => n.Id).ToHashSet(); |
| | 2 | 482 | | var rootSessionNotes = sessionNotes |
| | 2 | 483 | | .Where(n => !n.ParentId.HasValue || !sessionNoteIds.Contains(n.ParentId.Value)) |
| | 2 | 484 | | .ToList(); |
| | | 485 | | |
| | 8 | 486 | | foreach (var note in rootSessionNotes) |
| | | 487 | | { |
| | 2 | 488 | | var noteNode = BuildArticleNodeWithChildren(note, sessionNotes, articleIndex, nodeIndex); |
| | 2 | 489 | | noteNode.ParentId = sessionNode.Id; |
| | 2 | 490 | | sessionNode.Children.Add(noteNode); |
| | | 491 | | } |
| | | 492 | | |
| | 2 | 493 | | sessionNode.ChildCount = sessionNode.Children.Count; |
| | 2 | 494 | | return sessionNode; |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | private TreeNode BuildArticleNodeWithChildren( |
| | | 498 | | ArticleTreeDto article, |
| | | 499 | | List<ArticleTreeDto> allArticles, |
| | | 500 | | Dictionary<Guid, ArticleTreeDto> articleIndex, |
| | | 501 | | TreeNodeIndex nodeIndex) |
| | | 502 | | { |
| | 32 | 503 | | var node = CreateArticleNode(article); |
| | 32 | 504 | | nodeIndex.AddNode(node); |
| | | 505 | | |
| | 32 | 506 | | var children = allArticles |
| | 32 | 507 | | .Where(a => a.ParentId == article.Id) |
| | 32 | 508 | | .OrderBy(a => a.Title) |
| | 32 | 509 | | .ToList(); |
| | | 510 | | |
| | 68 | 511 | | foreach (var child in children) |
| | | 512 | | { |
| | 2 | 513 | | var childNode = BuildArticleNodeWithChildren(child, allArticles, articleIndex, nodeIndex); |
| | 2 | 514 | | childNode.ParentId = node.Id; |
| | 2 | 515 | | node.Children.Add(childNode); |
| | | 516 | | } |
| | | 517 | | |
| | 32 | 518 | | node.ChildCount = node.Children.Count; |
| | | 519 | | |
| | 32 | 520 | | return node; |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | /// <summary> |
| | | 524 | | /// Creates a TreeNode from an ArticleTreeDto. |
| | | 525 | | /// </summary> |
| | | 526 | | public static TreeNode CreateArticleNode(ArticleTreeDto article) |
| | | 527 | | { |
| | 34 | 528 | | return new TreeNode |
| | 34 | 529 | | { |
| | 34 | 530 | | Id = article.Id, |
| | 34 | 531 | | NodeType = TreeNodeType.Article, |
| | 34 | 532 | | ArticleType = article.Type, |
| | 34 | 533 | | Title = article.Title, |
| | 34 | 534 | | Slug = article.Slug, |
| | 34 | 535 | | IconEmoji = article.IconEmoji, |
| | 34 | 536 | | ParentId = article.ParentId, |
| | 34 | 537 | | WorldId = article.WorldId, |
| | 34 | 538 | | CampaignId = article.CampaignId, |
| | 34 | 539 | | ArcId = article.ArcId, |
| | 34 | 540 | | ChildCount = article.ChildCount, |
| | 34 | 541 | | Visibility = article.Visibility, |
| | 34 | 542 | | HasAISummary = article.HasAISummary |
| | 34 | 543 | | }; |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | /// <summary> |
| | | 547 | | /// Creates a virtual group node. |
| | | 548 | | /// </summary> |
| | | 549 | | public static TreeNode CreateVirtualGroupNode(VirtualGroupType groupType, string title, Guid? worldId) |
| | | 550 | | { |
| | 158 | 551 | | return new TreeNode |
| | 158 | 552 | | { |
| | 158 | 553 | | Id = Guid.NewGuid(), |
| | 158 | 554 | | NodeType = TreeNodeType.VirtualGroup, |
| | 158 | 555 | | VirtualGroupType = groupType, |
| | 158 | 556 | | Title = title, |
| | 158 | 557 | | WorldId = worldId |
| | 158 | 558 | | }; |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | /// <summary> |
| | | 562 | | /// Gets the appropriate icon for a document based on its content type. |
| | | 563 | | /// </summary> |
| | | 564 | | public static string GetDocumentIcon(string contentType) |
| | | 565 | | { |
| | 9 | 566 | | return contentType.ToLowerInvariant() switch |
| | 9 | 567 | | { |
| | 2 | 568 | | "application/pdf" => "fa-solid fa-file-pdf", |
| | 1 | 569 | | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "fa-solid fa-file-word", |
| | 1 | 570 | | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "fa-solid fa-file-excel", |
| | 1 | 571 | | "application/vnd.openxmlformats-officedocument.presentationml.presentation" => "fa-solid fa-file-powerpoint" |
| | 1 | 572 | | "text/plain" => "fa-solid fa-file-lines", |
| | 1 | 573 | | "text/markdown" => "fa-solid fa-file-lines", |
| | 3 | 574 | | string ct when ct.StartsWith("image/") => "fa-solid fa-file-image", |
| | 1 | 575 | | _ => "fa-solid fa-file" |
| | 9 | 576 | | }; |
| | | 577 | | } |
| | | 578 | | } |