| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Service for anonymous public access to worlds. |
| | | 11 | | /// All methods return only publicly visible content - no authentication required. |
| | | 12 | | /// </summary> |
| | | 13 | | public class PublicWorldService : IPublicWorldService |
| | | 14 | | { |
| | | 15 | | private readonly ChronicisDbContext _context; |
| | | 16 | | private readonly ILogger<PublicWorldService> _logger; |
| | | 17 | | private readonly IArticleHierarchyService _hierarchyService; |
| | | 18 | | |
| | 0 | 19 | | public PublicWorldService(ChronicisDbContext context, ILogger<PublicWorldService> logger, IArticleHierarchyService h |
| | | 20 | | { |
| | 0 | 21 | | _context = context; |
| | 0 | 22 | | _logger = logger; |
| | 0 | 23 | | _hierarchyService = hierarchyService; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Get a public world by its public slug. |
| | | 28 | | /// Returns null if world doesn't exist or is not public. |
| | | 29 | | /// </summary> |
| | | 30 | | public async Task<WorldDetailDto?> GetPublicWorldAsync(string publicSlug) |
| | | 31 | | { |
| | 0 | 32 | | var normalizedSlug = publicSlug.Trim().ToLowerInvariant(); |
| | | 33 | | |
| | 0 | 34 | | var world = await _context.Worlds |
| | 0 | 35 | | .AsNoTracking() |
| | 0 | 36 | | .Include(w => w.Owner) |
| | 0 | 37 | | .Include(w => w.Campaigns) |
| | 0 | 38 | | .Where(w => w.PublicSlug == normalizedSlug && w.IsPublic) |
| | 0 | 39 | | .FirstOrDefaultAsync(); |
| | | 40 | | |
| | 0 | 41 | | if (world == null) |
| | | 42 | | { |
| | 0 | 43 | | _logger.LogDebugSanitized("Public world not found for slug '{PublicSlug}'", normalizedSlug); |
| | 0 | 44 | | return null; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | _logger.LogDebugSanitized("Public world '{WorldName}' accessed via slug '{PublicSlug}'", |
| | 0 | 48 | | world.Name, normalizedSlug); |
| | | 49 | | |
| | 0 | 50 | | return new WorldDetailDto |
| | 0 | 51 | | { |
| | 0 | 52 | | Id = world.Id, |
| | 0 | 53 | | Name = world.Name, |
| | 0 | 54 | | Slug = world.Slug, |
| | 0 | 55 | | Description = world.Description, |
| | 0 | 56 | | OwnerId = world.OwnerId, |
| | 0 | 57 | | OwnerName = world.Owner?.DisplayName ?? "Unknown", |
| | 0 | 58 | | CreatedAt = world.CreatedAt, |
| | 0 | 59 | | CampaignCount = world.Campaigns?.Count ?? 0, |
| | 0 | 60 | | IsPublic = world.IsPublic, |
| | 0 | 61 | | PublicSlug = world.PublicSlug, |
| | 0 | 62 | | // Include public campaigns |
| | 0 | 63 | | Campaigns = world.Campaigns?.Select(c => new CampaignDto |
| | 0 | 64 | | { |
| | 0 | 65 | | Id = c.Id, |
| | 0 | 66 | | Name = c.Name, |
| | 0 | 67 | | Description = c.Description, |
| | 0 | 68 | | WorldId = c.WorldId, |
| | 0 | 69 | | IsActive = c.IsActive, |
| | 0 | 70 | | StartedAt = c.StartedAt |
| | 0 | 71 | | }).ToList() ?? new List<CampaignDto>() |
| | 0 | 72 | | }; |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Get the article tree for a public world. |
| | | 77 | | /// Only returns articles with Public visibility. |
| | | 78 | | /// Returns a hierarchical tree structure organized by virtual groups (Campaigns, Characters, Wiki). |
| | | 79 | | /// </summary> |
| | | 80 | | public async Task<List<ArticleTreeDto>> GetPublicArticleTreeAsync(string publicSlug) |
| | | 81 | | { |
| | 0 | 82 | | var normalizedSlug = publicSlug.Trim().ToLowerInvariant(); |
| | | 83 | | |
| | | 84 | | // First, verify the world exists and is public |
| | 0 | 85 | | var world = await _context.Worlds |
| | 0 | 86 | | .AsNoTracking() |
| | 0 | 87 | | .Include(w => w.Campaigns) |
| | 0 | 88 | | .ThenInclude(c => c.Arcs) |
| | 0 | 89 | | .Where(w => w.PublicSlug == normalizedSlug && w.IsPublic) |
| | 0 | 90 | | .FirstOrDefaultAsync(); |
| | | 91 | | |
| | 0 | 92 | | if (world == null) |
| | | 93 | | { |
| | 0 | 94 | | _logger.LogDebugSanitized("Public world not found for slug '{PublicSlug}'", normalizedSlug); |
| | 0 | 95 | | return new List<ArticleTreeDto>(); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // Get all public articles for this world |
| | 0 | 99 | | var allPublicArticles = await _context.Articles |
| | 0 | 100 | | .AsNoTracking() |
| | 0 | 101 | | .Where(a => a.WorldId == world.Id && a.Visibility == ArticleVisibility.Public) |
| | 0 | 102 | | .Select(a => new ArticleTreeDto |
| | 0 | 103 | | { |
| | 0 | 104 | | Id = a.Id, |
| | 0 | 105 | | Title = a.Title, |
| | 0 | 106 | | Slug = a.Slug, |
| | 0 | 107 | | ParentId = a.ParentId, |
| | 0 | 108 | | WorldId = a.WorldId, |
| | 0 | 109 | | CampaignId = a.CampaignId, |
| | 0 | 110 | | ArcId = a.ArcId, |
| | 0 | 111 | | Type = a.Type, |
| | 0 | 112 | | Visibility = a.Visibility, |
| | 0 | 113 | | HasChildren = false, // Will calculate below |
| | 0 | 114 | | ChildCount = 0, // Will calculate below |
| | 0 | 115 | | Children = new List<ArticleTreeDto>(), |
| | 0 | 116 | | CreatedAt = a.CreatedAt, |
| | 0 | 117 | | EffectiveDate = a.EffectiveDate, |
| | 0 | 118 | | IconEmoji = a.IconEmoji, |
| | 0 | 119 | | CreatedBy = a.CreatedBy |
| | 0 | 120 | | }) |
| | 0 | 121 | | .ToListAsync(); |
| | | 122 | | |
| | | 123 | | // Build article index and children relationships |
| | 0 | 124 | | var articleIndex = allPublicArticles.ToDictionary(a => a.Id); |
| | 0 | 125 | | var publicArticleIds = new HashSet<Guid>(allPublicArticles.Select(a => a.Id)); |
| | | 126 | | |
| | | 127 | | // Link children to parents |
| | 0 | 128 | | foreach (var article in allPublicArticles) |
| | | 129 | | { |
| | 0 | 130 | | if (article.ParentId.HasValue && articleIndex.TryGetValue(article.ParentId.Value, out var parent)) |
| | | 131 | | { |
| | 0 | 132 | | parent.Children ??= new List<ArticleTreeDto>(); |
| | 0 | 133 | | parent.Children.Add(article); |
| | 0 | 134 | | parent.HasChildren = true; |
| | 0 | 135 | | parent.ChildCount++; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // Sort children by title |
| | 0 | 140 | | foreach (var article in allPublicArticles.Where(a => a.Children?.Any() == true)) |
| | | 141 | | { |
| | 0 | 142 | | article.Children = article.Children!.OrderBy(c => c.Title).ToList(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | // Build virtual groups |
| | 0 | 146 | | var result = new List<ArticleTreeDto>(); |
| | | 147 | | |
| | | 148 | | // 1. Campaigns group - contains campaigns with their arcs and sessions |
| | 0 | 149 | | var campaignsGroup = CreateVirtualGroup("campaigns", "Campaigns", "fa-solid fa-dungeon"); |
| | 0 | 150 | | foreach (var campaign in world.Campaigns?.OrderBy(c => c.Name) ?? Enumerable.Empty<Chronicis.Shared.Models.Campa |
| | | 151 | | { |
| | 0 | 152 | | var campaignNode = new ArticleTreeDto |
| | 0 | 153 | | { |
| | 0 | 154 | | Id = campaign.Id, |
| | 0 | 155 | | Title = campaign.Name, |
| | 0 | 156 | | Slug = campaign.Name.ToLowerInvariant().Replace(" ", "-"), |
| | 0 | 157 | | Type = ArticleType.WikiArticle, // Use WikiArticle as placeholder |
| | 0 | 158 | | IconEmoji = "fa-solid fa-dungeon", |
| | 0 | 159 | | Children = new List<ArticleTreeDto>(), |
| | 0 | 160 | | IsVirtualGroup = true |
| | 0 | 161 | | }; |
| | | 162 | | |
| | | 163 | | // Add arcs under campaign |
| | 0 | 164 | | foreach (var arc in campaign.Arcs?.OrderBy(a => a.SortOrder).ThenBy(a => a.Name) ?? Enumerable.Empty<Chronic |
| | | 165 | | { |
| | 0 | 166 | | var arcNode = new ArticleTreeDto |
| | 0 | 167 | | { |
| | 0 | 168 | | Id = arc.Id, |
| | 0 | 169 | | Title = arc.Name, |
| | 0 | 170 | | Slug = arc.Name.ToLowerInvariant().Replace(" ", "-"), |
| | 0 | 171 | | Type = ArticleType.WikiArticle, |
| | 0 | 172 | | IconEmoji = "fa-solid fa-book-open", |
| | 0 | 173 | | Children = new List<ArticleTreeDto>(), |
| | 0 | 174 | | IsVirtualGroup = true |
| | 0 | 175 | | }; |
| | | 176 | | |
| | | 177 | | // Find session articles for this arc |
| | 0 | 178 | | var sessionArticles = allPublicArticles |
| | 0 | 179 | | .Where(a => a.ArcId == arc.Id && a.Type == ArticleType.Session && a.ParentId == null) |
| | 0 | 180 | | .OrderBy(a => a.Title) |
| | 0 | 181 | | .ToList(); |
| | | 182 | | |
| | 0 | 183 | | foreach (var session in sessionArticles) |
| | | 184 | | { |
| | 0 | 185 | | arcNode.Children.Add(session); |
| | 0 | 186 | | arcNode.HasChildren = true; |
| | 0 | 187 | | arcNode.ChildCount++; |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | if (arcNode.Children.Any()) |
| | | 191 | | { |
| | 0 | 192 | | campaignNode.Children.Add(arcNode); |
| | 0 | 193 | | campaignNode.HasChildren = true; |
| | 0 | 194 | | campaignNode.ChildCount++; |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | if (campaignNode.Children.Any()) |
| | | 199 | | { |
| | 0 | 200 | | campaignsGroup.Children!.Add(campaignNode); |
| | 0 | 201 | | campaignsGroup.HasChildren = true; |
| | 0 | 202 | | campaignsGroup.ChildCount++; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | if (campaignsGroup.Children!.Any()) |
| | | 207 | | { |
| | 0 | 208 | | result.Add(campaignsGroup); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | // 2. Player Characters group |
| | 0 | 212 | | var charactersGroup = CreateVirtualGroup("characters", "Player Characters", "fa-solid fa-user-ninja"); |
| | 0 | 213 | | var characterArticles = allPublicArticles |
| | 0 | 214 | | .Where(a => a.Type == ArticleType.Character && a.ParentId == null) |
| | 0 | 215 | | .OrderBy(a => a.Title) |
| | 0 | 216 | | .ToList(); |
| | | 217 | | |
| | 0 | 218 | | foreach (var article in characterArticles) |
| | | 219 | | { |
| | 0 | 220 | | charactersGroup.Children!.Add(article); |
| | 0 | 221 | | charactersGroup.HasChildren = true; |
| | 0 | 222 | | charactersGroup.ChildCount++; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | if (charactersGroup.Children!.Any()) |
| | | 226 | | { |
| | 0 | 227 | | result.Add(charactersGroup); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | // 3. Wiki group |
| | 0 | 231 | | var wikiGroup = CreateVirtualGroup("wiki", "Wiki", "fa-solid fa-book"); |
| | 0 | 232 | | var wikiArticles = allPublicArticles |
| | 0 | 233 | | .Where(a => a.Type == ArticleType.WikiArticle && a.ParentId == null) |
| | 0 | 234 | | .OrderBy(a => a.Title) |
| | 0 | 235 | | .ToList(); |
| | | 236 | | |
| | 0 | 237 | | foreach (var article in wikiArticles) |
| | | 238 | | { |
| | 0 | 239 | | wikiGroup.Children!.Add(article); |
| | 0 | 240 | | wikiGroup.HasChildren = true; |
| | 0 | 241 | | wikiGroup.ChildCount++; |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | if (wikiGroup.Children!.Any()) |
| | | 245 | | { |
| | 0 | 246 | | result.Add(wikiGroup); |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | // 4. Uncategorized (Legacy and other types without parents not already included) |
| | 0 | 250 | | var includedIds = new HashSet<Guid>(); |
| | | 251 | | |
| | | 252 | | // Collect all IDs from campaigns/arcs/sessions |
| | 0 | 253 | | foreach (var campaign in result.FirstOrDefault(r => r.Slug == "campaigns")?.Children ?? new List<ArticleTreeDto> |
| | | 254 | | { |
| | 0 | 255 | | foreach (var arc in campaign.Children ?? new List<ArticleTreeDto>()) |
| | | 256 | | { |
| | 0 | 257 | | foreach (var session in arc.Children ?? new List<ArticleTreeDto>()) |
| | | 258 | | { |
| | 0 | 259 | | CollectArticleIds(session, includedIds); |
| | | 260 | | } |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // Collect character and wiki IDs |
| | 0 | 265 | | foreach (var article in characterArticles) |
| | | 266 | | { |
| | 0 | 267 | | CollectArticleIds(article, includedIds); |
| | | 268 | | } |
| | 0 | 269 | | foreach (var article in wikiArticles) |
| | | 270 | | { |
| | 0 | 271 | | CollectArticleIds(article, includedIds); |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | var uncategorizedArticles = allPublicArticles |
| | 0 | 275 | | .Where(a => a.ParentId == null && !includedIds.Contains(a.Id)) |
| | 0 | 276 | | .OrderBy(a => a.Title) |
| | 0 | 277 | | .ToList(); |
| | | 278 | | |
| | 0 | 279 | | if (uncategorizedArticles.Any()) |
| | | 280 | | { |
| | 0 | 281 | | var uncategorizedGroup = CreateVirtualGroup("uncategorized", "Uncategorized", "fa-solid fa-folder"); |
| | 0 | 282 | | foreach (var article in uncategorizedArticles) |
| | | 283 | | { |
| | 0 | 284 | | uncategorizedGroup.Children!.Add(article); |
| | 0 | 285 | | uncategorizedGroup.HasChildren = true; |
| | 0 | 286 | | uncategorizedGroup.ChildCount++; |
| | | 287 | | } |
| | 0 | 288 | | result.Add(uncategorizedGroup); |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | _logger.LogDebugSanitized("Retrieved {Count} public articles for world '{PublicSlug}' in {GroupCount} groups", |
| | 0 | 292 | | allPublicArticles.Count, normalizedSlug, result.Count); |
| | | 293 | | |
| | 0 | 294 | | return result; |
| | 0 | 295 | | } |
| | | 296 | | |
| | | 297 | | private static ArticleTreeDto CreateVirtualGroup(string slug, string title, string icon) |
| | | 298 | | { |
| | 0 | 299 | | return new ArticleTreeDto |
| | 0 | 300 | | { |
| | 0 | 301 | | Id = Guid.NewGuid(), // Virtual ID |
| | 0 | 302 | | Title = title, |
| | 0 | 303 | | Slug = slug, |
| | 0 | 304 | | Type = ArticleType.WikiArticle, |
| | 0 | 305 | | IconEmoji = icon, |
| | 0 | 306 | | HasChildren = false, |
| | 0 | 307 | | ChildCount = 0, |
| | 0 | 308 | | Children = new List<ArticleTreeDto>(), |
| | 0 | 309 | | IsVirtualGroup = true |
| | 0 | 310 | | }; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private static void CollectArticleIds(ArticleTreeDto article, HashSet<Guid> ids) |
| | | 314 | | { |
| | 0 | 315 | | ids.Add(article.Id); |
| | 0 | 316 | | if (article.Children != null) |
| | | 317 | | { |
| | 0 | 318 | | foreach (var child in article.Children) |
| | | 319 | | { |
| | 0 | 320 | | CollectArticleIds(child, ids); |
| | | 321 | | } |
| | | 322 | | } |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | /// <summary> |
| | | 326 | | /// Get a specific article by path in a public world. |
| | | 327 | | /// Returns null if article doesn't exist, world is not public, or article is not Public visibility. |
| | | 328 | | /// Path format: "article-slug/child-slug" (does not include world slug) |
| | | 329 | | /// </summary> |
| | | 330 | | public async Task<ArticleDto?> GetPublicArticleAsync(string publicSlug, string articlePath) |
| | | 331 | | { |
| | 0 | 332 | | var normalizedSlug = publicSlug.Trim().ToLowerInvariant(); |
| | | 333 | | |
| | | 334 | | // First, verify the world exists and is public |
| | 0 | 335 | | var world = await _context.Worlds |
| | 0 | 336 | | .AsNoTracking() |
| | 0 | 337 | | .Where(w => w.PublicSlug == normalizedSlug && w.IsPublic) |
| | 0 | 338 | | .Select(w => new { w.Id, w.Name, w.Slug }) |
| | 0 | 339 | | .FirstOrDefaultAsync(); |
| | | 340 | | |
| | 0 | 341 | | if (world == null) |
| | | 342 | | { |
| | 0 | 343 | | _logger.LogDebugSanitized("Public world not found for slug '{PublicSlug}'", normalizedSlug); |
| | 0 | 344 | | return null; |
| | | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | if (string.IsNullOrWhiteSpace(articlePath)) |
| | | 348 | | { |
| | 0 | 349 | | _logger.LogDebugSanitized("Empty article path for public world '{PublicSlug}'", normalizedSlug); |
| | 0 | 350 | | return null; |
| | | 351 | | } |
| | | 352 | | |
| | 0 | 353 | | var slugs = articlePath.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 354 | | if (slugs.Length == 0) |
| | 0 | 355 | | return null; |
| | | 356 | | |
| | | 357 | | // Walk down the tree using slugs |
| | 0 | 358 | | Guid? currentParentId = null; |
| | 0 | 359 | | Guid? articleId = null; |
| | | 360 | | |
| | 0 | 361 | | for (int i = 0; i < slugs.Length; i++) |
| | | 362 | | { |
| | 0 | 363 | | var slug = slugs[i]; |
| | 0 | 364 | | var isRootLevel = (i == 0); |
| | | 365 | | |
| | | 366 | | Guid? foundArticleId; |
| | | 367 | | |
| | 0 | 368 | | if (isRootLevel) |
| | | 369 | | { |
| | | 370 | | // Root-level article: filter by WorldId and ParentId = null |
| | 0 | 371 | | foundArticleId = await _context.Articles |
| | 0 | 372 | | .AsNoTracking() |
| | 0 | 373 | | .Where(a => a.Slug == slug && |
| | 0 | 374 | | a.ParentId == null && |
| | 0 | 375 | | a.WorldId == world.Id && |
| | 0 | 376 | | a.Visibility == ArticleVisibility.Public) |
| | 0 | 377 | | .Select(a => (Guid?)a.Id) |
| | 0 | 378 | | .FirstOrDefaultAsync(); |
| | | 379 | | } |
| | | 380 | | else |
| | | 381 | | { |
| | | 382 | | // Child article: filter by ParentId |
| | 0 | 383 | | foundArticleId = await _context.Articles |
| | 0 | 384 | | .AsNoTracking() |
| | 0 | 385 | | .Where(a => a.Slug == slug && |
| | 0 | 386 | | a.ParentId == currentParentId && |
| | 0 | 387 | | a.Visibility == ArticleVisibility.Public) |
| | 0 | 388 | | .Select(a => (Guid?)a.Id) |
| | 0 | 389 | | .FirstOrDefaultAsync(); |
| | | 390 | | } |
| | | 391 | | |
| | 0 | 392 | | if (!foundArticleId.HasValue) |
| | | 393 | | { |
| | 0 | 394 | | _logger.LogDebugSanitized("Public article not found for slug '{Slug}' in path '{Path}' for world '{Publi |
| | 0 | 395 | | slug, articlePath, normalizedSlug); |
| | 0 | 396 | | return null; |
| | | 397 | | } |
| | | 398 | | |
| | 0 | 399 | | articleId = foundArticleId.Value; |
| | 0 | 400 | | currentParentId = foundArticleId.Value; |
| | 0 | 401 | | } |
| | | 402 | | |
| | | 403 | | // Found the article, now get full details |
| | 0 | 404 | | if (!articleId.HasValue) |
| | 0 | 405 | | return null; |
| | | 406 | | |
| | 0 | 407 | | var article = await _context.Articles |
| | 0 | 408 | | .AsNoTracking() |
| | 0 | 409 | | .Where(a => a.Id == articleId.Value && a.Visibility == ArticleVisibility.Public) |
| | 0 | 410 | | .Select(a => new ArticleDto |
| | 0 | 411 | | { |
| | 0 | 412 | | Id = a.Id, |
| | 0 | 413 | | Title = a.Title, |
| | 0 | 414 | | Slug = a.Slug, |
| | 0 | 415 | | ParentId = a.ParentId, |
| | 0 | 416 | | WorldId = a.WorldId, |
| | 0 | 417 | | CampaignId = a.CampaignId, |
| | 0 | 418 | | ArcId = a.ArcId, |
| | 0 | 419 | | Body = a.Body ?? string.Empty, |
| | 0 | 420 | | Type = a.Type, |
| | 0 | 421 | | Visibility = a.Visibility, |
| | 0 | 422 | | CreatedAt = a.CreatedAt, |
| | 0 | 423 | | ModifiedAt = a.ModifiedAt, |
| | 0 | 424 | | EffectiveDate = a.EffectiveDate, |
| | 0 | 425 | | CreatedBy = a.CreatedBy, |
| | 0 | 426 | | LastModifiedBy = a.LastModifiedBy, |
| | 0 | 427 | | IconEmoji = a.IconEmoji, |
| | 0 | 428 | | SessionDate = a.SessionDate, |
| | 0 | 429 | | InGameDate = a.InGameDate, |
| | 0 | 430 | | PlayerId = a.PlayerId, |
| | 0 | 431 | | AISummary = a.AISummary, |
| | 0 | 432 | | AISummaryGeneratedAt = a.AISummaryGeneratedAt, |
| | 0 | 433 | | Breadcrumbs = new List<BreadcrumbDto>() |
| | 0 | 434 | | }) |
| | 0 | 435 | | .FirstOrDefaultAsync(); |
| | | 436 | | |
| | 0 | 437 | | if (article == null) |
| | 0 | 438 | | return null; |
| | | 439 | | |
| | | 440 | | // Build breadcrumbs (only including public articles) using centralised hierarchy service |
| | 0 | 441 | | article.Breadcrumbs = await _hierarchyService.BuildBreadcrumbsAsync(articleId.Value, new HierarchyWalkOptions |
| | 0 | 442 | | { |
| | 0 | 443 | | PublicOnly = true, |
| | 0 | 444 | | IncludeWorldBreadcrumb = true, |
| | 0 | 445 | | IncludeVirtualGroups = true, |
| | 0 | 446 | | World = new WorldContext { Id = world.Id, Name = world.Name, Slug = world.Slug } |
| | 0 | 447 | | }); |
| | | 448 | | |
| | 0 | 449 | | _logger.LogDebugSanitized("Public article '{Title}' accessed in world '{PublicSlug}'", |
| | 0 | 450 | | article.Title, normalizedSlug); |
| | | 451 | | |
| | 0 | 452 | | return article; |
| | 0 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Resolve an article ID to its public URL path. |
| | | 457 | | /// Returns null if the article doesn't exist, is not public, or doesn't belong to the specified world. |
| | | 458 | | /// </summary> |
| | | 459 | | public async Task<string?> GetPublicArticlePathAsync(string publicSlug, Guid articleId) |
| | | 460 | | { |
| | 0 | 461 | | var normalizedSlug = publicSlug.Trim().ToLowerInvariant(); |
| | | 462 | | |
| | | 463 | | // Verify the world exists and is public |
| | 0 | 464 | | var world = await _context.Worlds |
| | 0 | 465 | | .AsNoTracking() |
| | 0 | 466 | | .Where(w => w.PublicSlug == normalizedSlug && w.IsPublic) |
| | 0 | 467 | | .Select(w => new { w.Id }) |
| | 0 | 468 | | .FirstOrDefaultAsync(); |
| | | 469 | | |
| | 0 | 470 | | if (world == null) |
| | | 471 | | { |
| | 0 | 472 | | _logger.LogDebugSanitized("Public world not found for slug '{PublicSlug}'", normalizedSlug); |
| | 0 | 473 | | return null; |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | // Get the article and verify it's public and belongs to this world |
| | 0 | 477 | | var article = await _context.Articles |
| | 0 | 478 | | .AsNoTracking() |
| | 0 | 479 | | .Where(a => a.Id == articleId && |
| | 0 | 480 | | a.WorldId == world.Id && |
| | 0 | 481 | | a.Visibility == ArticleVisibility.Public) |
| | 0 | 482 | | .Select(a => new { a.Id, a.Slug, a.ParentId }) |
| | 0 | 483 | | .FirstOrDefaultAsync(); |
| | | 484 | | |
| | 0 | 485 | | if (article == null) |
| | | 486 | | { |
| | 0 | 487 | | _logger.LogDebugSanitized("Public article {ArticleId} not found in world '{PublicSlug}'", articleId, normali |
| | 0 | 488 | | return null; |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | // Build the path by walking up the parent tree |
| | 0 | 492 | | var slugs = new List<string> { article.Slug }; |
| | 0 | 493 | | var currentParentId = article.ParentId; |
| | | 494 | | |
| | 0 | 495 | | while (currentParentId.HasValue) |
| | | 496 | | { |
| | 0 | 497 | | var parentArticle = await _context.Articles |
| | 0 | 498 | | .AsNoTracking() |
| | 0 | 499 | | .Where(a => a.Id == currentParentId.Value && a.Visibility == ArticleVisibility.Public) |
| | 0 | 500 | | .Select(a => new { a.Slug, a.ParentId }) |
| | 0 | 501 | | .FirstOrDefaultAsync(); |
| | | 502 | | |
| | 0 | 503 | | if (parentArticle == null) |
| | | 504 | | { |
| | | 505 | | // Parent is not public - this article's path is broken |
| | 0 | 506 | | _logger.LogDebug("Parent article not public in chain for article {ArticleId}", articleId); |
| | 0 | 507 | | return null; |
| | | 508 | | } |
| | | 509 | | |
| | 0 | 510 | | slugs.Insert(0, parentArticle.Slug); |
| | 0 | 511 | | currentParentId = parentArticle.ParentId; |
| | | 512 | | } |
| | | 513 | | |
| | 0 | 514 | | var path = string.Join("/", slugs); |
| | 0 | 515 | | _logger.LogDebugSanitized("Resolved article {ArticleId} to path '{Path}' in world '{PublicSlug}'", |
| | 0 | 516 | | articleId, path, normalizedSlug); |
| | | 517 | | |
| | 0 | 518 | | return path; |
| | 0 | 519 | | } |
| | | 520 | | } |