| | | 1 | | using Chronicis.Api.Services.ExternalLinks; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Extensions; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | |
| | | 6 | | namespace Chronicis.Api.Controllers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// API endpoints for External Link operations (SRD content, etc.) |
| | | 10 | | /// </summary> |
| | | 11 | | [ApiController] |
| | | 12 | | [Route("external-links")] |
| | | 13 | | // [Authorize] // Temporarily disabled for testing |
| | | 14 | | public class ExternalLinksController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IExternalLinkService _externalLinkService; |
| | | 17 | | private readonly ILogger<ExternalLinksController> _logger; |
| | | 18 | | |
| | 0 | 19 | | public ExternalLinksController( |
| | 0 | 20 | | IExternalLinkService externalLinkService, |
| | 0 | 21 | | ILogger<ExternalLinksController> logger) |
| | | 22 | | { |
| | 0 | 23 | | _externalLinkService = externalLinkService; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// GET /api/external-links/suggestions?worldId={worldId}&source={source}&query={query} |
| | | 29 | | /// Get external link suggestions for autocomplete. |
| | | 30 | | /// Filters to only include providers enabled for the specified world. |
| | | 31 | | /// </summary> |
| | | 32 | | [HttpGet("suggestions")] |
| | | 33 | | public async Task<ActionResult<List<ExternalLinkSuggestionDto>>> GetSuggestions( |
| | | 34 | | [FromQuery] Guid? worldId, |
| | | 35 | | [FromQuery] string? source, |
| | | 36 | | [FromQuery] string? query, |
| | | 37 | | CancellationToken ct) |
| | | 38 | | { |
| | 0 | 39 | | if (string.IsNullOrWhiteSpace(source)) |
| | | 40 | | { |
| | 0 | 41 | | return Ok(new List<ExternalLinkSuggestionDto>()); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | _logger.LogDebugSanitized( |
| | 0 | 45 | | "Getting external link suggestions for world {WorldId}, source '{Source}' with query '{Query}'", |
| | 0 | 46 | | worldId, source, query); |
| | | 47 | | |
| | 0 | 48 | | var suggestions = await _externalLinkService.GetSuggestionsAsync(worldId, source, query ?? "", ct); |
| | | 49 | | |
| | 0 | 50 | | var dtos = suggestions.Select(s => new ExternalLinkSuggestionDto |
| | 0 | 51 | | { |
| | 0 | 52 | | Source = s.Source, |
| | 0 | 53 | | Id = s.Id, |
| | 0 | 54 | | Title = s.Title, |
| | 0 | 55 | | Subtitle = s.Subtitle, |
| | 0 | 56 | | Category = s.Category, |
| | 0 | 57 | | Icon = s.Icon, |
| | 0 | 58 | | Href = s.Href |
| | 0 | 59 | | }).ToList(); |
| | | 60 | | |
| | 0 | 61 | | return Ok(dtos); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// GET /api/external-links/content?source={source}&id={id} |
| | | 66 | | /// Get external link content for display. |
| | | 67 | | /// </summary> |
| | | 68 | | [HttpGet("content")] |
| | | 69 | | public async Task<ActionResult<ExternalLinkContentDto>> GetContent( |
| | | 70 | | [FromQuery] string? source, |
| | | 71 | | [FromQuery] string? id, |
| | | 72 | | CancellationToken ct) |
| | | 73 | | { |
| | 0 | 74 | | if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(id)) |
| | | 75 | | { |
| | 0 | 76 | | return BadRequest(new ExternalLinkErrorDto { Message = "Source and id are required" }); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | _logger.LogDebugSanitized( |
| | 0 | 80 | | "Getting external link content for source '{Source}' with id '{Id}'", |
| | 0 | 81 | | source, id); |
| | | 82 | | |
| | 0 | 83 | | var content = await _externalLinkService.GetContentAsync(source, id, ct); |
| | | 84 | | |
| | 0 | 85 | | if (content == null) |
| | | 86 | | { |
| | 0 | 87 | | return NotFound(new ExternalLinkErrorDto { Message = "Content not found" }); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | var dto = new ExternalLinkContentDto |
| | 0 | 91 | | { |
| | 0 | 92 | | Source = content.Source, |
| | 0 | 93 | | Id = content.Id, |
| | 0 | 94 | | Title = content.Title, |
| | 0 | 95 | | Kind = content.Kind, |
| | 0 | 96 | | Markdown = content.Markdown, |
| | 0 | 97 | | Attribution = content.Attribution, |
| | 0 | 98 | | ExternalUrl = content.ExternalUrl, |
| | 0 | 99 | | JsonData = content.JsonData |
| | 0 | 100 | | }; |
| | | 101 | | |
| | 0 | 102 | | return Ok(dto); |
| | 0 | 103 | | } |
| | | 104 | | } |