using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Extensions; using PostFundManagement.Domain.Abstractions; using PostFundManagement.Domain.Errors; using PostFundManagement.Domain.Extensions; using PostFundManagement.Domain.Security; using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails; namespace PostFundManagement.Api.Endpoints.Evidence; [ApiVersionTarget(1)] public sealed class GetEvidenceUrlEndpoint : IEndpoint { public void Map(IEndpointRouteBuilder builder) { builder.MapGet("api/evidence/{evidenceId}/securelink", async (string evidenceId, IEvidenceService service, CancellationToken cancellationToken = default) => await service.GetEvidenceUrlAsync(evidenceIdHash: evidenceId, cancellationToken) switch { { IsSuccess: true, Value: var evidenceUrl } => Results.Ok(evidenceUrl), var r when r.HasError() => Results.Forbid(), var r when r.HasError() => Results.NotFound(), var r => Results.InternalServerError(r.Errors.ToProblems()) }) .WithDescription("Gets a secure download URL of the evidence file, URL expires (default: 15min)") .WithName(typeof(GetEvidenceUrlEndpoint).ToEndpointName()) .MapToApiVersion(new ApiVersion(1)) .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound) .Produces(StatusCodes.Status403Forbidden) .Produces(StatusCodes.Status401Unauthorized) .Produces(StatusCodes.Status500InternalServerError) .WithTags(EndpointTags.Grants) .CacheOutput(); } }