| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Chronicis.Api.Data; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Chronicis.Shared.Models; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Services.Articles; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Service for managing external resource links embedded in article content. |
| | | 11 | | /// </summary> |
| | | 12 | | public partial class ArticleExternalLinkService : IArticleExternalLinkService |
| | | 13 | | { |
| | | 14 | | private readonly ChronicisDbContext _context; |
| | | 15 | | private readonly ILogger<ArticleExternalLinkService> _logger; |
| | | 16 | | |
| | 0 | 17 | | public ArticleExternalLinkService( |
| | 0 | 18 | | ChronicisDbContext context, |
| | 0 | 19 | | ILogger<ArticleExternalLinkService> logger) |
| | | 20 | | { |
| | 0 | 21 | | _context = context; |
| | 0 | 22 | | _logger = logger; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Regex pattern to extract external links from article HTML. |
| | | 27 | | /// Matches: <span data-type="external-link" ... data-source="..." data-id="..." data-title="..."> |
| | | 28 | | /// </summary> |
| | | 29 | | [GeneratedRegex( |
| | | 30 | | @"<span[^>]*data-type=""external-link""[^>]*data-source=""([^""]*)""[^>]*data-id=""([^""]*)""[^>]*data-title=""( |
| | | 31 | | RegexOptions.IgnoreCase | RegexOptions.Compiled)] |
| | | 32 | | private static partial Regex ExternalLinkRegex(); |
| | | 33 | | |
| | | 34 | | public async Task SyncExternalLinksAsync(Guid articleId, string? htmlContent) |
| | | 35 | | { |
| | | 36 | | try |
| | | 37 | | { |
| | | 38 | | // Extract external links from HTML |
| | 0 | 39 | | var extractedLinks = ExtractExternalLinksFromHtml(htmlContent); |
| | | 40 | | |
| | 0 | 41 | | _logger.LogDebug( |
| | 0 | 42 | | "Extracted {Count} external links from article {ArticleId}", |
| | 0 | 43 | | extractedLinks.Count, |
| | 0 | 44 | | articleId); |
| | | 45 | | |
| | | 46 | | // Delete all existing external links for this article |
| | 0 | 47 | | var existingLinks = await _context.ArticleExternalLinks |
| | 0 | 48 | | .Where(ael => ael.ArticleId == articleId) |
| | 0 | 49 | | .ToListAsync(); |
| | | 50 | | |
| | 0 | 51 | | if (existingLinks.Any()) |
| | | 52 | | { |
| | 0 | 53 | | _context.ArticleExternalLinks.RemoveRange(existingLinks); |
| | 0 | 54 | | _logger.LogDebug( |
| | 0 | 55 | | "Removed {Count} existing external links for article {ArticleId}", |
| | 0 | 56 | | existingLinks.Count, |
| | 0 | 57 | | articleId); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // Insert new external links |
| | 0 | 61 | | if (extractedLinks.Any()) |
| | | 62 | | { |
| | 0 | 63 | | var newLinks = extractedLinks.Select(link => new ArticleExternalLink |
| | 0 | 64 | | { |
| | 0 | 65 | | Id = Guid.NewGuid(), |
| | 0 | 66 | | ArticleId = articleId, |
| | 0 | 67 | | Source = link.Source, |
| | 0 | 68 | | ExternalId = link.ExternalId, |
| | 0 | 69 | | DisplayTitle = link.DisplayTitle |
| | 0 | 70 | | }).ToList(); |
| | | 71 | | |
| | 0 | 72 | | await _context.ArticleExternalLinks.AddRangeAsync(newLinks); |
| | | 73 | | |
| | 0 | 74 | | _logger.LogDebug( |
| | 0 | 75 | | "Added {Count} new external links for article {ArticleId}", |
| | 0 | 76 | | newLinks.Count, |
| | 0 | 77 | | articleId); |
| | 0 | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | await _context.SaveChangesAsync(); |
| | 0 | 81 | | } |
| | 0 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 0 | 84 | | _logger.LogError( |
| | 0 | 85 | | ex, |
| | 0 | 86 | | "Error syncing external links for article {ArticleId}", |
| | 0 | 87 | | articleId); |
| | 0 | 88 | | throw; |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public async Task<List<ArticleExternalLinkDto>> GetExternalLinksForArticleAsync(Guid articleId) |
| | | 93 | | { |
| | | 94 | | try |
| | | 95 | | { |
| | 0 | 96 | | var links = await _context.ArticleExternalLinks |
| | 0 | 97 | | .Where(ael => ael.ArticleId == articleId) |
| | 0 | 98 | | .OrderBy(ael => ael.Source) |
| | 0 | 99 | | .ThenBy(ael => ael.DisplayTitle) |
| | 0 | 100 | | .Select(ael => new ArticleExternalLinkDto |
| | 0 | 101 | | { |
| | 0 | 102 | | Id = ael.Id, |
| | 0 | 103 | | ArticleId = ael.ArticleId, |
| | 0 | 104 | | Source = ael.Source, |
| | 0 | 105 | | ExternalId = ael.ExternalId, |
| | 0 | 106 | | DisplayTitle = ael.DisplayTitle |
| | 0 | 107 | | }) |
| | 0 | 108 | | .ToListAsync(); |
| | | 109 | | |
| | 0 | 110 | | _logger.LogDebug( |
| | 0 | 111 | | "Retrieved {Count} external links for article {ArticleId}", |
| | 0 | 112 | | links.Count, |
| | 0 | 113 | | articleId); |
| | | 114 | | |
| | 0 | 115 | | return links; |
| | | 116 | | } |
| | 0 | 117 | | catch (Exception ex) |
| | | 118 | | { |
| | 0 | 119 | | _logger.LogError( |
| | 0 | 120 | | ex, |
| | 0 | 121 | | "Error retrieving external links for article {ArticleId}", |
| | 0 | 122 | | articleId); |
| | 0 | 123 | | throw; |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Extracts external link information from HTML content. |
| | | 129 | | /// </summary> |
| | | 130 | | private List<(string Source, string ExternalId, string DisplayTitle)> ExtractExternalLinksFromHtml(string? htmlConte |
| | | 131 | | { |
| | 0 | 132 | | if (string.IsNullOrWhiteSpace(htmlContent)) |
| | | 133 | | { |
| | 0 | 134 | | return new List<(string, string, string)>(); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | var links = new List<(string Source, string ExternalId, string DisplayTitle)>(); |
| | 0 | 138 | | var matches = ExternalLinkRegex().Matches(htmlContent); |
| | | 139 | | |
| | 0 | 140 | | foreach (Match match in matches) |
| | | 141 | | { |
| | 0 | 142 | | if (match.Success && match.Groups.Count >= 4) |
| | | 143 | | { |
| | 0 | 144 | | var source = match.Groups[1].Value; |
| | 0 | 145 | | var externalId = match.Groups[2].Value; |
| | 0 | 146 | | var displayTitle = match.Groups[3].Value; |
| | | 147 | | |
| | | 148 | | // Validate that we have all required fields |
| | 0 | 149 | | if (!string.IsNullOrWhiteSpace(source) && |
| | 0 | 150 | | !string.IsNullOrWhiteSpace(externalId) && |
| | 0 | 151 | | !string.IsNullOrWhiteSpace(displayTitle)) |
| | | 152 | | { |
| | 0 | 153 | | links.Add((source, externalId, displayTitle)); |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | return links; |
| | | 159 | | } |
| | | 160 | | } |