| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using Chronicis.Shared.Models; |
| | | 6 | | using Chronicis.Shared.Utilities; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | |
| | | 9 | | namespace Chronicis.Api.Services |
| | | 10 | | { |
| | | 11 | | public class ArticleService : IArticleService |
| | | 12 | | { |
| | | 13 | | private readonly ChronicisDbContext _context; |
| | | 14 | | private readonly ILogger<ArticleService> _logger; |
| | | 15 | | private readonly IArticleHierarchyService _hierarchyService; |
| | | 16 | | |
| | 40 | 17 | | public ArticleService(ChronicisDbContext context, ILogger<ArticleService> logger, IArticleHierarchyService hiera |
| | | 18 | | { |
| | 40 | 19 | | _context = context; |
| | 40 | 20 | | _logger = logger; |
| | 40 | 21 | | _hierarchyService = hierarchyService; |
| | 40 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Get all articles the user has access to via WorldMembers. |
| | | 26 | | /// Private articles are only visible to their creator. |
| | | 27 | | /// This is the base query for all article access - use this instead of filtering by CreatedBy. |
| | | 28 | | /// </summary> |
| | | 29 | | private IQueryable<Article> GetAccessibleArticles(Guid userId) |
| | | 30 | | { |
| | 46 | 31 | | return from a in _context.Articles |
| | 46 | 32 | | join wm in _context.WorldMembers on a.WorldId equals wm.WorldId |
| | 46 | 33 | | where wm.UserId == userId |
| | 46 | 34 | | // Private articles only visible to creator |
| | 46 | 35 | | where a.Visibility != ArticleVisibility.Private || a.CreatedBy == userId |
| | 46 | 36 | | select a; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Get all root-level articles (ParentId is null) for worlds the user has access to. |
| | | 41 | | /// Optionally filter by WorldId. |
| | | 42 | | /// </summary> |
| | | 43 | | public async Task<List<ArticleTreeDto>> GetRootArticlesAsync(Guid userId, Guid? worldId = null) |
| | | 44 | | { |
| | 5 | 45 | | var query = GetAccessibleArticles(userId) |
| | 5 | 46 | | .AsNoTracking() |
| | 5 | 47 | | .Where(a => a.ParentId == null); |
| | | 48 | | |
| | 5 | 49 | | if (worldId.HasValue) |
| | | 50 | | { |
| | 1 | 51 | | query = query.Where(a => a.WorldId == worldId.Value); |
| | | 52 | | } |
| | | 53 | | |
| | 5 | 54 | | var rootArticles = await query |
| | 5 | 55 | | .Select(a => new ArticleTreeDto |
| | 5 | 56 | | { |
| | 5 | 57 | | Id = a.Id, |
| | 5 | 58 | | Title = a.Title, |
| | 5 | 59 | | Slug = a.Slug, |
| | 5 | 60 | | ParentId = a.ParentId, |
| | 5 | 61 | | WorldId = a.WorldId, |
| | 5 | 62 | | CampaignId = a.CampaignId, |
| | 5 | 63 | | ArcId = a.ArcId, |
| | 5 | 64 | | Type = a.Type, |
| | 5 | 65 | | Visibility = a.Visibility, |
| | 5 | 66 | | HasChildren = _context.Articles.Any(c => c.ParentId == a.Id), |
| | 5 | 67 | | ChildCount = _context.Articles.Count(c => c.ParentId == a.Id), |
| | 5 | 68 | | Children = new List<ArticleTreeDto>(), |
| | 5 | 69 | | CreatedAt = a.CreatedAt, |
| | 5 | 70 | | EffectiveDate = a.EffectiveDate, |
| | 5 | 71 | | IconEmoji = a.IconEmoji, |
| | 5 | 72 | | CreatedBy = a.CreatedBy, |
| | 5 | 73 | | HasAISummary = a.AISummary != null |
| | 5 | 74 | | }) |
| | 5 | 75 | | .OrderBy(a => a.Title) |
| | 5 | 76 | | .ToListAsync(); |
| | | 77 | | |
| | 5 | 78 | | return rootArticles; |
| | 5 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Get all articles for worlds the user has access to, in a flat list (no hierarchy). |
| | | 83 | | /// Useful for dropdowns, linking dialogs, etc. |
| | | 84 | | /// Optionally filter by WorldId. |
| | | 85 | | /// </summary> |
| | | 86 | | public async Task<List<ArticleTreeDto>> GetAllArticlesAsync(Guid userId, Guid? worldId = null) |
| | | 87 | | { |
| | 3 | 88 | | var query = GetAccessibleArticles(userId).AsNoTracking(); |
| | | 89 | | |
| | 3 | 90 | | if (worldId.HasValue) |
| | | 91 | | { |
| | 1 | 92 | | query = query.Where(a => a.WorldId == worldId.Value); |
| | | 93 | | } |
| | | 94 | | |
| | 3 | 95 | | var articles = await query |
| | 3 | 96 | | .Select(a => new ArticleTreeDto |
| | 3 | 97 | | { |
| | 3 | 98 | | Id = a.Id, |
| | 3 | 99 | | Title = a.Title, |
| | 3 | 100 | | Slug = a.Slug, |
| | 3 | 101 | | ParentId = a.ParentId, |
| | 3 | 102 | | WorldId = a.WorldId, |
| | 3 | 103 | | CampaignId = a.CampaignId, |
| | 3 | 104 | | ArcId = a.ArcId, |
| | 3 | 105 | | Type = a.Type, |
| | 3 | 106 | | Visibility = a.Visibility, |
| | 3 | 107 | | HasChildren = false, // Not relevant in flat list |
| | 3 | 108 | | ChildCount = 0, // Not relevant in flat list |
| | 3 | 109 | | Children = new List<ArticleTreeDto>(), |
| | 3 | 110 | | CreatedAt = a.CreatedAt, |
| | 3 | 111 | | EffectiveDate = a.EffectiveDate, |
| | 3 | 112 | | IconEmoji = a.IconEmoji, |
| | 3 | 113 | | CreatedBy = a.CreatedBy, |
| | 3 | 114 | | HasAISummary = a.AISummary != null |
| | 3 | 115 | | // Note: Aliases intentionally not loaded here - loaded separately via GetLinkSuggestions |
| | 3 | 116 | | }) |
| | 3 | 117 | | .OrderBy(a => a.Title) |
| | 3 | 118 | | .ToListAsync(); |
| | | 119 | | |
| | 3 | 120 | | return articles; |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Get all child articles of a specific parent. |
| | | 125 | | /// User must have access to the article's world via WorldMembers. |
| | | 126 | | /// </summary> |
| | | 127 | | public async Task<List<ArticleTreeDto>> GetChildrenAsync(Guid parentId, Guid userId) |
| | | 128 | | { |
| | 3 | 129 | | var children = await GetAccessibleArticles(userId) |
| | 3 | 130 | | .AsNoTracking() |
| | 3 | 131 | | .Where(a => a.ParentId == parentId) |
| | 3 | 132 | | .Select(a => new ArticleTreeDto |
| | 3 | 133 | | { |
| | 3 | 134 | | Id = a.Id, |
| | 3 | 135 | | Title = a.Title, |
| | 3 | 136 | | Slug = a.Slug, |
| | 3 | 137 | | ParentId = a.ParentId, |
| | 3 | 138 | | WorldId = a.WorldId, |
| | 3 | 139 | | CampaignId = a.CampaignId, |
| | 3 | 140 | | ArcId = a.ArcId, |
| | 3 | 141 | | Type = a.Type, |
| | 3 | 142 | | Visibility = a.Visibility, |
| | 3 | 143 | | HasChildren = _context.Articles.Any(c => c.ParentId == a.Id), |
| | 3 | 144 | | ChildCount = _context.Articles.Count(c => c.ParentId == a.Id), |
| | 3 | 145 | | Children = new List<ArticleTreeDto>(), |
| | 3 | 146 | | CreatedAt = a.CreatedAt, |
| | 3 | 147 | | EffectiveDate = a.EffectiveDate, |
| | 3 | 148 | | IconEmoji = a.IconEmoji, |
| | 3 | 149 | | CreatedBy = a.CreatedBy, |
| | 3 | 150 | | HasAISummary = a.AISummary != null |
| | 3 | 151 | | }) |
| | 3 | 152 | | .OrderBy(a => a.Title) |
| | 3 | 153 | | .ToListAsync(); |
| | | 154 | | |
| | 3 | 155 | | return children; |
| | 3 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Get full article details including breadcrumb path from root. |
| | | 160 | | /// User must have access to the article's world via WorldMembers. |
| | | 161 | | /// </summary> |
| | | 162 | | public async Task<ArticleDto?> GetArticleDetailAsync(Guid id, Guid userId) |
| | | 163 | | { |
| | 7 | 164 | | var article = await GetAccessibleArticles(userId) |
| | 7 | 165 | | .AsNoTracking() |
| | 7 | 166 | | .Where(a => a.Id == id) |
| | 7 | 167 | | .Select(a => new ArticleDto |
| | 7 | 168 | | { |
| | 7 | 169 | | Id = a.Id, |
| | 7 | 170 | | Title = a.Title, |
| | 7 | 171 | | Slug = a.Slug, |
| | 7 | 172 | | ParentId = a.ParentId, |
| | 7 | 173 | | WorldId = a.WorldId, |
| | 7 | 174 | | CampaignId = a.CampaignId, |
| | 7 | 175 | | ArcId = a.ArcId, |
| | 7 | 176 | | Body = a.Body ?? string.Empty, |
| | 7 | 177 | | Type = a.Type, |
| | 7 | 178 | | Visibility = a.Visibility, |
| | 7 | 179 | | CreatedAt = a.CreatedAt, |
| | 7 | 180 | | ModifiedAt = a.ModifiedAt, |
| | 7 | 181 | | EffectiveDate = a.EffectiveDate, |
| | 7 | 182 | | CreatedBy = a.CreatedBy, |
| | 7 | 183 | | LastModifiedBy = a.LastModifiedBy, |
| | 7 | 184 | | IconEmoji = a.IconEmoji, |
| | 7 | 185 | | SessionDate = a.SessionDate, |
| | 7 | 186 | | InGameDate = a.InGameDate, |
| | 7 | 187 | | PlayerId = a.PlayerId, |
| | 7 | 188 | | AISummary = a.AISummary, |
| | 7 | 189 | | AISummaryGeneratedAt = a.AISummaryGeneratedAt, |
| | 7 | 190 | | Breadcrumbs = new List<BreadcrumbDto>(), // Will populate separately |
| | 7 | 191 | | Aliases = a.Aliases.Select(al => new ArticleAliasDto |
| | 7 | 192 | | { |
| | 7 | 193 | | Id = al.Id, |
| | 7 | 194 | | AliasText = al.AliasText, |
| | 7 | 195 | | AliasType = al.AliasType, |
| | 7 | 196 | | EffectiveDate = al.EffectiveDate, |
| | 7 | 197 | | CreatedAt = al.CreatedAt |
| | 7 | 198 | | }).ToList() |
| | 7 | 199 | | }) |
| | 7 | 200 | | .FirstOrDefaultAsync(); |
| | | 201 | | |
| | 7 | 202 | | if (article == null) |
| | | 203 | | { |
| | 3 | 204 | | _logger.LogWarning("Article {ArticleId} not found", id); |
| | 3 | 205 | | return null; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // Build breadcrumb path using centralised hierarchy service |
| | 4 | 209 | | article.Breadcrumbs = await _hierarchyService.BuildBreadcrumbsAsync(id); |
| | | 210 | | |
| | 4 | 211 | | return article; |
| | 7 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Move an article to a new parent (or to root if newParentId is null). |
| | | 216 | | /// </summary> |
| | | 217 | | public async Task<(bool Success, string? ErrorMessage)> MoveArticleAsync(Guid articleId, Guid? newParentId, Guid |
| | | 218 | | { |
| | | 219 | | // 1. Get the article to move (must be in a world user has access to) |
| | 8 | 220 | | var article = await GetAccessibleArticles(userId) |
| | 8 | 221 | | .FirstOrDefaultAsync(a => a.Id == articleId); |
| | | 222 | | |
| | 8 | 223 | | if (article == null) |
| | | 224 | | { |
| | 1 | 225 | | _logger.LogWarning("Article {ArticleId} not found for user {UserId}", articleId, userId); |
| | 1 | 226 | | return (false, "Article not found"); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | // 2. If moving to same parent, nothing to do |
| | 7 | 230 | | if (article.ParentId == newParentId) |
| | | 231 | | { |
| | 1 | 232 | | return (true, null); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | // 3. If newParentId is specified, validate the target exists and user has access |
| | 6 | 236 | | if (newParentId.HasValue) |
| | | 237 | | { |
| | 4 | 238 | | var targetParent = await GetAccessibleArticles(userId) |
| | 4 | 239 | | .AsNoTracking() |
| | 4 | 240 | | .FirstOrDefaultAsync(a => a.Id == newParentId.Value); |
| | | 241 | | |
| | 4 | 242 | | if (targetParent == null) |
| | | 243 | | { |
| | 1 | 244 | | _logger.LogWarning("Target parent {NewParentId} not found for user {UserId}", newParentId, userId); |
| | 1 | 245 | | return (false, "Target parent article not found"); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // 4. Check for circular reference - cannot move an article to be a child of itself or its descendants |
| | 3 | 249 | | if (await WouldCreateCircularReferenceAsync(articleId, newParentId.Value, userId)) |
| | | 250 | | { |
| | 2 | 251 | | _logger.LogWarning("Moving article {ArticleId} to {NewParentId} would create circular reference", |
| | 2 | 252 | | articleId, newParentId); |
| | 2 | 253 | | return (false, "Cannot move an article to be a child of itself or its descendants"); |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | // 5. Perform the move |
| | 3 | 258 | | article.ParentId = newParentId; |
| | 3 | 259 | | article.ModifiedAt = DateTime.UtcNow; |
| | 3 | 260 | | article.LastModifiedBy = userId; |
| | | 261 | | |
| | 3 | 262 | | await _context.SaveChangesAsync(); |
| | | 263 | | |
| | 3 | 264 | | return (true, null); |
| | 8 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Check if moving articleId to become a child of targetParentId would create a circular reference. |
| | | 269 | | /// </summary> |
| | | 270 | | private async Task<bool> WouldCreateCircularReferenceAsync(Guid articleId, Guid targetParentId, Guid userId) |
| | | 271 | | { |
| | | 272 | | // If trying to move to self, that's circular |
| | 3 | 273 | | if (articleId == targetParentId) |
| | | 274 | | { |
| | 1 | 275 | | return true; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | // Walk up from targetParentId to root, checking if we encounter articleId |
| | 2 | 279 | | var currentId = (Guid?)targetParentId; |
| | 2 | 280 | | var visited = new HashSet<Guid>(); |
| | | 281 | | |
| | 5 | 282 | | while (currentId.HasValue) |
| | | 283 | | { |
| | | 284 | | // Prevent infinite loops (shouldn't happen with valid data, but safety first) |
| | 4 | 285 | | if (visited.Contains(currentId.Value)) |
| | | 286 | | { |
| | 0 | 287 | | _logger.LogError("Detected existing circular reference in hierarchy at article {ArticleId}", current |
| | 0 | 288 | | return true; |
| | | 289 | | } |
| | 4 | 290 | | visited.Add(currentId.Value); |
| | | 291 | | |
| | | 292 | | // If we find the article we're trying to move in the ancestor chain, it's circular |
| | 4 | 293 | | if (currentId.Value == articleId) |
| | | 294 | | { |
| | 1 | 295 | | return true; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Move up to parent |
| | 3 | 299 | | var parent = await GetAccessibleArticles(userId) |
| | 3 | 300 | | .AsNoTracking() |
| | 3 | 301 | | .Where(a => a.Id == currentId.Value) |
| | 3 | 302 | | .Select(a => new { a.ParentId }) |
| | 3 | 303 | | .FirstOrDefaultAsync(); |
| | | 304 | | |
| | 3 | 305 | | currentId = parent?.ParentId; |
| | | 306 | | } |
| | | 307 | | |
| | 1 | 308 | | return false; |
| | 3 | 309 | | } |
| | | 310 | | |
| | | 311 | | |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Get article by hierarchical path. |
| | | 315 | | /// Path format: "world-slug/article-slug/child-slug" (e.g., "stormlight/wiki/characters"). |
| | | 316 | | /// The first segment is the world slug, remaining segments are article hierarchy. |
| | | 317 | | /// </summary> |
| | | 318 | | public async Task<ArticleDto?> GetArticleByPathAsync(string path, Guid userId) |
| | | 319 | | { |
| | 7 | 320 | | if (string.IsNullOrWhiteSpace(path)) |
| | 1 | 321 | | return null; |
| | | 322 | | |
| | 6 | 323 | | var slugs = path.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| | 6 | 324 | | if (slugs.Length == 0) |
| | 0 | 325 | | return null; |
| | | 326 | | |
| | | 327 | | // First segment is the world slug |
| | 6 | 328 | | var worldSlug = slugs[0]; |
| | | 329 | | |
| | | 330 | | // Look up the world by slug - user must be a member of the world |
| | 6 | 331 | | var world = await _context.Worlds |
| | 6 | 332 | | .AsNoTracking() |
| | 6 | 333 | | .Where(w => w.Slug == worldSlug && w.Members.Any(m => m.UserId == userId)) |
| | 6 | 334 | | .Select(w => new { w.Id }) |
| | 6 | 335 | | .FirstOrDefaultAsync(); |
| | | 336 | | |
| | 6 | 337 | | if (world == null) |
| | | 338 | | { |
| | 2 | 339 | | _logger.LogWarningSanitized("World not found for slug '{WorldSlug}' or user {UserId} doesn't have access |
| | 2 | 340 | | return null; |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | // If only world slug provided, no article to return |
| | 4 | 344 | | if (slugs.Length == 1) |
| | | 345 | | { |
| | 1 | 346 | | _logger.LogWarningSanitized("Path '{Path}' contains only world slug, no article path", path); |
| | 1 | 347 | | return null; |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | // Remaining segments are the article path within the world |
| | 3 | 351 | | Guid? currentParentId = null; |
| | 3 | 352 | | Guid? articleId = null; |
| | | 353 | | |
| | | 354 | | // Walk down the tree using slugs (starting from index 1, skipping world slug) |
| | 14 | 355 | | for (int i = 1; i < slugs.Length; i++) |
| | | 356 | | { |
| | 5 | 357 | | var slug = slugs[i]; |
| | 5 | 358 | | var isRootLevel = (i == 1); // First article slug (index 1) is at root level |
| | | 359 | | |
| | | 360 | | Article? article; |
| | 5 | 361 | | if (isRootLevel) |
| | | 362 | | { |
| | | 363 | | // Root-level article: filter by WorldId and ParentId = null |
| | 3 | 364 | | article = await GetAccessibleArticles(userId) |
| | 3 | 365 | | .AsNoTracking() |
| | 3 | 366 | | .Where(a => a.Slug == slug && |
| | 3 | 367 | | a.ParentId == null && |
| | 3 | 368 | | a.WorldId == world.Id) |
| | 3 | 369 | | .FirstOrDefaultAsync(); |
| | | 370 | | } |
| | | 371 | | else |
| | | 372 | | { |
| | | 373 | | // Child article: filter by ParentId |
| | 2 | 374 | | article = await GetAccessibleArticles(userId) |
| | 2 | 375 | | .AsNoTracking() |
| | 2 | 376 | | .Where(a => a.Slug == slug && |
| | 2 | 377 | | a.ParentId == currentParentId) |
| | 2 | 378 | | .FirstOrDefaultAsync(); |
| | | 379 | | } |
| | | 380 | | |
| | 5 | 381 | | if (article == null) |
| | | 382 | | { |
| | 1 | 383 | | _logger.LogWarningSanitized("Article not found for slug '{Slug}' under parent {ParentId} in world {W |
| | 1 | 384 | | slug, currentParentId, world.Id, userId); |
| | 1 | 385 | | return null; |
| | | 386 | | } |
| | | 387 | | |
| | 4 | 388 | | articleId = article.Id; |
| | 4 | 389 | | currentParentId = article.Id; // Next iteration looks for children of this article |
| | 4 | 390 | | } |
| | | 391 | | |
| | | 392 | | // Found the article, now get full details |
| | 2 | 393 | | return articleId.HasValue |
| | 2 | 394 | | ? await GetArticleDetailAsync(articleId.Value, userId) |
| | 2 | 395 | | : null; |
| | 7 | 396 | | } |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Check if a slug is unique among siblings. |
| | | 400 | | /// For root articles (ParentId is null), checks uniqueness within the World. |
| | | 401 | | /// For child articles, checks uniqueness within the same parent. |
| | | 402 | | /// </summary> |
| | | 403 | | public async Task<bool> IsSlugUniqueAsync(string slug, Guid? parentId, Guid? worldId, Guid userId, Guid? exclude |
| | | 404 | | { |
| | | 405 | | IQueryable<Article> query; |
| | | 406 | | |
| | 5 | 407 | | if (parentId.HasValue) |
| | | 408 | | { |
| | | 409 | | // Child article: unique among siblings with same parent |
| | 2 | 410 | | query = GetAccessibleArticles(userId) |
| | 2 | 411 | | .AsNoTracking() |
| | 2 | 412 | | .Where(a => a.Slug == slug && |
| | 2 | 413 | | a.ParentId == parentId); |
| | | 414 | | } |
| | | 415 | | else |
| | | 416 | | { |
| | | 417 | | // Root article: unique among root articles in the same world |
| | 3 | 418 | | query = GetAccessibleArticles(userId) |
| | 3 | 419 | | .AsNoTracking() |
| | 3 | 420 | | .Where(a => a.Slug == slug && |
| | 3 | 421 | | a.ParentId == null && |
| | 3 | 422 | | a.WorldId == worldId); |
| | | 423 | | } |
| | | 424 | | |
| | 5 | 425 | | if (excludeArticleId.HasValue) |
| | | 426 | | { |
| | 1 | 427 | | query = query.Where(a => a.Id != excludeArticleId.Value); |
| | | 428 | | } |
| | | 429 | | |
| | 5 | 430 | | return !await query.AnyAsync(); |
| | 5 | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Generate a unique slug for an article among its siblings. |
| | | 435 | | /// For root articles (ParentId is null), checks uniqueness within the World. |
| | | 436 | | /// For child articles, checks uniqueness within the same parent. |
| | | 437 | | /// </summary> |
| | | 438 | | public async Task<string> GenerateUniqueSlugAsync(string title, Guid? parentId, Guid? worldId, Guid userId, Guid |
| | | 439 | | { |
| | 3 | 440 | | var baseSlug = SlugGenerator.GenerateSlug(title); |
| | | 441 | | |
| | | 442 | | IQueryable<Article> query; |
| | | 443 | | |
| | 3 | 444 | | if (parentId.HasValue) |
| | | 445 | | { |
| | | 446 | | // Child article: get slugs from siblings with same parent |
| | 0 | 447 | | query = GetAccessibleArticles(userId) |
| | 0 | 448 | | .AsNoTracking() |
| | 0 | 449 | | .Where(a => a.ParentId == parentId); |
| | | 450 | | } |
| | | 451 | | else |
| | | 452 | | { |
| | | 453 | | // Root article: get slugs from root articles in the same world |
| | 3 | 454 | | query = GetAccessibleArticles(userId) |
| | 3 | 455 | | .AsNoTracking() |
| | 3 | 456 | | .Where(a => a.ParentId == null && a.WorldId == worldId); |
| | | 457 | | } |
| | | 458 | | |
| | 3 | 459 | | var existingSlugs = await query |
| | 3 | 460 | | .Where(a => !excludeArticleId.HasValue || a.Id != excludeArticleId.Value) |
| | 3 | 461 | | .Select(a => a.Slug) |
| | 3 | 462 | | .ToHashSetAsync(); |
| | | 463 | | |
| | 3 | 464 | | return SlugGenerator.GenerateUniqueSlug(baseSlug, existingSlugs); |
| | 3 | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Build the full hierarchical path for an article, including world slug. |
| | | 469 | | /// Returns format: "world-slug/article-slug/child-slug" |
| | | 470 | | /// </summary> |
| | | 471 | | public async Task<string> BuildArticlePathAsync(Guid articleId, Guid userId) |
| | | 472 | | { |
| | 1 | 473 | | return await _hierarchyService.BuildPathAsync(articleId); |
| | 1 | 474 | | } |
| | | 475 | | } |
| | | 476 | | } |