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