| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for wiki link operations including creating articles from autocomplete |
| | | 7 | | /// and finding wiki folder locations. |
| | | 8 | | /// </summary> |
| | | 9 | | public interface IWikiLinkService |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new article from autocomplete and returns it. |
| | | 13 | | /// The article is created in the Wiki folder if one exists. |
| | | 14 | | /// </summary> |
| | | 15 | | Task<ArticleDto?> CreateArticleFromAutocompleteAsync(string articleName, Guid worldId); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Finds the Wiki folder for a given world. |
| | | 19 | | /// </summary> |
| | | 20 | | Task<Guid?> FindWikiFolderAsync(Guid worldId); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public class WikiLinkService : IWikiLinkService |
| | | 24 | | { |
| | | 25 | | private readonly IArticleApiService _articleApi; |
| | | 26 | | private readonly ILogger<WikiLinkService> _logger; |
| | | 27 | | |
| | 13 | 28 | | public WikiLinkService(IArticleApiService articleApi, ILogger<WikiLinkService> logger) |
| | | 29 | | { |
| | 13 | 30 | | _articleApi = articleApi; |
| | 13 | 31 | | _logger = logger; |
| | 13 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<ArticleDto?> CreateArticleFromAutocompleteAsync(string articleName, Guid worldId) |
| | | 35 | | { |
| | 6 | 36 | | if (string.IsNullOrWhiteSpace(articleName)) |
| | | 37 | | { |
| | 3 | 38 | | return null; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | | 43 | | // Find the Wiki folder for this world |
| | 3 | 44 | | Guid? wikiParentId = await FindWikiFolderAsync(worldId); |
| | | 45 | | |
| | 3 | 46 | | var createDto = new ArticleCreateDto |
| | 3 | 47 | | { |
| | 3 | 48 | | Title = articleName, |
| | 3 | 49 | | Body = string.Empty, |
| | 3 | 50 | | ParentId = wikiParentId, |
| | 3 | 51 | | EffectiveDate = DateTime.Now, |
| | 3 | 52 | | WorldId = worldId |
| | 3 | 53 | | }; |
| | | 54 | | |
| | 3 | 55 | | var created = await _articleApi.CreateArticleAsync(createDto); |
| | 3 | 56 | | return created; |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | _logger.LogError(ex, "Error creating article from autocomplete: {Name}", articleName); |
| | 0 | 61 | | return null; |
| | | 62 | | } |
| | 6 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<Guid?> FindWikiFolderAsync(Guid worldId) |
| | | 66 | | { |
| | | 67 | | try |
| | | 68 | | { |
| | | 69 | | // The Wiki folder is typically a child of the World article |
| | 10 | 70 | | var rootArticles = await _articleApi.GetRootArticlesAsync(worldId); |
| | | 71 | | |
| | 8 | 72 | | if (rootArticles.Any()) |
| | | 73 | | { |
| | 6 | 74 | | var worldArticle = rootArticles.First(); |
| | 6 | 75 | | var worldChildren = await _articleApi.GetChildrenAsync(worldArticle.Id); |
| | | 76 | | |
| | | 77 | | // Look for an article named "Wiki" (case-insensitive) |
| | 6 | 78 | | var wikiFolder = worldChildren.FirstOrDefault(a => |
| | 11 | 79 | | a.Title.Equals("Wiki", StringComparison.OrdinalIgnoreCase)); |
| | | 80 | | |
| | 6 | 81 | | if (wikiFolder != null) |
| | | 82 | | { |
| | 4 | 83 | | return wikiFolder.Id; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // Fallback: check if Wiki is at root level |
| | 4 | 88 | | var rootWiki = rootArticles.FirstOrDefault(a => |
| | 7 | 89 | | a.Title.Equals("Wiki", StringComparison.OrdinalIgnoreCase)); |
| | | 90 | | |
| | 4 | 91 | | if (rootWiki != null) |
| | | 92 | | { |
| | 1 | 93 | | return rootWiki.Id; |
| | | 94 | | } |
| | | 95 | | |
| | 3 | 96 | | return null; |
| | | 97 | | } |
| | 2 | 98 | | catch (Exception ex) |
| | | 99 | | { |
| | 2 | 100 | | _logger.LogError(ex, "Error finding Wiki folder for world {WorldId}", worldId); |
| | 2 | 101 | | return null; |
| | | 102 | | } |
| | 10 | 103 | | } |
| | | 104 | | } |