| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for communicating with the Article API. |
| | | 7 | | /// Uses HttpClientExtensions for consistent error handling and logging. |
| | | 8 | | /// </summary> |
| | | 9 | | public class ArticleApiService : IArticleApiService |
| | | 10 | | { |
| | | 11 | | private readonly HttpClient _http; |
| | | 12 | | private readonly ILogger<ArticleApiService> _logger; |
| | | 13 | | |
| | 0 | 14 | | public ArticleApiService(HttpClient http, ILogger<ArticleApiService> logger) |
| | | 15 | | { |
| | 0 | 16 | | _http = http; |
| | 0 | 17 | | _logger = logger; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<List<ArticleTreeDto>> GetRootArticlesAsync(Guid? worldId = null) |
| | | 21 | | { |
| | 0 | 22 | | var url = worldId.HasValue |
| | 0 | 23 | | ? $"articles?worldId={worldId.Value}" |
| | 0 | 24 | | : "articles"; |
| | | 25 | | |
| | 0 | 26 | | return await _http.GetListAsync<ArticleTreeDto>(url, _logger, "root articles"); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<List<ArticleTreeDto>> GetAllArticlesAsync(Guid? worldId = null) |
| | | 30 | | { |
| | 0 | 31 | | var url = worldId.HasValue |
| | 0 | 32 | | ? $"articles/all?worldId={worldId.Value}" |
| | 0 | 33 | | : "articles/all"; |
| | | 34 | | |
| | 0 | 35 | | return await _http.GetListAsync<ArticleTreeDto>(url, _logger, "all articles"); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<List<ArticleTreeDto>> GetChildrenAsync(Guid parentId) |
| | | 39 | | { |
| | 0 | 40 | | return await _http.GetListAsync<ArticleTreeDto>( |
| | 0 | 41 | | $"articles/{parentId}/children", |
| | 0 | 42 | | _logger, |
| | 0 | 43 | | $"children for article {parentId}"); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | public async Task<ArticleDto?> GetArticleDetailAsync(Guid id) |
| | | 47 | | { |
| | 0 | 48 | | return await _http.GetEntityAsync<ArticleDto>( |
| | 0 | 49 | | $"articles/{id}", |
| | 0 | 50 | | _logger, |
| | 0 | 51 | | $"article {id}"); |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | public async Task<ArticleDto?> GetArticleAsync(Guid id) => await GetArticleDetailAsync(id); |
| | | 55 | | |
| | | 56 | | public async Task<ArticleDto?> GetArticleByPathAsync(string path) |
| | | 57 | | { |
| | 0 | 58 | | var encodedPath = string.Join("/", path.Split('/').Select(Uri.EscapeDataString)); |
| | | 59 | | |
| | 0 | 60 | | return await _http.GetEntityAsync<ArticleDto>( |
| | 0 | 61 | | $"articles/by-path/{encodedPath}", |
| | 0 | 62 | | _logger, |
| | 0 | 63 | | $"article at path '{path}'"); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public async Task<ArticleDto?> CreateArticleAsync(ArticleCreateDto dto) |
| | | 67 | | { |
| | 0 | 68 | | return await _http.PostEntityAsync<ArticleDto>( |
| | 0 | 69 | | "articles", |
| | 0 | 70 | | dto, |
| | 0 | 71 | | _logger, |
| | 0 | 72 | | "article"); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<ArticleDto?> UpdateArticleAsync(Guid id, ArticleUpdateDto dto) |
| | | 76 | | { |
| | 0 | 77 | | return await _http.PutEntityAsync<ArticleDto>( |
| | 0 | 78 | | $"articles/{id}", |
| | 0 | 79 | | dto, |
| | 0 | 80 | | _logger, |
| | 0 | 81 | | $"article {id}"); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task<bool> DeleteArticleAsync(Guid id) |
| | | 85 | | { |
| | 0 | 86 | | return await _http.DeleteEntityAsync( |
| | 0 | 87 | | $"articles/{id}", |
| | 0 | 88 | | _logger, |
| | 0 | 89 | | $"article {id}"); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public async Task<bool> MoveArticleAsync(Guid articleId, Guid? newParentId) |
| | | 93 | | { |
| | 0 | 94 | | var moveDto = new ArticleMoveDto { NewParentId = newParentId }; |
| | | 95 | | |
| | 0 | 96 | | return await _http.PutBoolAsync( |
| | 0 | 97 | | $"articles/{articleId}/move", |
| | 0 | 98 | | moveDto, |
| | 0 | 99 | | _logger, |
| | 0 | 100 | | $"move article {articleId}"); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public async Task<List<ArticleSearchResultDto>> SearchArticlesAsync(string query) |
| | | 104 | | { |
| | 0 | 105 | | if (string.IsNullOrWhiteSpace(query)) |
| | 0 | 106 | | return new List<ArticleSearchResultDto>(); |
| | | 107 | | |
| | 0 | 108 | | var results = await _http.GetEntityAsync<GlobalSearchResultsDto>( |
| | 0 | 109 | | $"articles/search?query={Uri.EscapeDataString(query)}", |
| | 0 | 110 | | _logger, |
| | 0 | 111 | | $"search results for '{query}'"); |
| | | 112 | | |
| | 0 | 113 | | if (results == null) |
| | 0 | 114 | | return new List<ArticleSearchResultDto>(); |
| | | 115 | | |
| | | 116 | | // Combine all match types into a single list |
| | 0 | 117 | | var allResults = new List<ArticleSearchResultDto>(); |
| | 0 | 118 | | allResults.AddRange(results.TitleMatches); |
| | 0 | 119 | | allResults.AddRange(results.BodyMatches); |
| | 0 | 120 | | allResults.AddRange(results.HashtagMatches); |
| | 0 | 121 | | return allResults; |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | public async Task<List<ArticleSearchResultDto>> SearchArticlesByTitleAsync(string query) |
| | | 125 | | { |
| | 0 | 126 | | if (string.IsNullOrWhiteSpace(query)) |
| | 0 | 127 | | return new List<ArticleSearchResultDto>(); |
| | | 128 | | |
| | 0 | 129 | | var results = await _http.GetEntityAsync<GlobalSearchResultsDto>( |
| | 0 | 130 | | $"articles/search?query={Uri.EscapeDataString(query)}", |
| | 0 | 131 | | _logger, |
| | 0 | 132 | | $"title search results for '{query}'"); |
| | | 133 | | |
| | 0 | 134 | | return results?.TitleMatches ?? new List<ArticleSearchResultDto>(); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | public async Task<ArticleDto?> UpdateAliasesAsync(Guid articleId, string aliases) |
| | | 138 | | { |
| | 0 | 139 | | var dto = new ArticleAliasesUpdateDto { Aliases = aliases ?? string.Empty }; |
| | | 140 | | |
| | 0 | 141 | | return await _http.PutEntityAsync<ArticleDto>( |
| | 0 | 142 | | $"articles/{articleId}/aliases", |
| | 0 | 143 | | dto, |
| | 0 | 144 | | _logger, |
| | 0 | 145 | | $"aliases for article {articleId}"); |
| | 0 | 146 | | } |
| | | 147 | | } |