using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Extensions; using PostFundManagement.Domain.Abstractions; using PostFundManagement.Domain.Errors; using PostFundManagement.Domain.Extensions; using PostFundManagement.Domain.Models; namespace PostFundManagement.Api.Endpoints.Projects; [ApiVersionTarget(1)] public sealed class GetOutcomeEndpoint : IEndpoint { public void Map(IEndpointRouteBuilder builder) { builder.MapGet("api/projects/outcome/{outcomeId}", HandleGetAsync) .WithDescription("Gets a specific project outcome") .RequireAuthorization(Constants.MainScope) .WithName(typeof(GetOutcomeEndpoint).ToEndpointName()) .MapToApiVersion(new ApiVersion(1)) .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status403Forbidden) .Produces(StatusCodes.Status401Unauthorized) .Produces(StatusCodes.Status404NotFound) .Produces(StatusCodes.Status500InternalServerError) .WithTags(EndpointTags.Projects) .CacheOutput(); builder.MapGet("api/projects/outcomes/single/{outcomeId}", HandleGetAsync) .WithDescription("Gets a specific project outcome") .RequireAuthorization(Constants.MainScope) .WithName(typeof(GetOutcomeEndpoint).ToEndpointName("Single")) .MapToApiVersion(new ApiVersion(1)) .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status403Forbidden) .Produces(StatusCodes.Status401Unauthorized) .Produces(StatusCodes.Status404NotFound) .Produces(StatusCodes.Status500InternalServerError) .WithTags(EndpointTags.Projects) .CacheOutput(); } private static async Task HandleGetAsync(string outcomeId, IProjectService service, CancellationToken cancellationToken = default) => await service.GetOutcomeAsync(outcomeId, cancellationToken) switch { { IsSuccess: true, Value: var outcome } => Results.Ok(outcome), var r when r.HasError() => Results.Forbid(), var r when r.HasError() => Results.NotFound(r.Errors.ToProblems()), var r => Results.InternalServerError(r.Errors.ToProblems()) }; }