| | | 1 | | using Chronicis.Api.Infrastructure; |
| | | 2 | | using Chronicis.Api.Services.Routing; |
| | | 3 | | using Chronicis.Shared.Routing; |
| | | 4 | | using Chronicis.Shared.Utilities; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Unified slug-path resolution endpoint. Anonymous-accessible; gating is handled by IReadAccessPolicyService. |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Route("paths")] |
| | | 15 | | [AllowAnonymous] |
| | | 16 | | public sealed class PathsController : ControllerBase |
| | | 17 | | { |
| | 1 | 18 | | private static readonly HashSet<string> KeywordSegments = |
| | 1 | 19 | | new(StringComparer.OrdinalIgnoreCase) { "wiki", "maps" }; |
| | | 20 | | |
| | | 21 | | private readonly ISlugPathResolver _resolver; |
| | | 22 | | private readonly ICurrentUserService _currentUserService; |
| | | 23 | | |
| | 21 | 24 | | public PathsController(ISlugPathResolver resolver, ICurrentUserService currentUserService) |
| | | 25 | | { |
| | 21 | 26 | | _resolver = resolver; |
| | 21 | 27 | | _currentUserService = currentUserService; |
| | 21 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// GET /paths/resolve/{*path} |
| | | 32 | | /// Resolves a URL path into a typed entity identity. |
| | | 33 | | /// Returns 200 with SlugPathResolution, 404 when not found, 400 when a segment is invalid. |
| | | 34 | | /// </summary> |
| | | 35 | | [HttpGet("resolve/{*path}")] |
| | | 36 | | public async Task<ActionResult<SlugPathResolution>> Resolve( |
| | | 37 | | string path, |
| | | 38 | | CancellationToken cancellationToken) |
| | | 39 | | { |
| | | 40 | | var segments = (path ?? string.Empty) |
| | | 41 | | .Trim('/') |
| | | 42 | | .Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| | | 43 | | .Select(s => s.ToLowerInvariant()) |
| | | 44 | | .ToList(); |
| | | 45 | | |
| | | 46 | | if (segments.Count == 0) |
| | | 47 | | return NotFound(); |
| | | 48 | | |
| | | 49 | | // Validate each segment — keywords are exempt from slug validation |
| | | 50 | | for (var i = 0; i < segments.Count; i++) |
| | | 51 | | { |
| | | 52 | | var seg = segments[i]; |
| | | 53 | | if (!KeywordSegments.Contains(seg) && !SlugGenerator.IsValidSlug(seg)) |
| | | 54 | | return BadRequest(); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | var currentUserId = _currentUserService.IsAuthenticated |
| | | 58 | | ? (Guid?)((await _currentUserService.GetCurrentUserAsync())?.Id) |
| | | 59 | | : null; |
| | | 60 | | |
| | | 61 | | var resolution = await _resolver.ResolveAsync(segments, currentUserId, cancellationToken); |
| | | 62 | | |
| | | 63 | | return resolution == null ? NotFound() : Ok(resolution); |
| | | 64 | | } |
| | | 65 | | } |